Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ever cast reference types when you can use "as"? [duplicate]

Tags:

c#

casting

Possible Duplicate:
Casting: (NewType) vs. Object as NewType

In C#, why ever cast reference types when you can use "as"?

Casting can generate exceptions whereas "as" will evaulate to null if the casting fails.

Wouldn't "as" be easier to use with reference types in all cases?

eg:

MyObject as DataGridView

rather than,

(DataGridView)MyObject
like image 502
CJ7 Avatar asked Aug 13 '10 09:08

CJ7


People also ask

What is type casting in C#?

Type casting is when you assign a value of one data type to another type.

What is type casting and type conversion in C?

1. In type casting, a data type is converted into another data type by a programmer using casting operator. Whereas in type conversion, a data type is converted into another data type by a compiler. 2. Type casting can be applied to compatible data types as well as incompatible data types.

What do you know about type casting?

Type casting is a way of converting data from one data type to another data type. This process of data conversion is also known as type conversion or type coercion. In Java, we can cast both reference and primitive data types. By using casting, data can not be changed but only the data type is changed.

How do you type a cast?

Typecasting is making a variable of one type, such as an int, act like another type, a char, for one single operation. To typecast something, simply put the type of variable you want the actual variable to act as inside parentheses in front of the actual variable. (char)a will make 'a' function as a char.


2 Answers

Consider the following alternatives:

Foo(someObj as SomeClass);

and:

Foo((SomeClass)someObj);

Due to someObj being of the wrong type, the first version passes null to Foo. Some time later, this results in a NullReferenceException being thrown. How much later? Depends on what Foo does. It might store the null in a field, and then minutes later it's accessed by some code that expects it to be non-null.

But with the second version, you find the problem immediately.

Why make it harder to fix bugs?

Update

The OP asked in a comment: isn't is easier to use as and then check for null in an if statement?

If the null is unexpected and is evidence of a bug in the caller, you could say:

SomeClass c = someObj as SomeClass;
if (c == null)
{
    // hmm...
}

What do you do in that if-block? There are two general solutions. One is to throw an exception, so it is the caller's responsibility to deal with their mistake. In which case it is definitely simpler to write:

SomeClass c = (SomeClass)someObj;

It simply saves you writing the if/throw logic by hand.

There is another alternative though. If you have a "stock" implementation of SomeClass that you are happy to use where nothing better is available (maybe it has methods that do nothing, or return "empty" values, etc.) then you could do this:

SomeClass c = (someObj as SomeClass) ?? _stockImpl;

This will ensure that c is never null. But is that really better? What if the caller has a bug; don't you want to help find bugs? By swapping in a default object, you disguise the bug. That sounds like an attractive idea until you waste a week of your life trying to track down a bug.

(In a way this mimics the behaviour of Objective-C, in which any attempt to use a null reference will never throw; it just silently does nothing.)

like image 82
Daniel Earwicker Avatar answered Oct 26 '22 18:10

Daniel Earwicker


operator 'as' work with reference types only.

like image 22
Arseny Avatar answered Oct 26 '22 17:10

Arseny