Home > DeveloperSection > Forums > Difference between 'is'and 'as' operator in C#
Anupam Mishra

Total Post:135

Points:949
Posted on    January-27-2016 10:40 PM

 C# C#  .NET 
Ratings:


 1 Reply(s)
 316  View(s)
Rate this:
Hi Everyone,
I want to ' is ' operator in my program. So, please  anyone can give me a exact difference of 'is' and 'as' operator in c#.

Thank you.


Anupam Mishra

Total Post:135

Points:949
Posted on    January-28-2016 3:21 AM

“IS” operator is useful to check if objects are compatible with a type. For instance in the below code we are checking if “stud” object is a type of “Student” class.

object stud = new Student();
if(stud== Student)
{

“AS” operator helps to do conversion from one type to other type. For instance in the below code we are converting object to a string data type. If the “AS” operator is not able to type cast it returns NULL

object obj = "Anupam";
string str = obj as string;

Don't want to miss updates? Please click the below button!

Follow MindStick