728x90
반응형

[예제1]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string a = "10";    // 원래 스트링 타입
            
            //var, object, dynamic의 공통점은 모든 타입을 다 받아 준다.
            var b = "10";       // 컴파일 할때 타입이 결정된다.
                                // 결정이 나면 타입 변경 불가

            object c = "10";    // 컴파일 할때 타입이 결정된다. (Compile Time)
                                // 결정이 나도 이후에 타입 변경 가능

            // b = 1000;        // 컴파일 에러
            c = 1000;           // 이 시점부터 int 타입이 된다. (Run time)
            dynamic d = "10";   // 실행될때 타입이 결정된다.

            Console.WriteLine(a.GetType());
            Console.WriteLine(b.GetType());
            Console.WriteLine(c.GetType());
            Console.WriteLine(d.GetType());

            Console.WriteLine(int.Parse(a));
            Console.WriteLine(int.Parse(b));
            //Console.WriteLine(int.Parse(c)); // 문법에러
            Console.WriteLine(int.Parse(d));

            // string  : 그냥 표준 타입일 뿐이다.
            // var     : 모든 타입을 다 받아준다.
            // object  : 컴파일 중에서도 타입을 변경할수 있도록 한다.
            // dynamic : 실행 중에도 타입을 변경할 수 있도록 한다. 

        }
    }
}


[예제2]

using System;
using static System.Console;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteLine(5 / 3);
            WriteLine(5 % 3);
            WriteLine(1 + 2);

            WriteLine(1 + "2");
            WriteLine("1" + 2);
            WriteLine("1" + "2");
        }
    }
}

 


[예제3]

using System;
using static System.Console;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "abcdef";
            WriteLine(s.Length);
        }
    }
}


[예제4]

using System;
using static System.Console;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "abcdef";
            WriteLine(s.Length);

            // 구분선
            WriteLine("----------------------------");
            
        }
    }
}


[예제5]

using System;
using static System.Console;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "abcdef";
            WriteLine(s.Length);

            // 구분선
            WriteLine("----------------------------");

            // Exception 발생 안함
            string s2 = null;
            WriteLine(s2?.Length); //  Elvis 연산자

            // Exception 발생
            try
            {
                string s3 = null; // Unhandled exception
                WriteLine(s3.Length);
            }
            catch (Exception e)
            {
                //WriteLine("{0}", e);
            }
            WriteLine("----------------------------");
            WriteLine(1000);
            
            //
            int[] ar = null;
            WriteLine(ar?[0]); // exception 발생
            WriteLine(2000);
        }
    }
}


[예제6]

using System;
using static System.Console;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int? a = null;
            int? b = 30;

            WriteLine(a ?? 10); // ?? null 병합
            WriteLine(b ?? 20); // ?? null 병합
        }
    }
}

 

 

728x90
반응형

+ Recent posts