2010년 이후 도입된 C# 기능들 몇가지

Look! there's a flower!·2024년 8월 23일
  1. Async/Await : 태스크를 기다리기 위해 별도 Thread 사용하지 않아도 됨
  async Task<string> GetDataAsync()
  {
      var result = await httpClient.GetStringAsync(url);
      return result;
  }
  1. Null-conditional operator (?.) : null check 안해도 됨

    string name = person?.Name;
  2. Expression-bodied member : dynamic한 readonly 변수

  public string FullName => $"{FirstName} {LastName}";
  1. is expression
  if (obj is int i)
  {
      Console.WriteLine($"It's an integer: {i}");
  }
  1. Tuples
   (string Name, int Age) person = ("Alice", 30);    
  1. Inside function (lamda function)
  void OuterFunction()
  {
      int InnerFunction(int x) => x * x;
      var result = InnerFunction(5);
  }
  1. 간단한 switch
  string GetSound(Animal animal) => animal switch
  {
      Dog => "Woof",
      Cat => "Meow",
      _ => "Unknown"
  };
  1. Nullable reference type
  string? nullableString = null;
  1. Record : 순서가 없는 named structure
  public record Person(string FirstName, string LastName);
  1. init-only 설정 property
  public string Name { get; init; }
profile
Why don't you take a look around for a moment?

0개의 댓글