Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best practice for syntax in casting a variable?

Tags:

c#

Which (if any) is more correct? Why?

string someVariable = (string) someOtherVariable;
string someVariable = someOtherVariable.ToString();
string someVariable = someOtherVariable as string;

I've used all three, but I don't have any preference or understanding why one is better than the other.

like image 296
Deane Avatar asked Jan 12 '10 21:01

Deane


People also ask

What is type casting mention syntax?

Type casting refers to changing an variable of one data type into another. The compiler will automatically change one type of data into another if it makes sense. For instance, if you assign an integer value to a floating-point variable, the compiler will convert the int to a float.

What is casting a variable?

Casting and Ranges of Variables. In Java, type casting is used to convert variable values from one type to another. By casting we don't mean something to do with fishing, but it is a similar idea to casting a pot in clay. In Java when you cast you are changing the “shape” (or type) of the variable.

How do you cast a variable in Python?

Specify a Variable Type Casting in python is therefore done using constructor functions: int() - constructs an integer number from an integer literal, a float literal (by removing all decimals), or a string literal (providing the string represents a whole number)

What is type casting in C programming show with an example?

In type casting, the compiler automatically changes one data type to another one depending on what we want the program to do. For instance, in case we assign a float variable (floating point) with an integer (int) value, the compiler will ultimately convert this int value into the float value.


2 Answers

These are not all examples of casting.

This is a cast:

string someVariable = (string) someOtherVariable;

This is method call:

string someVariable = someOtherVariable.ToString();

And this is a safe cast:

string someVariable = someOtherVariable as string;

The first and third examples are actual casts. The first cast has the potential to throw an InvalidCastException whereas the third example will not throw that exception. That is why the as operator is known as a safe cast.

like image 188
Andrew Hare Avatar answered Sep 22 '22 09:09

Andrew Hare


Here's my article on the subject.

http://blogs.msdn.com/ericlippert/archive/2009/10/08/what-s-the-difference-between-as-and-cast-operators.aspx

As for which one is "most correct", the one that is most correct is the one that has the meaning you intend to convey to the reader of the program.

"ToString()" conveys "this is probably not a string; if it is not, then I wish to obtain from the object a string which represents it."

The "cast" operator conveys either "this is a string, and I am willing to have my program crash if I am wrong", or the opposite, "this is not a string and I want to call a user-defined conversion on this object to string".

The "as" operator conveys "this might be a string and if it isn't, I want the result to be null."

Which of those four things do you mean?

like image 38
Eric Lippert Avatar answered Sep 26 '22 09:09

Eric Lippert