Visual Studio 2008에서는 Targeting을 정해줄 수 있다 : .NET Framework의 종류를 지정해서 compile을 시킬 수 있다.
1. Auto-implemented property
- Property에서 간단히 값을 저장하는 형식이라면 private 변수들을 선언하거나 property의 구현을 하지 않더라도, 구현이 가능하다.
- Code가 매우 간단해지고, 간단한 수행에서의 가독성을 높여준다.
- 기존의 get/set에서의 특별한 행동을 해주는 경우에는 전과 같이 안의 코드를 만들어줘야지 된다.
class AutoImplementedClass { public string Name { get; set; } public string Customer { get; private set; } }
2. Object / Collection Initializer
- Class의 선언과 동시에 private 변수를 초기화 시키는 것이 가능하다.
- Object / Collection Initialize에서는 {}를 사용하는 것을 유념할것.
- Public 변수들만 된다.
List<int> powers = new List<int>{ 1, 2, 3, 4, 5, 6, 7 }; class Point { public int x, y; } Point point = new Point{x = 0, y = 1};
3. Local Variable Type Inference
- Type 명을 적지 않더라도, compiler에서 선언 대치해줄수 있다.
- 'var' keyword를 이용해서 사용가능하다.
- 주의할 점은 var에서 type을 변화시킬 수 없는 경우에서는 에러가 나타난다.
- Type이 동적으로 되는 것이 아니라, 초기화 될때 Type은 결정되어 고정된다.
4. Anonymous Type
- 익명 Type Class
- 프로그램에서 참조할 수 없다.
- 복잡한 임시 테이블에 사용하는 것이 좋다.
static void Main(string[] args) { var x = new {a =3 , b = 5, c ="some text"} assert.AreEqual(3, x.a) assert.AreEqual(5, x.b) assert.AreEqual("some text", x.c) }
5. Lamda Expression
- Delegate와 매우 연관이 높다.
- Lamda expression에서 input/output에서 type을 명확히 넣어주고 싶은 경우에는 type을 결정해서 넣어주면 된다.
- Func<T> : return 값이 존재하는 경우에 기본적으로 사용되는 delegate type (new delegate로 만들어줘도 상관이 없지만, 기왕에 있는 녀석이면 사용하자.)
- Action<T> : return 값이 존재하지 않는 경우에 사용되는 delegate type (new delegate로 만들어줘도 상관이 없지만, 기왕에 있는 녀석이면 사용하자.)
SomeDelegate d3 = s => s.ToUpper(); SomeDelegate d4 = (string s) => {return s.ToUpper()} string a = d3("abcde")
6. Extension Methods
- 기존 Class에 새로운 기능을 추가한다.
- 상속을 받지 않고도 가능하다.
- 실행 우선 순위가 Instance method보다 낮다.
- "this string s" 라는 구문으로 string class에 대한 확장 class를 구현 가능하다.
- 특정한 API를 만드는 것은 좋지만, 가독성에 심각한 문제를 초래할 수도 있다.
public static class Extensions { public static int ToInt32(this string s) { return 0; } }
7. Partial Methods
- Prital Method는 Partial Class안에 선언되어야지 된다.
- 반드시 Return type은 void가 되어야지 된다.
- 반드시 private 한정자에서만 가능하다.
- C++에서의 함수 선언을 header에서 하는 것과 비슷하게 생각하면 쉽다.
- 구현부가 없는 경우에는 compiler가 연관된 모든 code를 삭제할 수 있다.
8. Query Expressions [LINQ]
- var = contacts = from c in customers where c.City == "London" SELECT new {c.Customer.ID, c.City}
- Where 문을 Extension Method를 이용해서 만들어주고, Lamda Expression을 이용해서 내부적으로 code를 작성시켜주게 된다.
class Customer { public string CustomerID { get; set; } public string ContactName { get; set; } public string City { get; set; } } public class Program { static void Main(string[] args) { List<Customers> customers = LoadCustomers(); var query = from c in customers where c.City == "London" select (new {c.CustomerID, c.ContactName} ); foreach (var item in query) { Console.WriteLine(item); } } }