기본 콘텐츠로 건너뛰기

[C#] 간결한 식(expression)

[ 기본 간결식 ] member => expression;
(example 1 : 메서드)


using System;

public class Person
{
    public Person(string firstName, string lastName)
    {
        fname = firstName;
        lname = lastName;
    }

    private string fname;
    private string lname;

    public override string ToString() => $"{fname} {lname}".Trim();

public void DisplayName() => Console.WriteLine(ToString());

}


class Example
{
    static void Main()
    {
        Person p = new Person("Mandy", "Dejesus");
        Console.WriteLine(p);
        p.DisplayName();

    }
}


(example 2 : 생성자)

public class Location
{
   private string locationName;
   public Location(string name) => Name = name;
   public string Name
   {
      get => locationName;
      set => locationName = value;
   }
}

(example 3 : 종료자) using System; public class Destroyer {    public override string ToString() => GetType().Name;    ~Destroyer() => Console.WriteLine($"The {ToString()} destructor is executing."); } (example 4 : 속성가져오기 문)
public class Location
{
   private string locationName;  

   public Location(string name) => Name = name;

   public string Name
   {
      get => locationName;
      set => locationName = value;
   }
}


//(example 5 : 인덱서)


using System;
using System.Collections.Generic;


public class Sports
{
   private string[] types = { "Baseball", "Basketball", "Football",
                              "Hockey", "Soccer", "Tennis",
                              "Volleyball" };

   public string this[int i]
   {
      get => types[i];
      set => types[i] = value;
   }
}

by Microsoft.MSDN C# Guide Docs

이 블로그의 인기 게시물

[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#][Network][Thread] C#에서 쓰레드를 이용한 에코서버의 기본구조

// Thread Netwotk Server Exam namespace threadNetworkExam {     public partial class Form1 : Form     {         // TCP 소켓통신 서버구현을 위해 TCP리스너 정의/초기화         private TcpListener tcpListner = null;                 public Form1()         {             InitializeComponent();         }         // Thread Method         // Client 접속시 스레드가 호출하여 작업할 메소드 정의         private void AcceptClient()         {             while (true)             {                 // Client 소켓통신을 위해 수신을 대기하는 TcpClient 생성                 // TcpListner를 통해 생성                 TcpC...