tagged [casting]

C# Casting vs. Parse

C# Casting vs. Parse Which of the following is better code in c# and why? or Ultimately, is it better to cast or to parse?

2 Apr at 08:53

Object As Interface

Object As Interface I've got an object that implements an interface, I then find that object using reflection. How can I cast the object into the interface and then place it into a `List` ?

How to cast or convert an unsigned int to int in C?

How to cast or convert an unsigned int to int in C? My apologies if the question seems weird. I'm debugging my code and this seems to be the problem, but I'm not sure. Thanks!

4 Apr at 06:52

Cast operation precedence in C#

Cast operation precedence in C# Will the differences below matter significantly in C#? Which one do you use?

return single instance object as IEnumerable

return single instance object as IEnumerable I have in instance of class foo and i want to return it as IEnumerable. Can i do it without creating a new list etc.. Perhaps something like the following:

3 Aug at 18:6

invalid cast exception on int to double

invalid cast exception on int to double Maybe I'm crazy, but I thought this was a valid cast: Why is LinqPad throwing a > InvalidCastException: Specified cast is not valid. ?

29 Sep at 01:0

Haskell: Converting Int to String

Haskell: Converting Int to String I know you can convert a `String` to an number with `read`: But how do you grab the `String` representation of an `Int` value?

5 Feb at 00:58

Why does (int)(object)10m throw "Specified cast is not valid" exception?

Why does (int)(object)10m throw "Specified cast is not valid" exception? Why this explicit cast does throw `Specified cast is not valid.` exception ? But this works:

9 Oct at 17:11

C# and Interfaces - Explicit vs. Implicit

C# and Interfaces - Explicit vs. Implicit In C#, if a class *has all the correct methods/signatures for an Interface*, but **doesn't** explicitly implement it like: Can the class still be cast as that...

2 May at 02:43

What is the difference between casting and conversion?

What is the difference between casting and conversion? Eric Lippert's comments in [this question](https://stackoverflow.com/questions/3166349/what-is-the-type-in-typeobjectname-var) have left me thoro...

23 May at 12:34

What's wrong with casting 0.0 to double?

What's wrong with casting 0.0 to double? I have `InvalidCastException` when I try to cast 0.0 to double, why is that so? It's fine when I do `(float)value` instead. ![alt text](https://i.stack.imgur.c...

6 Nov at 14:6

Fastest way to convert string to integer in PHP

Fastest way to convert string to integer in PHP Using PHP, what's the fastest way to convert a string like this: `"123"` to an integer? Why is that particular method the fastest? What happens if it ge...

27 Oct at 05:21

How to convert string to boolean php

How to convert string to boolean php How can I convert string to `boolean`? it returns, > boolean true but it should be `boolean false`.

20 Jan at 15:27

Which is preferred: new Nullable<int> or (int?)null?

Which is preferred: new Nullable or (int?)null? Which way is preferred in expressions like this: is it better to cast on `null` or create a `new Nullable` ?

29 Apr at 17:43

Hive cast string to date dd-MM-yyyy

Hive cast string to date dd-MM-yyyy How can I cast a string in the format 'dd-MM-yyyy' to a date type also in the format 'dd-MM-yyyy' in Hive? Something along the lines of:

9 Sep at 09:9

C convert floating point to int

C convert floating point to int I'm using (not C++). I need to convert a float number into an `int`. I do not want to round to the the nearest number, I simply want to eliminate what is after the inte...

Why can't I cast DateTime[] to object[]?

Why can't I cast DateTime[] to object[]? It seems that I can cast DateTime to object, so why can't I cast array DateTime[] to object[]? I know this has something to do with value/reference types, but ...

16 May at 23:51

Why cannot cast Integer to String in java?

Why cannot cast Integer to String in java? I found some strange exception: How it can be possible? Each object can be casted to String, doesn't it? The code is: Thanks.

23 Jan at 14:44

How to cast ArrayList<> from List<>

How to cast ArrayList from List Can somebody please explain me why I can't cast `List` to `ArrayList` with first approach and I do with second one? Thank you. First approach: Second approach:

27 Feb at 21:53

Cast to a reflected Type in C#

Cast to a reflected Type in C# Consider the following code: Do I need to do some magic with `typeFoo` to get the result?

5 Aug at 08:2

What's the C# equivalent to C++'s dynamic_cast?

What's the C# equivalent to C++'s dynamic_cast? This C++ code checks if `o` is a `Node *` and if so, calls a method on `d`. What's the shortest and/or most efficient way to write the equivalent in C#?

16 Feb at 17:44

Cast List<int> to List<string> in .NET 2.0

Cast List to List in .NET 2.0 Can you cast a `List` to `List` somehow? I know I could loop through and .ToString() the thing, but a cast would be awesome. I'm in C# 2.0 (so no [LINQ](http://en.wikiped...

30 Aug at 16:35

Casting an out-of-range number to an enum in C# does not produce an exception

Casting an out-of-range number to an enum in C# does not produce an exception The following code does not produce an exception but instead passes the value 4 to tst. Can anyone explain the reason behi...

22 Feb at 15:24

Is it possible to assign a base class object to a derived class reference with an explicit typecast?

Is it possible to assign a base class object to a derived class reference with an explicit typecast? Is it possible to assign a base class object to a derived class reference with an explicit typecast...

Is using "as" in C# a safe way of casting?

Is using "as" in C# a safe way of casting? I am wondering if using the keyword "as" in the following code is a safe way (i.e. won't blow up) of casting in C#: Is there a safer way of casting?

4 Apr at 00:25