기본 콘텐츠로 건너뛰기

[C#] C# 대리자(delegate)의 발전


C# 1.0에서는 코드의 다른 위치에 정의된 메서드를 사용하여 명시적으로 초기화하는 방식으로 대리자의 인스턴스를 만들었습니다. C# 2.0에서는 대리자 호출에서 실행될 수 있는 이름 없는 인라인 문 블록을 작성하는 방법으로 무명 메서드의 개념을 소개했습니다. C# 3.0에서는 개념적으로 무명 메서드와 비슷하지만 더 간결하고 표현이 다양한 람다 식을 소개했습니다. 이러한 두 기능을 함께 익명 함수라고 합니다. 일반적으로 .NET Framework의 버전 3.5 이상을 대상으로 하는 애플리케이션은 람다 식을 사용해야 합니다.

다음 예제에서는 C# 1.0에서 C# 3.0까지 대리자 만들기의 발전을 보여 줍니다

class Test
{
    delegate void TestDelegate(string s);
    static void M(string s)
    {
        Console.WriteLine(s);
    }

    static void Main(string[] args)
    {
        // Original delegate syntax required
        // initialization with a named method.
        TestDelegate testDelA = new TestDelegate(M);

        // C# 2.0: A delegate can be initialized with
        // inline code, called an "anonymous method." This
        // method takes a string as an input parameter.
        TestDelegate testDelB = delegate(string s) { Console.WriteLine(s); };

        // C# 3.0. A delegate can be initialized with
        // a lambda expression. The lambda also takes a string
        // as an input parameter (x). The type of x is inferred by the compiler.
        TestDelegate testDelC = (x) => { Console.WriteLine(x); };

        // Invoke the delegates.
        testDelA("Hello. My name is M and I write lines.");
        testDelB("That's nothing. I'm anonymous and ");
        testDelC("I'm a famous author.");

        // Keep console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
/* Output:
    Hello. My name is M and I write lines.
    That's nothing. I'm anonymous and
    I'm a famous author.
    Press any key to exit.
 */

이 블로그의 인기 게시물

[C#] 연산자 목록(Operators)

[C#] C# 프로그램의 일반적인 구조(A skeleton of a C# program)

// A skeleton of a C# program  using System; namespace YourNamespace {     class YourClass     {     }     struct YourStruct     {     }     interface IYourInterface      {     }     delegate int YourDelegate();     enum YourEnum      {     }     namespace YourNestedNamespace     {         struct YourStruct          {         }     }     class YourMainClass     {         static void Main(string[] args)          {             //Your program starts here...         }     } } By Microsoft.MSDN :: .NET Guide Docs

[C#] .NET Configuration file :: App.config

프로그램의 옵션들을 담아 두는 파일로서 아주 예전에는 .INI 파일을 사용하였었으며, 이후 윈도우즈가 관리하는 레지스트리 데이타베이스에 시스템 및 응용프로그램의 옵션들을 저장하였다. 레지스트리는 프로그램의 옵션을 저장하는 훌륭한 저장 장소이나, 모든 응용프로그램이 항상 레지스트리에 데이타를 쓰는 권한을 갖는 것은 아니였기 때문에 Permission 제약점이 있었다. .NET Framework에서는 프로그램의 행위를 결정짓는 옵션들을 저장하기 위해 .Config 파일을 사용한다. 데스크탑 응용프로그램 .EXE 에 대해서 Configuration 파일(구성파일)은 .EXE.Config이 되는데, 예를 들어, TEST.EXE의 구성 파일은 TEST.EXE.Config가 된다. Visual Studio에서 콘솔, 윈폼, 혹은 WPF 등의  데스크탑 프로젝트를 생성한 후 App.config 파일을 추가하고(아래 그림처럼 Application Configuration File 추가) 여기에 필요한 옵션들을 설정한 후 빌드하면 .EXE 파일에 대한 .EXE.Config 파일이 자동으로 생성 된다. 그리고 이렇게 추가한 App.config 파일의 내용을 소스코드에서 사용하기위해서는 "참조"에서 '참조 추가하기'를 통해 반드시 에셈블리의 "System.Configurarion"을 추가하고 사용 할 소스코드에 using 문으로 추가해 주어야 한다.  " using System.Configuration; " <예제> [App.config] <?xml version="1.0" encoding="utf-8"?> <configuration>   <startup>     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1...