티스토리 뷰



List of All New Features in C# 6.0: Part 1


마이크로 소프트는 Visual Studio 2015에서 사용되는 C# 6.0에 대한 새로운 키워드와 기능들을 발표하였습니다. 



이 글에서는 아래의 주제를 다루도록 할것입니다. 


  • Static 사용하는 방법
  • 자동 프로퍼티 초기화 하기
  • Dictionary 초기화 하기


테스트를 위해서 VS 2015를 시작하고 프로젝트를 생성하십시오. 콘솔 어플리케이션을 선택하고 프로젝트를 생성합니다. 



OK 버튼을 누르고, Solution Explorer을 확인합니다. Program.cs를 통해서 앞으로의 내용을 테스트 해볼 것입니다. 



1. Static 사용하기


기존에 Static 클래스의 함수를 호출할 때는 Static클래스 이름.함수이름과 같은 방식으로 사용하였습니다. 이는 동일한 클래스를 사용하는 경우에도 무조건 클래스의 이름을 중복으로 들어가게 끔 만들었습니다. 하지만 6.0에서는 네임스페이스에 Static 클래스를 추가하여 함수를 바로 호출할 수 있게끔 변경되었고, 이는 C# 컴파일러에 새로운 기능이 추가가 되었기 때문입니다. 


[C# 5.0에서의 Static 사용 그림]




[C# 6.0에서의 Static 사용 그림]




[5.0에서의 해당 코드]


using System;  

using System.Collections.Generic;  

using System.Linq;  

using System.Text;  

using System.Threading.Tasks;  

namespace TestNewCSharp6  

{  

    class Program  

    {  

        static void Main(string[] args)  

        {  

            Console.WriteLine("Enter first value ");  

            int val1 =Convert.ToInt32(Console.ReadLine());  


            Console.WriteLine("Enter next value ");  

            int val2 = Convert.ToInt32(Console.ReadLine());  


            Console.WriteLine("sum : {0}", (val1 + val2));  

            Console.ReadLine();  

        }  

    }  

}  


[6.0에서 해당 코드]


using System;  

using System.Collections.Generic;  

using System.Linq;  

using System.Text;  

using System.Threading.Tasks;  

using System.Convert;  

using System.Console;  

namespace Project1  

{  

    class Program  

    {  

        static void Main(string[] args)  

        {  

            WriteLine("Enter first value ");  

            int val1 = ToInt32(ReadLine());  

            WriteLine("Enter next value ");  

            int val2 = ToInt32(ReadLine());  

            WriteLine("sum : {0}", (val1+val2));  

            ReadLine();  

        }  

    }  

}  


코드와 그림에 따라서 어떠한 차이가 있는지 확인할 수 있습니다. 아래의 그림은 위의 코드를 실행시킨것으로 동일한 결과를 얻는 것을 확인할 수 있습니다. 




2. 자동 프로퍼티 초기화 하기


자동 프로퍼티 초기화는 프로퍼티에 값을 할당하기위한 새로운 컨셉으로 프로퍼티 선언중에 사용할 수 있습니다. 따라서 사용자는 기본 값을 read=only 프로퍼티에 설정할 수 있습니다. 5.0 버전에서는 이를 위해서 클래스의 기본 생성자에서 설정을 해야 했습니다. 다음 예를 살펴보겠습니다. 특정 프로퍼티에 값을 설정하기 위해서는 아래와 같이 사용해야 합니다. 


[In C# 5.0]




[Code]


using System;  

using System.Collections.Generic;  

using System.Linq;  

using System.Text;  

namespace TestNewCSharp6  

{  

    class Emp  

    {  

        public Emp()  

        {  

            Name = "nitin";  

            Age = 25;  

            Salary = 999;  

        }  

        public string Name { get; set; }  

        public int Age { get; set; }  

        public int Salary { get;private set; }  

    }  

}  


6.0에서는 아래와 같이 프로퍼티 선언시에 기본값을 설정할 수 있습니다. 


[In C# 6.0]




[Code]


using System;  

using System.Collections.Generic;  

using System.Linq;  

using System.Text;  

using System.Threading.Tasks;  

  

namespace Project2  

{  

    class Emp  

    {  

        public string Name { get; set; }="nitin";  

        public int Age { get; set; }=25;  

        public int Salary { get; }=999;  

    }  

}  


3. Dictionary Initializer


Dictionary 초기화는 C# 6.0의 새로운 컨셉입니다. 6.0에서는 Dictionary Collection의 키에 대한 값을 바로 초기화 할 수 있습니다. 또한 키는 string 또는 다른 데이터 타입으로 만들 수도 있습니다. 5.0과 6.0 버전에서 어떻게 차이가 나는지 아래의 예를 통해서 살펴보도록 하겠습니다.


[C# 5.0]


[Code]


using System;  

using System.Collections.Generic;  

using System.Data;  

using System.Linq;  

using System.Text;  

using System.Threading.Tasks;  

namespace TestNewCSharp6  

{  

    class Program  

    {  

        static void Main(string[] args)  

        {  

            Dictionary<string, string> dicStr = new Dictionary<string, string>()  

            {  

                {"Nitin","Noida"},  

                {"Sonu","Baraut"},  

                {"Rahul","Delhi"},  

            };  

            dicStr["Mohan"] = "Noida";  

            foreach (var item in dicStr)  

            {  

                Console.WriteLine(item.Key+"   "+ item.Value);  

            }  

            Console.WriteLine("********************************************************************");  

            Dictionary<int, string> dicInt = new Dictionary<int, string>()  

            {  

              {1,"Nitin"},  

              {2,"Sonu"},  

              {3,"Mohan"},  

            };  

            dicInt[4] = "Rahul";  

            foreach (var item in dicInt)  

            {  

                Console.WriteLine(item.Key + "   " + item.Value);  

            }  

            Console.Read();  

        }  

    }  

}  


[C# 6.0]




[Code]


using System;  

using System.Collections.Generic;  

using System.Data;  

using System.Linq;  

using System.Text;  

using System.Threading.Tasks;  

namespace Project3  

{  

    class Program  

    {  

        static void Main(string[] args)  

        {  

            Dictionary<string, string> dicStr = new Dictionary<string, string>()  

            {  

                ["Nitin"]="Noida",  

                ["Sonu"]="Baraut",  

                ["Rahul"]="Delhi",  

            };  

            dicStr["Mohan"] = "Noida";  

            foreach (var item in dicStr)  

            {  

                Console.WriteLine(item.Key + "   " + item.Value);  

            }  

            Console.WriteLine("********************************************************************");  

            Dictionary<int, string> dicInt = new Dictionary<int, string>()  

            {  

              [1]="Nitin",  

              [2]="Sonu",  

              [3]="Mohan"  

            };  

            dicInt[4] = "Rahul";  

            foreach (var item in dicInt)  

            {  

                Console.WriteLine(item.Key + "   " + item.Value);  

            }  

            Console.Read();  

        }  

    }  

}  


6.0에서는 = 연산자를 통해서 바로 초기화를 할 수 있습니다. 하지만 5.0에서는 {key, value}의 객체를 만들어 초기화를 해야 합니다. 



이 글은 c-shartcornet.com의 글을 한글 번역한 내용입니다. 

List of All New Features in C# 6.0: Part 1 [링크]


이 글에서는 C# 6.0의 변경 사항 중 1) Static 사용방법, 2) 프로퍼티 초기화, 3) Dictionary 초기화에 대해서 알아보았습니다.


이 글이 도움이 되셨나요?

그렇다면 아래의 그림을 클릭해주세요.


댓글