Sponsored Ad

Tuesday, May 18, 2010

How to Check if Given Object is Compatible with given Type in C# | IS Operator

This sample program will help you to check given object compatibility with given type using is operator given in C#.

Like in this example we are comparing the int and string type to passing object type.

 

 

 

Program Output:

How to Check if Given Object is Compatible with given Type

 

 

C# Source Code:

 

using System;
using System.Text;

namespace Console_App
{
    public class Class1
    {
        public static void fun1(object obj)
        {
            if (obj is int)
            {
                Console.WriteLine("int");              
            }
            else if (obj is string)
            {
                Console.WriteLine("string");
            }

        }    
    }
    public class Class2
    {
        public static void Main()
        {
            Class1.fun1(1);
            Class1.fun1("1");
            Console.ReadLine();
        }
    }

}

0 comments:

Post a Comment

Sponsored Ad

More Related Articles

Website Update

Followers