3

Possible Duplicate:
What is the difference between the following casts in c#?

While am working on C#, her am doing type casting at that point I got a doubt:-

What is the difference between Object type casting for "as vs. (int)/(string)/... soon"?

Example:

int a = (int) value;

VS.

int a = value as int;

string a = (string) value;

VS.

string a = value as string;

and soon...

Can any help explain this in detail?

Thanks in advance.

Community
  • 1
  • 1
Siva Charan
  • 17,940
  • 9
  • 60
  • 95

5 Answers5

3

From msdn:

The as operator is used to perform certain types of conversions between compatible reference or nullable types.

Remarks:

The as operator is like a cast operation. However, if the conversion is not possible, as returns null instead of raising an exception. Consider the following expression:

expression as type

It is equivalent to the following expression except that expression is evaluated only one time.

expression is type ? (type)expression : (type)null

Note that the as operator only performs conversions to reference or nullable types, and boxing conversions. The as operator cannot perform other conversions, such as user-defined conversions, which should instead be performed by using cast expressions.

SynerCoder
  • 12,493
  • 4
  • 47
  • 78
1

You cannot use as for non-nullable value type, you can only use as for nullable one only. What I mean is that the below line will give you a build error/

int a = value as int;

Any way, the as returns null if the cast is not valid. We usually use it.

Class1 c1 = value as Class1;
if (c1 != null)
{
    //do your logic.
}
Ghyath Serhal
  • 7,466
  • 6
  • 44
  • 60
  • 3
    You can't use `as` for a *non-nullable* value type. You can use it for a nullable one, e.g. `int? y = x as int?;` – Jon Skeet Nov 15 '11 at 07:21
  • 1
    I was *contradicting* you. Your first sentence is incorrect, in the way indicated by my comment. – Jon Skeet Nov 15 '11 at 07:24
  • 1
    Better, although still not as clear as it might be. (My comment was *in the context* of your original sentence.) – Jon Skeet Nov 15 '11 at 07:30
1

The major difference between prefix- and as-casting is what happens when the cast fails. Imagine, for instance, that g is really an instance of AnotherSpecificType, which is not a subclass of SpecificType. In this case, the prefix-cast throws an exception — however, the as-cast returns null when the cast fails, letting the execution of the program barrel on.

Look at this article:

https://sites.google.com/site/gmamaladze/projects/short-articles/prefix-casting-versus-as-casting-in-c

George Mamaladze
  • 7,593
  • 2
  • 36
  • 52
0

Using the as operator differs from a cast in C# in three important ways:

  • It returns null when the variable you are trying to convert is not of the requested type or in it's inheritance chain, instead of throwing an exception.
  • It can only be applied to reference type variables converting to reference types.
  • Using as will not perform user-defined conversions, such as implicit or explicit conversion operators, which casting syntax will do.
Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
0

If you try casting as as on a value type, for example int a = value as int;, you will get an InvalidCastException. Value types must be casted as (int) intValue.

If you are considering any reference type. You may use as where failed on casting will return null. You may use direct casting like (string) value, here will get an exception if the casting fails.

Abdul Munim
  • 18,869
  • 8
  • 52
  • 61