Is Keyword is used to check the type while As is used for type casting.
Exp-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IsAndAsKeyword
{
class Program
{
static void Main(string[] args)
{
object obj = "this is a test";
//check Is keyword
if (obj is string)
{
Console.WriteLine("obj is {0} type.", "string");
}
else
{
Console.WriteLine("obj is not {0} type.", "string");
}
//check As keyword
var a = obj as string;
if (a is string)
{
Console.WriteLine("obj as {0} type.", "string");
}
else
{
Console.WriteLine("obj as not {0} type.", "string");
}
Console.ReadLine();
}
}
}
Exp-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IsAndAsKeyword
{
class Program
{
static void Main(string[] args)
{
object obj = "this is a test";
//check Is keyword
if (obj is string)
{
Console.WriteLine("obj is {0} type.", "string");
}
else
{
Console.WriteLine("obj is not {0} type.", "string");
}
//check As keyword
var a = obj as string;
if (a is string)
{
Console.WriteLine("obj as {0} type.", "string");
}
else
{
Console.WriteLine("obj as not {0} type.", "string");
}
Console.ReadLine();
}
}
}
No comments:
Post a Comment