기본 콘텐츠로 건너뛰기

[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#] 연산자 목록(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