using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class BaseClass
{
}
public class DerivedClass : BaseClass
{
}
class Program
{
static void Main(string[] args)
{
BaseClass b = new BaseClass();
BaseClass d = new DerivedClass();
Console.WriteLine("b is BaseClass : {0}", b is BaseClass);
Console.WriteLine("b is DerivedClass : {0}", b is DerivedClass);
Console.WriteLine("d is BaseClass : {0}", d is BaseClass);
Console.WriteLine("d is DerivedClass : {0}", d is DerivedClass);
}
}
}
실행하면 결과는 다음과 같습니다.
b is BaseClass : True
b is DerivedClass : False
d is BaseClass : True
d is DerivedClass : True
if(b is DerivedClass)
.....
if(b as DerivedClass != null)
.....
그러므로 obj라는 객체가 BaseClass인지 DerivedClass인지 알기 위해서는
if(obj is BaseClass)
Console.WriteLine("obj is BaseClass");
else
Console.WriteLine("obj is DerivedClass");
그러므로
if(obj is DerivedClass)
Console.WriteLine("obj is DerivedClass");
else
Console.WriteLine("obj is BaseClass");
이렇게 해야 제대로 동작하게 되죠.
댓글 없음:
댓글 쓰기