9.22.2012

is vs as operators in C#

Difference between is and as operators in C#
S.No
Is operator
As opeator
1
Meaning:
The is operator allows us to check whether an object is compatible with a specific type.
Here the phrase "is compatible" means that an object is either of that type or is derived from that type.
Meaning:
The as operator is used to perform certain types of conversions between compatible reference types.
i.e., The as operator is used to perform explicit type conversion of reference type.If the type being converted is compatible with the specified type,then that conversion is successful else as operator will return a null value.
2
How it works ?
i.Checks whether Object is compatible with given type
ii. If yes, returns true
iii. If no, returns false

Remarks:
An is expression evaluates to true if both of the following conditions are met:
expression is not null.
expression can be cast to type.
That is, a cast expression of the form (type)(expression) will complete without throwing an exception. For more information, see 7.6.6 Cast expressions.
How it works ?
i.Checks whether Object is compatible with given type
ii. If yes, returns not null reference i.e same object
iii. If no, returns null instead of raising an exception.
3
When to use is operator ?
The is-cast is only ideal if the casted variable will not be needed for further use.
When to use as operator ?
If we need to use the casted variable, use the as-cast.
4
For what type of conversions,is operator can be used ?
is operator only considers reference conversions, boxing conversions, and unboxing conversions.
For what type of conversions,as operator can be used ?
as operator only performs reference conversions and boxing conversions.
5
Syntax:
expression is type
Syntax:
expression as type
6
Example:
int k = 0;
if (k is object)
{
Console.WriteLine("i is an object");
}
Example:
object x = "I am here";
object y = 10;

string s1 = x as string; // here conversion is compatible so,s1="I am Here"

string s2 = y as string;// here conversion is imcompatible so,s2=null

Notes:

1.Syntax: expression as type is equivalent to:
expression is type ? (type)expression : (type)null

Example: Employee e = obj as Employee; is equivalent to:
Employee e = obj is Employee ? (Employee)obj : (Employee)null;

2.Both is and as operators cannot be overloaded.

3.The is operator will never throw an exception.

4. A compile-time warning will be issued if the expression expression is type is known to always be true or always be false.

5. Both is and as operators cannot use user-defined conversions

6.The as cast is an efficient, elegant way to cast in the C# language. It promotes exception-neutral and less redundant code.

References:








1 comment:

  1. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article. Dot Net Training in chennai | Dot Net Training institute in velachery

    ReplyDelete