... | ... | @@ -128,12 +128,61 @@ event도 간편표기법중의 하나인데, 다음 조건을 만족하는 정 |
|
|
5. event(callback)의 두 번째 인자는 해당 event에 속한 의미 있는 값이 제공된다.
|
|
|
|
|
|
|
|
|
* * **Event 사용법**
|
|
|
* **<a name = "first">Event 사용법**
|
|
|
``` cs
|
|
|
public delegate void PriceChangedDelegate(Object o, PriceEventArgs p);
|
|
|
class OddCallbackArg : EventArgs
|
|
|
{
|
|
|
public int Odd;
|
|
|
|
|
|
public OddCallbackArg(int odd)
|
|
|
{
|
|
|
this.Odd = odd;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class OddGenerator
|
|
|
{
|
|
|
public event EventHandler OddGenerated;
|
|
|
|
|
|
public void Run(int limit)
|
|
|
{
|
|
|
for (int i = 1; i <= limit; i++)
|
|
|
{
|
|
|
if (i % 2 != 0)
|
|
|
{
|
|
|
OddGenerated(this, new OddCallbackArg(i));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class Program
|
|
|
{
|
|
|
static void PrintOdd(object sender, EventArgs arg)
|
|
|
{
|
|
|
Console.Write((arg as OddCallbackArg).Odd + " ");
|
|
|
}
|
|
|
|
|
|
static int Sum;
|
|
|
|
|
|
static void SumOdd(object sender, EventArgs arg)
|
|
|
{
|
|
|
Sum += (arg as OddCallbackArg).Odd;
|
|
|
}
|
|
|
|
|
|
static void Main()
|
|
|
{
|
|
|
OddGenerator gen = new OddGenerator();
|
|
|
|
|
|
gen.OddGenerated += PrintOdd;
|
|
|
gen.OddGenerated += SumOdd;
|
|
|
|
|
|
gen.Run(20);
|
|
|
Console.WriteLine();
|
|
|
Console.WriteLine(Sum);
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
1. Object : delegate의 첫번째 parameter는 event를 발생시킨 object class를 담고 있다.
|
|
|
2. System.EvetArgs를 상속받은 클래스 : 두번째 parameter는 event가 전달해야하는 정보를 포함한 class이다.
|
|
|
|
|
|
## **3. 람다식**
|
|
|
* **람다식 용도**
|
... | ... | @@ -221,19 +270,136 @@ class Program |
|
|
> **yield return/break**
|
|
|
> yield return/break keyword를 사용하면기존의 IEnumerable, IEnumerator interface를 이용해 구현했던 열거 기능을 쉽게 구현할 수 있다.
|
|
|
|
|
|
* **
|
|
|
|
|
|
* **람다식으로 Event를 구현하기**
|
|
|
Event의 [Event 사용법](#first)예제에서 callback method 부분을 아래와 같이 lamda 식을 사용해서 구현할 수 있다.
|
|
|
```cs
|
|
|
class Program
|
|
|
{
|
|
|
static int Sum;
|
|
|
|
|
|
static void Main()
|
|
|
{
|
|
|
OddGenerator gen = new OddGenerator();
|
|
|
|
|
|
gen.OddGenerated += (sender, arg) => Console.Write((arg as OddCallbackArg).Odd + " ");
|
|
|
gen.OddGenerated += (sender, arg) => Sum += (arg as OddCallbackArg).Odd;
|
|
|
|
|
|
gen.Run(20);
|
|
|
Console.WriteLine();
|
|
|
Console.WriteLine(Sum);
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
## **4. 확장 메소드 조사**
|
|
|
일반적으로 기존 class를 확장하는 방법으로 상속이 많이 쓰인다. 하지만 sealed class나 class를 상속받아 확장하면 기존 소스코드를 새롭게 상속받은 클래스명으로 바꾸어하는 경우에 상속이 좋은 선택은 아니다.
|
|
|
일반적으로 기존 class를 확장하는 방법으로 상속이 많이 쓰인다. 하지만 sealed class나 class를 상속받아 확장하면 기존 소스코드를 새롭게 상속받은 클래스명으로 바꾸어하는 경우에 상속이 좋은 선택은 아니다. 따라서 기존 class 내부 구조를 전혀 바꾸지 않고 마치 새로운 instance method를 정의하는 것처럼 추가할 수 있는데, 이를 extension method라 한다.
|
|
|
* 확장 method는 static class에 정의되어야 함
|
|
|
* 확장 method는 반드시 static이여야 한다
|
|
|
* 확장하려는 type의 parameter를 this keyword와 함께 명시
|
|
|
* static method의 호출을 instance method를 호출하듯이 문법적으로 지원해주는 것이므로 base class의 protected member 호출이나 method override가 불가능하다
|
|
|
|
|
|
```cs
|
|
|
public static class ExtensionMethods
|
|
|
{
|
|
|
public static string UppercaseFirstLetter(this string value)
|
|
|
{
|
|
|
if (value.Length > 0)
|
|
|
{
|
|
|
char[] array = value.ToCharArray();
|
|
|
array[0] = char.ToUpper(array[0]);
|
|
|
return new string(array);
|
|
|
}
|
|
|
return value;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class Program
|
|
|
{
|
|
|
static void Main()
|
|
|
{
|
|
|
string value = "dot net perls";
|
|
|
value = value.UppercaseFirstLetter();
|
|
|
Console.WriteLine(value);
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
**1. FirstOrDefault()**
|
|
|
```cs
|
|
|
public static TSource FirstOrDefault<TSource>(
|
|
|
this IEnumerable<TSource> source
|
|
|
)
|
|
|
```
|
|
|
기본적으로 sequence의 first element를 반환하고 element가 없을 경우 default value를 반환한다.
|
|
|
```cs
|
|
|
List<int> months = new List<int> { };
|
|
|
|
|
|
int firstMonth = months.FirstOrDefault();
|
|
|
Console.WriteLine("The value of the firstMonth variable is {0}", firstMonth);
|
|
|
|
|
|
FirstOrDefault()
|
|
|
SingleOrDefault()
|
|
|
Where()
|
|
|
Select()
|
|
|
** overload 버전 전부다 조사할 필요는 없습니다. 위 메소드의 역할과 간단한 샘플만 조사해주세요.
|
|
|
months.Add(12);
|
|
|
months.Add(1);
|
|
|
|
|
|
firstMonth = months.FirstOrDefault();
|
|
|
Console.WriteLine("The value of the firstMonth variable is {0}", firstMonth);
|
|
|
```
|
|
|
|
|
|
**2. SingleOrDefault()**
|
|
|
```cs
|
|
|
public static TSource SingleOrDefault<TSource>(
|
|
|
this IEnumerable<TSource> source
|
|
|
)
|
|
|
```
|
|
|
element가 1개라는 전제하에 사용되며 그 element를 반환한다. element가 없을 경우 default value를 반환한다(sequence에 하나 이상의 element가 있을 경우 exception을 발생시킨다).
|
|
|
```cs
|
|
|
int[] pageNumbers = { 2, };
|
|
|
|
|
|
// Setting the default value to 1 after the query.
|
|
|
int pageNumber1 = pageNumbers.SingleOrDefault();
|
|
|
Console.WriteLine("The value of the pageNumber1 variable is {0}", pageNumber1);
|
|
|
|
|
|
Array.Clear(pageNumbers, 0, 1);
|
|
|
|
|
|
pageNumber1 = pageNumbers.SingleOrDefault();
|
|
|
Console.WriteLine("The value of the pageNumber1 variable is {0}", pageNumber1);
|
|
|
```
|
|
|
|
|
|
**3. Where()**
|
|
|
```cs
|
|
|
public static IEnumerable<TSource> Where<TSource>(
|
|
|
this IEnumerable<TSource> source,
|
|
|
Func<TSource, bool> predicate
|
|
|
)
|
|
|
```
|
|
|
predicate 값에 따라 value들의 sequence를 filtering한다.
|
|
|
|
|
|
```cs
|
|
|
List<string> fruits = new List<string> { "apple", "passionfruit", "banana", "mango", "orange", "blueberry", "grape", "strawberry" };
|
|
|
|
|
|
IEnumerable<string> query = fruits.Where(fruit => fruit.Length < 6);
|
|
|
foreach (string fruit in query)
|
|
|
{
|
|
|
Console.WriteLine(fruit);
|
|
|
}
|
|
|
```
|
|
|
|
|
|
**4. Select()**
|
|
|
```cs
|
|
|
public static IEnumerable<TResult> Select<TSource, TResult>(
|
|
|
this IEnumerable<TSource> source,
|
|
|
Func<TSource, TResult> selector
|
|
|
)
|
|
|
```
|
|
|
sequence의 각각 element를 새로운 형태로 만든다.
|
|
|
|
|
|
```cs
|
|
|
List<string> fruits = new List<string> { "apple", "passionfruit", "banana", "mango", "orange", "blueberry", "grape", "strawberry" };
|
|
|
|
|
|
IEnumerable<int> query = fruits.Select(fruit => fruit.Length);
|
|
|
foreach (int fruit in query)
|
|
|
{
|
|
|
Console.WriteLine(fruit);
|
|
|
}
|
|
|
```
|
|
|
## 5. **LINQ 기본 문법 정리**
|
|
|
|
|
|
**1. where**
|
... | ... | |