is and as operator in c#

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 1703 red flag

is and as operator in c#

Is and as operator in c#

In this article we will learn two operator in c#.NET , they are "is" and "as". Basically those two operator is useful when we want to compare two object and try to convert one object into another one. Let’s see with example.

"Is" operator

It is useful when we want to check whether one object belongs to particular class or not?. Have a look on below code.

using System;
using System.Text;
namespace Asynchronious
{
    class Test
    {
        public string Name { get; set; }
    }
    class Test1:Test
    {
        public string Surname { get; set; }
    }
        class Program
        {
            public static void Main(String[] args)
            {
                Test t = new Test();
                if (t is Test1)
                    Console.WriteLine("t is object of Test1");
                else
                    Console.WriteLine("t is not object of Test1");
 
                Console.ReadLine();
            }
        }
}

Here we have declared two classes called Test and Test1 and Test1 is inherited from Test class. Now, within Main() function we are creating object of Test class. Here we are interested to check whether t is object of Test1 or not? And obviously it is not object of Test1. So, using is operator we are checking within if condition.

Here is sample output.


In output we are seeing that else condition is getting satisfied. So t is not object is Test1 class.

"as" operator

as operator is useful when we want to convert one type to another type. But we have to keep in mind that both type should be compatible.  Means by applying boxing and unboxing concept it should possible to convert one type to another type. Let’s see by example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
namespace Asynchronious
{
    class Test
    {
        public Test() { }
        public string Name { get; set; }
    }
    class Test1 : Test
    {
        public Test1() { }
        public string Surname { get; set; }
    }
        class Program
        {
            public static void Main(String[] args)
            {
                Test1 t1 = new Test1();
                t1.Name = "Sourav";
                t1.Surname = "Kayal";
 
                Test t = new Test();
                t = t1 as Test;
                Console.WriteLine("Name:- " + t.Name);
                Console.ReadLine();
            }
        }
}

Here is sample output.


Here we have Implemented inheritance concept between Test and Test1 class. Now, within Main() function we are populating object ob Test1 class(sub class) and then we are converting from t1 object to object of Test class using below line.

t= t1 as Test

In output we are seeing that value of t1 object is initiated into t object.

conclusion:-
In this article we have seen how to use "is" and "as" operator in c#.NET. Hope you have understood the concept.
Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)