Understand type casting using “as”,”is” and "()" operator in C#

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

How to cast data type using "is" ,"as" and () operator

Understand type casting using “as”,”is” and "()" operator in C#

In this article we will learn how to type cast in C#. Type casting operation is very common operation in application development. We can use type casting operation when we want to cast one data type to another data type. For example if we want to convert value type to reference type and reference type to value type.

We can perform type casting operation by three types.

1)      Using is operator

2)      Using as operator

3)      Using parenthesis () operator

Using is operator

IS operator is used to check whether the type of an given object is compatible with another type of object or not? It returns boolean value after checking. If true then it’s possible to convert and if false then two types are not compatible. Try to understand below example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp
{
    class Base
    {
        public string Name { get; set; }
    }
    class Derive:Base
    {
        public string Surname { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //Create object of Object class
            Object o = new Object();
            bool t = o is Derive;
            Console.WriteLine(t);
 
            Base b = new Base();
            t = b is Base;
            Console.WriteLine(t);
 
            Console.ReadLine();
        }
    }
}

Here is output. In first casting both object are incompatible so that it’s showing False and in next  operation we are casting objects which belongs to same class , so that it’s possible to cast.


Using as operator:-

“As” operator is used to cast one data type from another data type. If there is no possibility of casting then it returns null. Have a look on below example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            String name = "sourav";
            object surname = "Kayal";
            String fullname = name + surname as string;
            Console.WriteLine("Full Name is:-" +  fullname);
            Console.ReadLine();
        }
    }
}

Here is sample example.


In this example we have cast object to string. It is possible to convert object to string in C# so that the surname is converted to string otherwise it would throw null as return type.

Using () operator

This is one example of explicit type conversion. Have a look on below example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp
{
    class Base
    {
        public string Name { get; set; }
    }
    class Derive:Base
    {
        public string Surname { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
 
            //() operator
            Derive d = new Derive();
            d.Name = "Sourav";
            d.Surname = "Kayal";
 
            Base b = (Base)d;
            Console.WriteLine(b.Name);
            Console.ReadLine();
        }
    }
}

In this example we are casting drive class object to Base class using () operator. Here is sample output.


Conclusion:-

In this article we have learned different ways to cast object in C#. Hope, you have understood the concept. Enjoy learning.

 

 

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)