почему мы предпочитаем? к?? оператор в c#?

Какие сказанные michaelpryor, плюс получают хорошую книгу. Я нашел невозможным приблизиться к программированию iPhone без одного. Тот, который я использовал, был от Прагматически настроенные Программисты .

Между прочим, если Вы хотите сократить издержки, мини-Mac или MacBook начального уровня, хорошо для разработки. Получите его с минимальным количеством памяти и обновите его, по крайней мере, до памяти 2G сами, поскольку это значительно более дешево, чем цены памяти яблока. С любым необходимо быть в состоянии снова использовать существующий экран, клавиатуру, мышь, диски usb, и т.д.

, Если Вы идете Mac мини-маршрут, можно хотеть удержать, пока новые модели не ожидают, как слухи, они собираются объявить о серьезном обновлении на этой неделе.

, О, и Вы не должны загружать XCode, он прибывает в диски установки. Просто загрузите SDK.

Наконец, что не менее важно, это может указывать очевидное, но Вам нужно устройство iPhone для тестирования на. Можно использовать средство моделирования в SDK, но все еще необходимо попробовать его на устройстве. Можно также использовать 'специальное распределение' для имения друзей с устройствами, испытывают приложение.

Удача.

39
задан RameshVel 9 October 2009 в 12:55
поделиться

7 ответов

The null coalesce operator is much clearer when checking for null, that is its main purpose. It can also be chained.

object a = null;
object b = null;
object c = new object();
object d = a ?? b ?? c; //d == c.

While that operator is limited to null checking, the ternary operator is not. For example

bool isQuestion = true;
string question = isQuestion ? "Yes" : "No";

I think people just aren't aware of the null coalesce operator so they use the ternary operator instead. Ternary existed before C# in most C style languages so if you don't know C# inside and out and/or you programmed in another language, ternary is a natural choice. If you are checking for null though, use the null coalesce operator, it is designed for that, and the IL is slightly optimized (compare ?? to an if then else).

Here is an example comparing the use of each

object a = null;
object b = null;
object c = null;

object nullCoalesce = a ?? b ?? c;

object ternary = a != null ? a : b != null ? b : c;

object ifThenElse;

if (a != null)
    ifThenElse = a;
else if (b != null)
    ifThenElse = b;
else if (c != null)
    ifThenElse = c;

First, just look at the syntax for null coalesce, it is way clearer. Ternary is really confusing. Now lets look at the IL

Null Coalesce Only

.entrypoint
.maxstack 2
.locals init (
    [0] object a,
    [1] object b,
    [2] object c,
    [3] object nullCoalesce)
L_0000: ldnull 
L_0001: stloc.0 
L_0002: ldnull 
L_0003: stloc.1 
L_0004: newobj instance void [mscorlib]System.Object::.ctor()
L_0009: stloc.2 
L_000a: ldloc.0 
L_000b: dup 
L_000c: brtrue.s L_0015
L_000e: pop 
L_000f: ldloc.1 
L_0010: dup 
L_0011: brtrue.s L_0015
L_0013: pop 
L_0014: ldloc.2 
L_0015: stloc.3 
L_0016: ldloc.3 
L_0017: call void [mscorlib]System.Console::WriteLine(object)
L_001c: ret 

Ternary Only

.entrypoint
.maxstack 2
.locals init (
    [0] object a,
    [1] object b,
    [2] object c,
    [3] object ternary)
L_0000: ldnull 
L_0001: stloc.0 
L_0002: ldnull 
L_0003: stloc.1 
L_0004: newobj instance void [mscorlib]System.Object::.ctor()
L_0009: stloc.2 
L_000a: ldloc.0 
L_000b: brtrue.s L_0016
L_000d: ldloc.1 
L_000e: brtrue.s L_0013
L_0010: ldloc.2 
L_0011: br.s L_0017
L_0013: ldloc.1 
L_0014: br.s L_0017
L_0016: ldloc.0 
L_0017: stloc.3 
L_0018: ldloc.3 
L_0019: call void [mscorlib]System.Console::WriteLine(object)
L_001e: ret 

If Then Else Only

.entrypoint
.maxstack 1
.locals init (
    [0] object a,
    [1] object b,
    [2] object c,
    [3] object ifThenElse)
L_0000: ldnull 
L_0001: stloc.0 
L_0002: ldnull 
L_0003: stloc.1 
L_0004: newobj instance void [mscorlib]System.Object::.ctor()
L_0009: stloc.2 
L_000a: ldloc.0 
L_000b: brfalse.s L_0011
L_000d: ldloc.0 
L_000e: stloc.3 
L_000f: br.s L_001a
L_0011: ldloc.1 
L_0012: brfalse.s L_0018
L_0014: ldloc.1 
L_0015: stloc.3 
L_0016: br.s L_001a
L_0018: ldloc.2 
L_0019: stloc.3 
L_001a: ldloc.3 
L_001b: call void [mscorlib]System.Console::WriteLine(object)
L_0020: ret 

IL isn't one of my strong points, so maybe someone can edit my answer and expand on it. I was going to explain my theory, but I'd rather not confuse myself and others. The number of LOC is similar for all three, but not all IL operators take the same length of time to execute.

58
ответ дан 27 November 2019 в 02:26
поделиться

The ?? operator (also known as the null-coalescing operator) is less known than the ternary operator, as it made its debut with .NET 2.0 and Nullable Types. Reasons for not using it probably include not begin aware that it exists, or being more familiar with the ternary operator.

That said, checking for null is not the only thing the ternary operator is good for, so it's not a replacement for it as such, more like a better alternative for a very specific need. :)

12
ответ дан 27 November 2019 в 02:26
поделиться

One reason I can think of is that this operator was introduced in .NET 2.0 so the code for .NET 1.1 cannot have it.

I agree with you, we should be using this more often.

ref link

6
ответ дан 27 November 2019 в 02:26
поделиться

I think it's just a habit from other languages. AFAIK, ?? operator is not used in any other language.

1
ответ дан 27 November 2019 в 02:26
поделиться

One reason (as others have already touched) is likely to be lack of awareness. It could also be (as in my own case), a wish to keep the number of approaches to do similar things in a code base down as much as possible. So I tend to use the ternary operator for all compact if-a-condition-is-met-do-this-otherwise-do-that situations.

For instance, I find the following two statements rather similar on a conceptual level:

return a == null ? string.Empty : a;    
return a > 0 ? a : 0;
2
ответ дан 27 November 2019 в 02:26
поделиться

Based on Bob's answer

public object nullCoalesce(object a, object b, object c)
{
    return a ?? b ?? c;
}
public object ternary(object a, object b, object c)
{
    return a != null ? a : b != null ? b : c;
}
public object ifThenElse(object a, object b, object c)
{
    if (a != null)
        return a;
    else if (b != null)
        return b;
    else
        return c;
}

... this is the IL from release builds ...

.method public hidebysig instance object nullCoalesce(
    object a, 
    object b, 
    object c) cil managed
{
    .maxstack 8
    L_0000: ldarg.1 
    L_0001: dup 
    L_0002: brtrue.s L_000b
    L_0004: pop 
    L_0005: ldarg.2 
    L_0006: dup 
    L_0007: brtrue.s L_000b
    L_0009: pop 
    L_000a: ldarg.3 
    L_000b: ret 
}

.method public hidebysig instance object ternary(
    object a, 
    object b, 
    object c) cil managed
{
    .maxstack 8
    L_0000: ldarg.1 
    L_0001: brtrue.s L_000a
    L_0003: ldarg.2 
    L_0004: brtrue.s L_0008
    L_0006: ldarg.3 
    L_0007: ret 
    L_0008: ldarg.2 
    L_0009: ret 
    L_000a: ldarg.1 
    L_000b: ret 
}

.method public hidebysig instance object ifThenElse(
    object a, 
    object b, 
    object c) cil managed
{
    .maxstack 8
    L_0000: ldarg.1 
    L_0001: brfalse.s L_0005
    L_0003: ldarg.1 
    L_0004: ret 
    L_0005: ldarg.2 
    L_0006: brfalse.s L_000a
    L_0008: ldarg.2 
    L_0009: ret 
    L_000a: ldarg.3 
    L_000b: ret 
}
4
ответ дан 27 November 2019 в 02:26
поделиться

I would have thought the equivalent of

var res = data ?? data.toString();

would be

var res = (data!=null) ? data : data.toString();
0
ответ дан 27 November 2019 в 02:26
поделиться
Другие вопросы по тегам:

Похожие вопросы: