티스토리 뷰
타입 캐스팅 시 성공하면 캐스팅 된 값을 반환하고
실패하면 null 값을 반환
as를 사용할 때는 null을 받을 수 있는 타입을 사용해야 한다.
class Weapon
{
}
class Player
{
public void Attack(object weapon)
{
Weapon myWeapon = weapon as Weapon;
}
}
class Program
{
static void Main(string[] args)
{
Player p = new Player();
Weapon sword = new Weapon();
p.Attack(sword);
}
}
as Keyword가 캐스팅 된 값을 반환한다면 is Keyword는 캐스팅 가능 여부를 bool 값으로 반환한다.
사용은 as Keyword와 동일.
'프로그래밍 > C#' 카테고리의 다른 글
new 한정자, sealed Keyword (0) | 2016.07.31 |
---|---|
Property(프로퍼티), Indexer(인덱서) (0) | 2016.07.30 |
가변 길이 매개 변수 (0) | 2016.07.29 |
nullable 데이터 타입, var Keyword (0) | 2016.07.29 |
ref keyword, out keyword (0) | 2016.07.27 |
댓글