... | ... | @@ -187,7 +187,7 @@ class FileState |
|
|
Console.WriteLine("async {0}, {1}", task2.Result, task6.Result);
|
|
|
}
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### **C# 6.0**
|
|
|
C# 6.0 구문을 사용한 C# 소스코드는 반드시 C# 6.0에서 compile해야 하지만 생성된 결과물은 C# 2.0의 것과 다르지 않기 때문에 실행은 설치된 닷넷 프레임워크의 버전 제약을 받지 않는다.
|
... | ... | @@ -222,7 +222,7 @@ public class Person |
|
|
```
|
|
|
|
|
|
* **람다 식을 이용한 method, property 및 index 정의**
|
|
|
C# 언어에는 클래스가 직관적으로 배열처럼 다뤄질 수 있을 때 사용하기 쉽도록 this keyword를 이용한 indexer라고 하는 특별한 구문을 제공한다.
|
|
|
C# 언어에는 클래스가 직관적으로 배열처럼 다뤄질 수 있을 때 사용하기 쉽도록 this keyword를 이용한 indexer라고 하는 특별한 구문을 제공한다. 생성자의 경우 method긴 하지만 예외적으로 람다 식을 이용해 구현할 수 없다.
|
|
|
```cs
|
|
|
public class Vector
|
|
|
{
|
... | ... | @@ -261,9 +261,69 @@ C# 언어에는 클래스가 직관적으로 배열처럼 다뤄질 수 있을 |
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
* **using static 구문을 이용한 타입명 생략**
|
|
|
생성자의 경우 method긴 하지만 예외적으로 람다 식을 이용해 구현할 수 없다.
|
|
|
기존의 static member를 사용하는 경우 반드시 타입명과 함께 써야만 했다. 소스코드 파일 범위 내에서는 static type의 정적 member를 타입명없이 바로 호출할 수 있다.
|
|
|
```cs
|
|
|
using System;
|
|
|
using static System.Console;
|
|
|
using static ConsoleApplication1.BitMode;
|
|
|
using static ConsoleApplication1.MyDay;
|
|
|
|
|
|
namespace ConsoleApplication1
|
|
|
{
|
|
|
public enum MyDay
|
|
|
{
|
|
|
Saturday, Sunday, // enum field 내부 구현은 static 속성을 갖는다.
|
|
|
}
|
|
|
|
|
|
public class BitMode
|
|
|
{
|
|
|
public const int ON = 1;
|
|
|
public const int OFF = 0;
|
|
|
public int Fail;
|
|
|
}
|
|
|
|
|
|
class Program
|
|
|
{
|
|
|
static void Main(string[] args)
|
|
|
{
|
|
|
Console.WriteLine("Test 1");
|
|
|
WriteLine("Test 2");
|
|
|
|
|
|
int bit = ON;
|
|
|
MyDay day = Sunday;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
* **null 조건 연산자**
|
|
|
null 값 확인 용도의 if문을 대폭 줄일 수 있다.
|
|
|
```cs
|
|
|
public class Person
|
|
|
{
|
|
|
public string Name { get; set; }
|
|
|
public int Age { get; set; }
|
|
|
}
|
|
|
|
|
|
class Program
|
|
|
{
|
|
|
static void Main(string[] args)
|
|
|
{
|
|
|
PrintObject(null);
|
|
|
}
|
|
|
|
|
|
private static void PrintObject(Object obj)
|
|
|
{
|
|
|
Person person = obj as Person;
|
|
|
Console.WriteLine(person?.Name);
|
|
|
Console.WriteLine(person?.Age);
|
|
|
//Console.WriteLine(person != null ? new int?(person.Age) : null);
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
* **문자열 내에 식을 포함**
|
|
|
```cs
|
|
|
class Person
|
... | ... | @@ -291,8 +351,48 @@ C# 언어에는 클래스가 직관적으로 배열처럼 다뤄질 수 있을 |
|
|
}
|
|
|
```
|
|
|
|
|
|
* **catch/finally 블록 내에서 await 사용 가능**
|
|
|
C#5.0의 경우 catch와 finally의 예외 처리 블록 내에서 비동기 호출(await)을 할 수 없다는 제약이 있었다.
|
|
|
* **nameof 연산자**
|
|
|
```cs
|
|
|
public class Person
|
|
|
{
|
|
|
public string Name { get; set; }
|
|
|
public int Age { get; set; }
|
|
|
|
|
|
public Person(string name, int age)
|
|
|
{
|
|
|
Name = name;
|
|
|
Age = age;
|
|
|
}
|
|
|
|
|
|
public override string ToString()
|
|
|
{
|
|
|
return $"이름 : {Name}, 나이 : {Age}";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class Program
|
|
|
{
|
|
|
static void OutputPerson(string Name, int age)
|
|
|
{
|
|
|
Console.WriteLine($"{nameof(OutputPerson)} {nameof(Name)} == {Name}, {nameof(age)} == {age}");
|
|
|
}
|
|
|
|
|
|
static void Main(string[] args)
|
|
|
{
|
|
|
OutputPerson("Cat", 67);
|
|
|
|
|
|
Person person = new Person("Dog", 1);
|
|
|
Console.WriteLine(person);
|
|
|
|
|
|
Console.WriteLine($"{nameof(Person)} 속성 : {nameof(Person.Name)}, {nameof(Person.Age)}");
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
> **실행 결과**
|
|
|
> OutputPerson `Name == Cat, age == 67`
|
|
|
이름 : Dog, 나이 : 1
|
|
|
Person 속성 : Name, Age
|
|
|
Press any key to continue . . .
|
|
|
|
|
|
* **collection 초기화 구문에 extend method로 정의한 Add 지원**
|
|
|
```cs
|
... | ... | |