Co to jest "??"operator? [duplikat]

To pytanie ma już odpowiedź tutaj:

Zastanawiałem się nad ?? znakami w C# kodzie. Do czego to służy? I jak mogę go użyć?

A co z int?? Czy to jest nullable int?

Zobacz też:

?? Operator koalescencyjny Null - > co robi coalescing mean?

Author: Community, 2009-05-06

12 answers

Nazywa się to "operatorem koalescencji zerowej" i działa mniej więcej tak:

Zamiast robić:

int? number = null;
int result = number == null ? 0 : number;

Możesz teraz po prostu zrobić:

int result = number ?? 0;
 33
Author: BFree,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-05-05 23:57:36

To Operator koalescencji zerowej. Został wprowadzony w C # 2.

Wynikiem wyrażenia a ?? b jest a Jeśli nie jest to null, lub b w przeciwnym razie. b nie jest oceniana, chyba że jest potrzebna.

Dwie miłe rzeczy:

  • Ogólny typ wyrażenia jest typem drugiego operanda, co jest ważne, gdy używasz typów wartości nullable:

    int? maybe = ...;
    int definitely = maybe ?? 10;
    

    (zauważ, że nie można użyć typu wartości nie-nullable jako pierwszego operandu - byłoby bez sensu.)

  • Reguły asocjacji oznaczają, że możesz to łatwo połączyć. Na przykład:

    string address = shippingAddress ?? billingAddress ?? contactAddress;
    

, który użyje pierwszej, niezerowej wartości z adresu wysyłki, faktury lub adresu kontaktowego.

 51
Author: Jon Skeet,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-19 08:31:03

To jest operator koalesce. Zasadniczo jest to skrót od następującego

x ?? new Student();
x != null ? x : new Student();

Dokumentacja MSDN o operatorze

 12
Author: JaredPar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-05-05 23:56:51

To nowy operator null Coalesce.

The ?? operator zwraca lewy operand, jeśli nie jest null, lub zwraca prawy operand.

Możesz przeczytać o tym tutaj: http://msdn.microsoft.com/en-us/library/ms173224 (VS.80). aspx

 8
Author: Scott Ferguson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-05-05 23:57:09

Jest to skrót od:

Text = (category == null ? "Home" : category);
 8
Author: Konamiman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-19 08:13:44

To operator koalesce. zwróci inną wartość, jeśli pierwsza wartość to null

string value1 = null;
string value2 = "other";

string value3 = value1 ?? value2; // assigns "other" to value 3
 7
Author: Darren Kopp,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-05-06 00:03:54

Sprawdza, czy kategoria ma wartość null - w takim przypadku wartość null jest zastępowana przez "Home".

 5
Author: tanascius,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-19 08:09:29

Jednym z moich ulubionych zastosowań dla operatora koalescencyjnego null jest unikanie poleceń if W moim kodzie (myślę, że poleceń if są brzydkie i po prostu bałagan rzeczy w większości razy). Na przykład, weźmy typowy scenariusz, w którym można wybrać załadowanie czegoś z pamięci podręcznej, jeśli jest dostępna, w przeciwnym razie załaduj z db i zapełnij pamięć podręczną.

private SomeData GetData() {
    var data = HttpRuntime.Cache.Get("key") as SomeData;

    if (data == null) {
        data = DAL.GetData(some parameters...);
        HttpRuntime.Cache.Add("key", data, ....);
    }

    return data;
}
Dla mnie to brzydki kod. Może i jestem trochę Analny, ale dlaczego nie refakturować tego zamiast tego?
private SomeData GetDataAndCache() {
    var data = DAL.GetData(some parameters...);
    HttpRuntime.Cache.Add("key", data, ....);
    return data;
}

private SomeData GetData() {
    var data = HttpRuntime.Cache.Get("key") as SomeData;
    return data ?? GetDataAndCache();
}
[[2]}jest bardziej zgodna z SRP i jest czystsza i łatwiejsze do odczytania, IMO. Funkcje spełniają dokładnie jedną wyraźnie rozpoznawalną funkcję.
 4
Author: Chris,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-05-06 00:25:55

Jeśli kategoria jest null, tekst stanie się"Home"

 4
Author: brysseldorf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-19 08:09:18

Zwraca pierwszą wartość not-null. Poręczne.

 2
Author: marcc,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-05-05 23:57:51

?? Null-Operator Koalescencyjny

Int? jest nullable int, co oznacza, że może mieć wartości normalnej int I null. Przeczytaj to Po szczegóły.

 2
Author: TobiasBohnen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-05-06 00:20:30

To null-Operator koalescencyjny . Jest używany z typami nullable (między innymi sorry:)

 1
Author: JP Alioto,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-05-06 00:06:40