Difference between is and as operators in c#


Published on

Difference between is and as operators in C#

Published in: Technology, Sports
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Difference between is and as operators in c#

  1. 1. Difference between is and as operators in C# S.No Is operator As opeator 1 Meaning: Meaning: The is operator allows us to The as operator is used to perform check whether an object is certain types of conversions compatible with a specific type. between compatible reference Here the phrase "is types. compatible" means that an i.e., The as operator is used to object is either of that type or is perform explicit type conversion of derived from that type. 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 ? How it works ? i.Checks whether Object is i.Checks whether Object is compatible with given type compatible with given type ii. If yes, returns true ii. If yes, returns not null reference iii. If no, returns false i.e same object iii. If no, returns null instead of Remarks: raising an exception. 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. 3 When to use is operator ? When to use as operator ? The is-cast is only ideal if the If we need to use the casted casted variable will not be variable, use the as-cast. needed for further use. 4 For what type of For what type of conversions,as conversions,is operator can operator can be used ? be used ? as operator only performs is operator only considers reference conversions and boxing reference conversions, boxing conversions. conversions, and unboxing conversions. 5 Syntax: Syntax:
  2. 2. expression is type expression as type 6 Example: Example: int k = 0; object x = "I am here"; if (k is object) object y = 10; { Console.WriteLine("i is an string s1 = x as string; // here object"); conversion is compatible so,s1="I } am Here" string s2 = y as string;// here conversion is imcompatible so,s2=nullNotes:1.Syntax: expression as type is equivalent to:expression is type ? (type)expression : (type)nullExample: 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 toalways be true or always be false.5. Both is and as operators cannot use user-defined conversions6.The as cast is an efficient, elegant way to cast in the C# language. It promotesexception-neutral and less redundant code.References:http://www.dotnetspider.com/sites/961/Forum-1095-What-difference-between-operator-C.aspxhttp://msdn.microsoft.com/en-us/library/scekt9xw%28v=vs.100%29.aspxhttp://msdn.microsoft.com/en-us/library/cscsdfbt%28v=vs.80%29.aspxhttp://mr-ponna.com/Question/363/What-is-difference-between-is-and-as-operators-in-c-/http://msdn.microsoft.com/en-us/library/8edha89s.aspxhttp://www.c-sharpcorner.com/uploadfile/dhananjaycoder/type-casting-with-is-and-as-operator-in-C-Sharp/http://www.c-sharpcorner.com/uploadfile/abhikumarvatsa/the-is-and-as-operators-in-C-Sharp/http://www.dotnetperls.com/as
  3. 3. http://www.dotnetperls.com/isAnd, further updates on difference between questions and answers, please visit myblog @ http://onlydifferencefaqs.blogspot.in/