as and as in C#

These keywords are used in scenarios where there is compatible type conversion or we need to check for type compatibility.

Normally if you try to cast a variable to an incompatible type then it will give you an exception. For example the following conversion will give you an exception:-

object str = "Hi";
int a = (int)(str);

So in order to avoid the exception you might have to enclose that conversion in try - catch block, but that won't be a good thing to do. A much better approach would be to first check whether the given variable can be converted to the specified type or not. Here comes the role of 'is' keyword. The 'is' keyword evaluates whether an object instance is compatible (or convertible) to a type or not. For example the is keyword evaluates to true in the following scenarios :-

public class Class1 : Interface1

public class Class2 : Class1

Class1 obj1 = new Class1();

Class2 obj2 = new Class2();

Class1 obj3 = new Class2();

Console.WriteLine(obj1 is Interface1);  //true
Console.WriteLine(obj1 is Class1);       //true
Console.WriteLine(obj1 is Class2);       //false

Console.WriteLine(obj2 is Class1);       //true
Console.WriteLine(obj2 is Class2);       //true

Console.WriteLine(obj3 is Class1);       //true
Console.WriteLine(obj3 is Class2);       //true

Note that the 'is' keyword only considers compatible reference conversions, boxing and unboxing conversions. It does not take into account user-defined conversions or implicit or explicit conversions. For example it will return false in the following case :-

int a = 1;
Console.WriteLine(value is long);        //false

So now you can safely convert an object type by checking if the conversion is possible or not before doing the actual type conversion, however there is one problem this method of first checking the possibility of a conversion and then performing the conversion is not optimized as in this case if we are casting an expression then the expression will be evaluated two times. First, while testing it and second, during the actual conversion. A much better approach would be to use the 'as' operator which is explained below.

Alike 'is' keyword the 'as' operator is used to perform type conversions between compatible reference types. It is similar to casting a variable type except for the fact that if the conversion is not possible then instead of raising an exception it returns null.

So the below two code segments are equivalent to each other, except that the first one is more optimized and much more cleaner than the second one.

exp as type

exp is type ? (type)exp : (type)null

However, there in one thing to note that the 'as' operator can perform only compatible reference conversions, nullable conversions, and boxing conversions, it cannot perform conversions that require an explicit cast operator. For example it will return null in the below code :-

int a = 1;
string str = a as string;            //  the value of str  will be null after executing this line

In the above code segment the value of str will be null after the execution of second line.

No comments:

Post a Comment