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:
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