什么是“??”运算符? [重复]

时间:2021-08-25 11:42:53

This question already has an answer here:

这个问题在这里已有答案:

I was wondering about ?? signs in C# code. What is it for? And how can I use it?

我想知道??签到C#代码。它是为了什么?我该如何使用它?

What about int?? Is it a nullable int?

什么关于int?它是一个可以为空的int吗?

See also:

?? Null Coalescing Operator —> What does coalescing mean?

??空融合算子 - >合并是什么意思?

12 个解决方案

#1


It's called the "null coalescing operator" and works something like this:

它被称为“空合并运算符”,其工作方式如下:

Instead of doing:

而不是做:

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

You can now just do:

你现在可以这样做:

int result = number ?? 0;

#2


It's the null coalescing operator. It was introduced in C# 2.

它是空合并运算符。它是在C#2中引入的。

The result of the expression a ?? b is a if that's not null, or b otherwise. b isn't evaluated unless it's needed.

表达式的结果是?? b是if不为null,否则为b。除非需要,否则不评估b。

Two nice things:

两件好事:

  • The overall type of the expression is that of the second operand, which is important when you're using nullable value types:

    表达式的整体类型是第二个操作数的类型,这在您使用可空值类型时很重要:

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

    (Note that you can't use a non-nullable value type as the first operand - it would be pointless.)

    (请注意,您不能使用非可空值类型作为第一个操作数 - 它将毫无意义。)

  • The associativity rules mean you can chain this really easily. For example:

    关联性规则意味着您可以轻松地将其链接起来。例如:

    string address = shippingAddress ?? billingAddress ?? contactAddress;
    

That will use the first non-null value out of the shipping, billing or contact address.

这将使用运费,账单或联系地址中的第一个非空值。

#3


That is the coalesce operator. It essentially is shorthand for the following

那就是合并运营商。它基本上是以下的简写

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

MSDN Documentation on the operator

关于运营商的MSDN文档

#4


It's the new Null Coalesce operator.

这是新的Null Coalesce运算符。

The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.

?? ??如果左侧操作数不为null,则运算符返回左侧操作数,否则返回右侧操作数。

You can read about it here: http://msdn.microsoft.com/en-us/library/ms173224(VS.80).aspx

你可以在这里阅读:http://msdn.microsoft.com/en-us/library/ms173224(VS.80).aspx

#5


It is a shortcut for:

它是一个快捷方式:

Text = (category == null ? "Home" : category);

#6


it's the coalesce operator. it will return another value if the first value is null

这是合并运营商。如果第一个值为null,它将返回另一个值

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

string value3 = value1 ?? value2; // assigns "other" to value 3

#7


it checks if category is null - when this is the case the null value is replaced by "Home".

它检查category是否为null - 在这种情况下,null值被“Home”替换。

#8


One of my favorite uses for the null coalescing operator is to avoid if statements in my code (I think if statements are ugly and just clutter things up most times). For example, take a typical scenario where one might choose to load something from cache if available, otherwise load from the db and populate the cache.

对于空合并运算符,我最喜欢的一个用途是避免在我的代码中使用if语句(我认为如果语句很丑陋并且大多数情况下都是混乱的话)。例如,假设一个典型的场景,如果可用,可以选择从缓存加载某些东西,否则从db加载并填充缓存。

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;
}

To me, that's ugly code. I may be a bit anal, but why not refactor it to this instead?

对我来说,这是丑陋的代码。我可能有点肛门,但为什么不重构呢?

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();
}

It more closely follows SRP and is cleaner and easier to read, IMO. The functions perform exactly one clearly identifiable function each.

它更紧密地遵循SRP,更清晰,更容易阅读,IMO。这些功能各自执行一个清晰可识别的功能。

#9


if category is null, Text will become "Home"

如果category为null,Text将变为“Home”

#10


Returns the first not-null value. Handy.

返回第一个非空值。便利。

#11


?? Null-Coalescing Operator

??空融合运算符

int? is a nullable int, which means it can have the values of a normal int and null. Read this for details.

诠释?是一个可以为null的int,这意味着它可以具有普通int和null的值。阅读本文了解详情。

#12


That's the null-coalescing operator . It's used with nullable types (among other things, sorry :)

这是零合并运算符。它用于可空类型(除其他外,抱歉:)

#1


It's called the "null coalescing operator" and works something like this:

它被称为“空合并运算符”,其工作方式如下:

Instead of doing:

而不是做:

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

You can now just do:

你现在可以这样做:

int result = number ?? 0;

#2


It's the null coalescing operator. It was introduced in C# 2.

它是空合并运算符。它是在C#2中引入的。

The result of the expression a ?? b is a if that's not null, or b otherwise. b isn't evaluated unless it's needed.

表达式的结果是?? b是if不为null,否则为b。除非需要,否则不评估b。

Two nice things:

两件好事:

  • The overall type of the expression is that of the second operand, which is important when you're using nullable value types:

    表达式的整体类型是第二个操作数的类型,这在您使用可空值类型时很重要:

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

    (Note that you can't use a non-nullable value type as the first operand - it would be pointless.)

    (请注意,您不能使用非可空值类型作为第一个操作数 - 它将毫无意义。)

  • The associativity rules mean you can chain this really easily. For example:

    关联性规则意味着您可以轻松地将其链接起来。例如:

    string address = shippingAddress ?? billingAddress ?? contactAddress;
    

That will use the first non-null value out of the shipping, billing or contact address.

这将使用运费,账单或联系地址中的第一个非空值。

#3


That is the coalesce operator. It essentially is shorthand for the following

那就是合并运营商。它基本上是以下的简写

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

MSDN Documentation on the operator

关于运营商的MSDN文档

#4


It's the new Null Coalesce operator.

这是新的Null Coalesce运算符。

The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.

?? ??如果左侧操作数不为null,则运算符返回左侧操作数,否则返回右侧操作数。

You can read about it here: http://msdn.microsoft.com/en-us/library/ms173224(VS.80).aspx

你可以在这里阅读:http://msdn.microsoft.com/en-us/library/ms173224(VS.80).aspx

#5


It is a shortcut for:

它是一个快捷方式:

Text = (category == null ? "Home" : category);

#6


it's the coalesce operator. it will return another value if the first value is null

这是合并运营商。如果第一个值为null,它将返回另一个值

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

string value3 = value1 ?? value2; // assigns "other" to value 3

#7


it checks if category is null - when this is the case the null value is replaced by "Home".

它检查category是否为null - 在这种情况下,null值被“Home”替换。

#8


One of my favorite uses for the null coalescing operator is to avoid if statements in my code (I think if statements are ugly and just clutter things up most times). For example, take a typical scenario where one might choose to load something from cache if available, otherwise load from the db and populate the cache.

对于空合并运算符,我最喜欢的一个用途是避免在我的代码中使用if语句(我认为如果语句很丑陋并且大多数情况下都是混乱的话)。例如,假设一个典型的场景,如果可用,可以选择从缓存加载某些东西,否则从db加载并填充缓存。

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;
}

To me, that's ugly code. I may be a bit anal, but why not refactor it to this instead?

对我来说,这是丑陋的代码。我可能有点肛门,但为什么不重构呢?

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();
}

It more closely follows SRP and is cleaner and easier to read, IMO. The functions perform exactly one clearly identifiable function each.

它更紧密地遵循SRP,更清晰,更容易阅读,IMO。这些功能各自执行一个清晰可识别的功能。

#9


if category is null, Text will become "Home"

如果category为null,Text将变为“Home”

#10


Returns the first not-null value. Handy.

返回第一个非空值。便利。

#11


?? Null-Coalescing Operator

??空融合运算符

int? is a nullable int, which means it can have the values of a normal int and null. Read this for details.

诠释?是一个可以为null的int,这意味着它可以具有普通int和null的值。阅读本文了解详情。

#12


That's the null-coalescing operator . It's used with nullable types (among other things, sorry :)

这是零合并运算符。它用于可空类型(除其他外,抱歉:)