C#의 Null 병합 연산자

C#에서는 ?? 운영자 Null 병합 연산자로 알려져 있습니다. null이 아닌 경우 왼쪽 피연산자의 값을 반환합니다. null이면 오른쪽 피연산자를 평가하고 그 결과를 반환합니다. 또는 왼쪽 피연산자가 null이 아닌 것으로 평가되면 오른쪽 피연산자를 평가하지 않습니다.

통사론:

p ?? q 

여기서 p는 ??의 왼쪽, q는 오른쪽 피연산자이다. 운영자. p의 값은 null을 허용하는 유형일 수 있지만 q의 값은 null을 허용하지 않는 유형이어야 합니다. p 값이 null이면 q 값을 반환합니다. 그렇지 않으면 p 값을 반환합니다.

중요한 사항:

  • ?? 연산자는 null 값을 확인하는 데 사용되며 값이 null(또는 nullable 유형)인 변수에 기본값을 할당할 수도 있습니다.
  • 과부하가 허용되지 않습니까 ?? 운영자.
  • 오른쪽 연관입니다.
  • 안에 ?? 연산자의 경우 throw 표현식을 ??의 오른쪽 피연산자로 사용할 수 있습니다. 코드를 더욱 간결하게 만드는 연산자입니다.
  • 당신은 ??를 사용할 수 있습니다 값 유형과 참조 유형이 있는 연산자.

    예:




    // C# program to illustrate how to use> // ?? operator with value types and> // reference types> using> System;> > namespace> example {> > class> Program {> > static> void> Main(> string> [] args)> > {> > > // Reference types> > string> item_1 => null> ;> > string> item_2 => 'techcodeview.com'> ;> > string> item_3 => 'GFG'> ;> > > string> item_4 = item_1 ?? item_2;> > item_3 = item_4 ?? item_2;> > > Console.WriteLine(> 'Value of item_4 is: {0} '> +> > 'Value of item_3 is: {1}'> , item_4, item_3);> > > // Value types> > int> ? item_5 => null> ;> > > Program obj => new> Program();> > > // Using ?? operator assigns> > // the value of a value type> > // and also you are allowed> > // to use method with ?? operator> > int> ? item_6 = item_5 ?? obj.Add(10, 30);> > Console.WriteLine(> 'Value of item_6 is: {0}'> , item_6);> > }> > > // Method> > public> int> Add(> int> a,> int> b)> > {> > int> result = a + b;> > return> result;> > }> }> }>

    산출:

     Value of item_4 is: techcodeview.com Value of item_3 is: techcodeview.com Value of item_6 is: 40 
  • 의 도움으로 ?? 운영자는 예방할 수 있습니다 InvalidOperationException .

    예:




    // C# program to illustrate how ??> // operator prevent the> // InvalidOperationException> using> System;> > namespace> example {> > class> GFG {> > > // Main Method> > static> void> Main(> string> [] args)> > {> > // Creating items of nullable types> > int> ? item_1 => null> ;> > > /*> > Here if you use this commented part,> > then this statement will give you an> > InvalidOperationException. So to> > overcome this problem we use ?? operator> > int? item_2 = item_1.Value;> > */> > > // With the help of ?? operator we> > // assign a default value to the item_2> > // And the value of item_1 is null.> > int> ? item_2 = item_1 ?? 100;> > Console.WriteLine(> 'Value of item_1 is: {0}'> , item_1);> > Console.WriteLine(> 'Value of item_2 is: {0}'> , item_2);> > }> }> }>

    산출:

     Value of item_1 is: Value of item_2 is: 100 
  • 의 도움으로 ?? 연산자를 사용하면 중복된 if-else 조건을 많이 제거하고 코드를 간결하고 읽기 쉽게 만들 수 있습니다.

    예:




    // C# program to illustrate how ??> // operator replaces if-else statements> using> System;> > namespace> example {> > class> GFG {> > > // Main Method> > static> void> Main(> string> [] args)> > {> > // Creating items of nullable types> > int> ? item_1 => null> ;> > > int> ? item_2;> > > if> (item_1.HasValue) {> > item_2 = item_1;> > }> > else> {> > item_2 = 200;> > }> > Console.WriteLine(> 'Value of item_1 is: {0}'> , item_1);> > Console.WriteLine(> 'Value of item_2 is: {0}'> , item_2);> > }> }> }>

    산출:

     Value of item_1 is: Value of item_2 is: 200 




    // C# program to illustrate how ??> // operator replaces if-else statements> using> System;> > namespace> example {> > class> GFG {> > > // Main Method> > static> void> Main(> string> [] args)> > {> > // Creating items of nullable types> > int> ? item_1 => null> ;> > > // Using ?? operator> > int> ? item_2 = item_1 ?? 200;> > Console.WriteLine(> 'Value of item_1 is: {0}'> , item_1);> > Console.WriteLine(> 'Value of item_2 is: {0}'> , item_2);> > }> }> }>

    산출:

     Value of item_1 is: Value of item_2 is: 200 
  • ?? 연산자는 중첩될 수 있습니다. 이렇게 하면 코드를 더 읽기 쉽게 만들고 여러 if-else 조건도 줄일 수 있습니다.

    예:




    // C# program to illustrate how> // we use nested ?? operator> using> System;> > namespace> example {> > class> GFG {> > > // Main Method> > static> void> Main(> string> [] args)> > {> > // Creating items of nullable types> > int> ? item_1 => null> ;> > int> ? item_2 => null> ;> > int> ? item_3 = 500;> > > // Nested ?? operator> > int> ? item_4 = item_1 ?? item_2 ?? item_3;> > > Console.WriteLine(> 'Value of item_4 is: {0} '> , item_4);> > }> }> }>

    산출:

    Value of item_4 is: 500