달력

52024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

[C#]as is

프로그래밍/C# 2017. 4. 22. 00:35
반응형
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            object[] obj = new object[2];
            obj[0] = 1;
            obj[1] = "strValue";

            // is는 특정 객체 형변환이 가능한지 여부 확인         
            Console.WriteLine("Test  : " + (obj[0] is int));
            Console.WriteLine("Test  : " + (obj[0] is string));

            // as는 특정 객체 형변환이 가능하면 형변환객체를 불가능 하면 null을 반환한다.
            Console.WriteLine("Test  : " + ((obj[0] as string) == null ? "null" : obj[0]));
            Console.WriteLine("Test  : " + ((obj[1] as string) == null ? "null" : obj[1]));

            Console.Read();
        }
    }    
}

 

실행화면

 

Posted by 유령회사
|