tagged [casting]

Casting entire array of objects to string

Casting entire array of objects to string I have an array of type `object` which are strings. I would like to convert them to strings. What would be the quickest way of doing so? Eg.: I have this `obj...

5 Jul at 14:11

C#: Dynamic runtime cast

C#: Dynamic runtime cast I would like to implement a method with the following signature Anyone know how to do that? obj definitely implements castTo but needs to be cast properly in order to have som...

7 Feb at 20:6

Casting using System.Type - c#

Casting using System.Type - c# Is it possible to cast an object to a desired type using `System.Type?` as the reference? I had a search and the general consensus was no, although I was hoping there ma...

9 Dec at 16:16

What's the best way to extract a one-dimensional array from a rectangular array in C#?

What's the best way to extract a one-dimensional array from a rectangular array in C#? Say I have a rectangular string array - not a jagged array What's the best way to extract a one-dimensional array...

1 Oct at 16:28

Cast Generic<Derived> to Generic<Base>

Cast Generic to Generic I have a base WPF UserControl that handles some common functionality for derived UserControls. In the code-behind of any derived UserControl I call an event In my base UserCont...

18 Apr at 21:49

What's the best way to create a percentage value from two integers in C#?

What's the best way to create a percentage value from two integers in C#? I have two integers that I want to divide to get a percentage. This is what I have right now: This gives the right answer. But...

3 Jan at 22:25

Casting a generic class without specific type

Casting a generic class without specific type I have the following generic class Now, I'm getting an Object X which I know for sure is Home: ``` public void DoSomething(object x) { if(x is // Check ...

7 Jan at 21:51

Why can't Double be implicitly cast to Decimal

Why can't Double be implicitly cast to Decimal I don't understand the casting rules when it comes to decimal and double. It is legal to do this What confuses me however is that decimal is a 16 byte da...

16 Mar at 15:15

Cast child object to parent type for serialization

Cast child object to parent type for serialization I need to be able to cast an instance of a child object to an instance of its parent object. The cast above doesn't seem to work, and the object stil...

18 Jan at 02:27

Why does upcasting IDictionary<TKey, TValue> to IEnumerable<object> fail?

Why does upcasting IDictionary to IEnumerable fail? See the following code snippet: The above cast will throw an invalid cast exception. Actually, `IDictionary` also indirectly implements `IEnumerable...

18 Jul at 15:8

C# Cast Entire Array?

C# Cast Entire Array? I see this `Array.ConvertAll` method, but it requires a `Converter` as an argument. I don't see why I need a converter, when I've already defined an implicit one in my class: I'm...

14 Jan at 22:47

Why does null need an explicit type cast here?

Why does null need an explicit type cast here? The following code does not compile: In order to compile, it needs to be changed to Since both `b = null` and `b = a` are legal, this doesn't make sense ...

9 Apr at 16:44

string = string + int: What's behind the scenes?

string = string + int: What's behind the scenes? In C# you can implicitly concatenate a string and let's say, an integer: My questions are: 1. Why, by assuming the fact that you can implicitly concate...

21 Sep at 07:57

Converting a double to an int in Javascript without rounding

Converting a double to an int in Javascript without rounding In C# the following code returns 2: In Javascript, however, the only way of converting a "double" to an "int" that I'm aware of is by using...

24 May at 15:27

Casting a double as an int, does it round or just strip digits?

Casting a double as an int, does it round or just strip digits? Doing some calculations with doubles which then need to be cast to an int. So i have a quick question, when casting a double say 7.5 to ...

8 Nov at 15:33

Action<object, EventArgs> could not be cast to EventHandler?

Action could not be cast to EventHandler? I was wiring up an event to use a lambda which needed to remove itself after triggering. I couldn't do it by inlining the lambda to the += event (no accessabl...

6 Jan at 19:54

Why Enumerable.Cast does not utilize user-defined casts?

Why Enumerable.Cast does not utilize user-defined casts? Say, we have 2 classes: Then why ``` A a = new A { a = 0 }; B b = a; //OK List listA = new List { new A { a = 0 } }; List listB = listA.Cast()....

25 Sep at 04:32

Casting string to enum

Casting string to enum I'm reading file content and take string at exact location like this Output will always be either `Ok` or `Err` On the other side I have `MyObject` which have `ContentEnum` like...

24 Sep at 16:10

Coverting List of Dictionary to DataTable

Coverting List of Dictionary to DataTable Currently we are doing this by looping through each value of list and dictionary: ``` private DataTable ChangeToDictionary(List> list) { DataTable dat...

8 Mar at 12:11

How to get name of System.Xml.Linq.XNode?

How to get name of System.Xml.Linq.XNode? Each XML element, like `` has a name - "title". I use foreach to enumerate items supplied by method and handle each of them by tag name. I cast XNode to XElem...

1 Oct at 18:13

Casting to byte in C#

Casting to byte in C# > [What happens when you cast from short to byte in C#?](https://stackoverflow.com/questions/7575643/what-happens-when-you-cast-from-short-to-byte-in-c) Can someone explain wha...

23 May at 12:33

Converting Array to IEnumerable<T>

Converting Array to IEnumerable To my surprise, I get the following statement: to complain about not being able to convert from to . I thought that the latter was inheriting from the former. Apparentl...

Why cast to an interface?

Why cast to an interface? In Jesse Liberty's Programming C# (p.142) he provides an example where he casts an object to an interface. What is the point of this, particularly if the object's class imple...

25 Sep at 11:16

Why should casting be avoided?

Why should casting be avoided? I generally avoid casting types as much as possible since I am under the impression that it's poor coding practice and may incur a performance penalty. But if someone as...

12 Nov at 19:29

Cast LINQ result to ObservableCollection

Cast LINQ result to ObservableCollection The fact that it is a LINQ result might perhaps not be relevant for the question, but I'm mentioning it anyway - since this is the context which has resulted i...

23 Sep at 10:53