기본 콘텐츠로 건너뛰기

[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#][DB][ADO.NET] SqlCommand

public partial class Form1 : Form     {         string strConn = "server=.\\SQLEXPRESS;database=example;uid=sa;pwd=12345";         SqlConnection Conn;         SqlDataAdapter dataAdapter;         DataSet dataSet;         public Form1()         {             InitializeComponent();         }         private void SqlCommandInit()         {             string strSelectSQL = "SELECT * FROM tblEx01";             dataAdapter.SelectCommand = new SqlCommand(strSelectSQL, Conn);                      string strInsertCommand = "INSERT INTO tblEx01 (name, age) VALUES (@name, @age)";             dataAdapter.InsertCommand = ne...

Visual Studio 단축키

문자 검색/치환 Ctrl + F  -> 검색 Ctrl + H  -> 치환 자동 들여쓰기(indent) Ctrl + K, Ctrl + D  -> 파일전체 Ctrl + K, Ctrl + F  -> 선택영역 코드개요 확장/축소 Ctrl + M, Ctrl + L  -> 파일전체 확장/축소 Ctrl + M, Ctrl + M  -> 선택영역 확장/축소 주석처리 Ctrl + K, Ctrl + C  -> 선택영역 주석화 Ctrl + K, Ctrl + U  -> 선택영역 주석 취소 함수정의로 이동 F12 커서 위치 이력 기록 Ctrl + - (마이너스)  -> 직전 커서위치로 이동 Ctrl + Shift + - (마이너스)  -> 다음 커서위치로 이동