Files
freeCodeCamp/guide/russian/csharp/null-coalescing-operator/index.md
Randell Dawson 0a1eeea424 fix(guide) Replace invalid prism code block names (#35961)
* fix: replace sh with shell

fix replace terminal with shell

fix replace node with js

fix replace output with shell

fix replace cs with csharp

fix replace c++ with cpp

fix replace c# with csharp

fix replace javasctipt with js

fix replace syntax  with js

fix replace unix with shell

fix replace linux with shell

fix replace java 8 with java

fix replace swift4 with swift

fix replace react.js with jsx

fix replace javascriot with js

fix replace javacsript with js

fix replace c++ -  with cpp

fix: corrected various typos

fix: replace Algorithm with nothing

fix: replace xaml with xml

fix: replace solidity with nothing

fix: replace c++ with cpp

fix: replace txt with shell

fix: replace code with json and css

fix: replace console with shell
2019-05-15 19:08:19 +02:00

76 lines
2.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Null-coalescing Operator
localeTitle: Оператор Null-coalescing
---
# Оператор Null-coalescing
Оператор null-coalescing в C # используется, чтобы помочь назначить одну переменную другому и указать альтернативное значение, если исходное значение равно `null` . Оператор нулевой коалесценции в C # равен `??` ,
## Пример 1
Поскольку `name` равно `null` , `clientName` будет присвоено значение «John Doe».
```csharp
string name = null;
string clientName = name ?? "John Doe";
Console.WriteLine(clientName);
```
```csharp
> John Doe
```
## Пример 2.
Поскольку `name` не равно `null` , `clientName` будет присвоено значение `name` , которое является «Jane Smith».
```csharp
string name = "Jane Smith";
string clientName = name ?? "John Doe";
Console.WriteLine(clientName);
```
```csharp
> Jane Smith
```
## Альтернатива if ... else Statement
Вы можете использовать оператор `if...else` для проверки наличия `null` и назначения другого значения.
```csharp
string clientName;
if (name != null)
clientName = name;
else
clientName = "John Doe";
```
Однако это может быть значительно упрощено с помощью оператора нулевой коалесценции.
```csharp
string clientName = name ?? "John Doe";
```
## Альтернатива условному (тройному) оператору
Также можно использовать условный оператор для проверки наличия `null` и присвоения другого значения.
```csharp
string clientName = name != null ? name : "John Doe";
```
Опять же, это можно упростить с помощью оператора нуль-коалесценции.
```csharp
string clientName = name ?? "John Doe";
```
## Рекомендации
* [?? Оператор (ссылка C #)](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-conditional-operator)