tagged [casting]
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...
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...
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...
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...
- Modified
- 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...
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 ...
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...
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...
- Modified
- 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...
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...
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 ...
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...
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...
- Modified
- 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 ...
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...
- Modified
- 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()....
- Modified
- 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...
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...
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...
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...
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...
- Modified
- 15 Oct at 13:55
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...
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...
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...