728x90
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable?view=net-7.0
List ๋ IEnumerable ์ธํฐํ์ด์ค๋ฅผ ๋ฐ๊ณ ์๋ค. IEnumerable์ Enumerator๋ฅผ ๊ฐ๊ณ ์๋ค. ๋ฐ๋ณต์์ ์์๋๋ก ํ๋์ฉ ๊ฐ์ ๊ฐ์ ธ์ค๊ณ ์ถ์ ๋ IEnumerable ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํ๋ค.
*IEnumerable: Enumerator์๊ฒ ๋ค์ ๊ฐ์ฒด๋ฅผ ๋ฐ์์ ํ๋์ฉ ๋๊ฒจ์ฃผ๋ '์ญํ '์ ํจ.
*Enumerator: ๋ช๋ฒ์งธ๊น์ง ์ฝ์๋์ง์ ๋ํ state ๊ฐ์ ์ ์ฅํจ. MoveNext(), Current() ํจ์ ๋ฐ property๋ฅผ ๊ฐ๊ณ ์์.
- MoveNext() ํจ์๋ฅผ ํธ์ถ๋ฐ์ผ๋ฉด ๋ค์ ์๋ฒ์ผ๋ก ์ด๋.
- Current๋ฅผ ์๊ตฌํ ๋ ํด๋น ์๋ฒ์ ๊ฐ์ฒด๋ฅผ ๋ฆฌํดํจ.
* Where
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);
}
/*
This code produces the following output:
apple
mango
grape
*/
* ToList
string[] fruits = { "apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "strawberry" };
List<int> lengths = fruits.Select(fruit => fruit.Length).ToList();
foreach (int length in lengths)
{
Console.WriteLine(length);
}
/*
This code produces the following output:
5
12
6
5
6
9
5
10
*/
* Max
double?[] doubles = { null, 1.5E+104, 9E+103, -2E+103 };
double? max = doubles.Max();
Console.WriteLine("The largest number is {0}.", max);
/*
This code produces the following output:
The largest number is 1.5E+104.
*/
* Select
string[] fruits = { "apple", "banana", "mango", "orange",
"passionfruit", "grape" };
var query =
fruits.Select((fruit, index) =>
new { index, str = fruit.Substring(0, index) });
foreach (var obj in query)
{
Console.WriteLine("{0}", obj);
}
/*
This code produces the following output:
{index=0, str=}
{index=1, str=b}
{index=2, str=ma}
{index=3, str=ora}
{index=4, str=pass}
{index=5, str=grape}
*/
...
728x90
'๐ฎ Unity Study > C#' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C#] ๋๊ธฐ/๋น๋๊ธฐ (feat. Coroutine, Task, async/await) (0) | 2023.08.16 |
---|---|
[C#] Action ๊ณผ Func (feat. Delegate, callback) (0) | 2023.08.16 |
C# Code Convention (0) | 2022.10.13 |
[KLA ๋ฉํฐ์บ ํผ์ค] C#7.0 ์ ๋๋ก ๋ฐฐ์ฐ๊ธฐ Part.2(์ ๋ฌธ2) (0) | 2022.08.04 |
[KLA ๋ฉํฐ์บ ํผ์ค] C#7.0 ์ ๋๋ก ๋ฐฐ์ฐ๊ธฐ Part.1(์ ๋ฌธ1) (0) | 2022.07.14 |