C# Apps => 연산자(C# 참조)

2017.06.25 15:26

졸리운_곰 조회 수:191

=> 연산자(C# 참조)

2017-05-24 1 읽는 시간(분) 참가자 

  • Bill Wagner
  •  
  • OpenLocalizationService
  •  
  • Saisang

=> 토큰을 람다 연산자라고 합니다. 이 연산자는 람다 식에서 왼쪽의 입력 변수를 오른쪽의 람다 본문과 구분하는 데 사용됩니다. 람다 식은 무명 메서드와 유사한 인라인 식이지만 보다 유연하며, 메서드 구문으로 표현되는 LINQ 쿼리에서 광범위하게 사용됩니다. 자세한 내용은 람다 식을 참조하세요.

다음 예제에서는 문자열 배열에서 가장 짧은 문자열 길이를 찾아 표시하는 두 가지 방법을 보여 줍니다. 예제의 첫 번째 부분에서는 람다 식(w => w.Length)을 words 배열의 각 요소에 적용한 다음 <xref:System.Linq.Enumerable.Min%2A> 메서드를 사용하여 가장 작은 길이를 찾습니다. 비교를 위해 예제의 두 번째 부분에서는 쿼리 구문을 사용하여 동일한 작업을 수행하는 더 긴 솔루션을 보여 줍니다.

C#

string[] words = { "cherry", "apple", "blueberry" };  

// Use method syntax to apply a lambda expression to each element  
// of the words array.   
int shortestWordLength = words.Min(w => w.Length);  
Console.WriteLine(shortestWordLength);  

// Compare the following code that uses query syntax.  
// Get the lengths of each word in the words array.  
var query = from w in words  
            select w.Length;  
// Apply the Min method to execute the query and get the shortest length.  
int shortestWordLength2 = query.Min();  
Console.WriteLine(shortestWordLength2);  

// Output:   
// 5  
// 5  

설명

=> 연산자는 할당 연산자(=)와 우선 순위가 같으며 오른쪽 결합성이 있습니다.

입력 변수의 형식을 명시적으로 지정하거나 컴파일러에서 유추하도록 할 수 있습니다. 두 경우 모두 변수는 컴파일 시간에 강력한 형식이어야 합니다. 다음 예제와 같이 형식을 지정할 때 형식 이름과 변수 이름을 괄호로 묶어야 합니다.

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

C#

int shortestWordLength = words.Min((string w) => w.Length);  

예제

다음 예제에서는 두 개의 인수를 사용하는 표준 쿼리 연산자 <xref:System.Linq.Enumerable.Where%2A?displayProperty=fullName>의 오버로드에 대한 람다 식을 작성하는 방법을 보여 줍니다. 람다 식에는 둘 이상의 매개 변수가 사용되므로 매개 변수를 괄호로 묶어야 합니다. 두 번째 매개 변수 index는 컬렉션에서 현재 요소의 인덱스를 나타냅니다. Where 식은 길이가 배열의 해당 인덱스 위치보다 작은 모든 문자열을 반환합니다.

C#

static void Main(string[] args)  
{  
    string[] digits = { "zero", "one", "two", "three", "four", "five",   
            "six", "seven", "eight", "nine" };  

    Console.WriteLine("Example that uses a lambda expression:");  
    var shortDigits = digits.Where((digit, index) => digit.Length < index);  
    foreach (var sD in shortDigits)  
    {  
        Console.WriteLine(sD);  
    }  

    // Output:  
    // Example that uses a lambda expression:  
    // five  
    // six  
    // seven  
    // eight  
    // nine  
}  

 

[출처] https://docs.microsoft.com/ko-kr/dotnet/csharp/language-reference/operators/lambda-operator

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
44 Machine Learning with ML.Net and C#/VB.Net file 졸리운_곰 2018.08.16 997
43 Build Simple AI .NET Library - Part 6 - ML Algorithms file 졸리운_곰 2018.03.26 1102
42 Build Simple AI .NET Library - Part 5 - Artificial Neural Networks file 졸리운_곰 2018.03.26 1046
41 Build Simple AI .NET Library - Part 4 - Beyond Perceptron file 졸리운_곰 2018.03.26 293
40 Build Simple AI .NET Library - Part 3 - Perceptron file 졸리운_곰 2018.03.26 238
39 Build Simple AI .NET Library - Part 2 - Machine Learning Introduction file 졸리운_곰 2018.03.26 309
38 Build Simple AI .NET Library - Part 1 - Basics First file 졸리운_곰 2018.03.26 293
37 텍스트 파일을 한 번에 한 줄씩 읽기(Visual C#) 졸리운_곰 2017.08.30 354
36 마우스 클릭 매크로 (C# 버전) file 졸리운_곰 2017.07.30 1401
35 Assembly Manipulation and C# / VB.NET Code Injection file 졸리운_곰 2017.07.01 419
34 Snoop is the open source WPF spying utility file 졸리운_곰 2017.07.01 263
33 Hawkeye - The .Net Runtime Object Editor Hawkeye - The .Net Runtime Object Editor file 졸리운_곰 2017.07.01 151
32 .NET Object Spy and InvokeRemote file 졸리운_곰 2017.07.01 227
31 C#에서의 리플렉션(Reflection) 졸리운_곰 2017.07.01 340
30 Loading an assembly using Reflection and invoking static methods from it file 졸리운_곰 2017.07.01 154
» => 연산자(C# 참조) file 졸리운_곰 2017.06.25 191
28 대리자 사용(C# 프로그래밍 가이드) 졸리운_곰 2017.06.25 248
27 Using C# reflection to call a constructor 졸리운_곰 2017.06.25 175
26 Reflection introduction 졸리운_곰 2017.06.25 147
25 C# - Interfaces 졸리운_곰 2017.06.25 168
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED