Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否有条件运算符用于将C中的变量设置为另一个变量的值(如果后者不是');t空?_C#_Conditional Operator - Fatal编程技术网

C# 是否有条件运算符用于将C中的变量设置为另一个变量的值(如果后者不是');t空?

C# 是否有条件运算符用于将C中的变量设置为另一个变量的值(如果后者不是');t空?,c#,conditional-operator,C#,Conditional Operator,类似于三元运算符(?:)或空合并运算符(??)。在我看来,当其他运营商存在时,占用两行并如此冗长似乎很愚蠢 编辑: 既然它是被要求的,这里有两个可能的例子,我希望我能找到 var variable ?= mightBeNull; 或 实际上,它可以用来将一个值(如果它不为null)分配给变量,也可以是一个if,而不使用Else,很好地封装在操作符中。可空类型可以表示基础类型的所有值,以及一个额外的null值 可为null的类型可以表示基础类型的所有值以及附加的null值 可为null的类型可以

类似于三元运算符(?:)或空合并运算符(??)。在我看来,当其他运营商存在时,占用两行并如此冗长似乎很愚蠢

编辑: 既然它是被要求的,这里有两个可能的例子,我希望我能找到

var variable ?= mightBeNull;


实际上,它可以用来将一个值(如果它不为null)分配给变量,也可以是一个if,而不使用Else,很好地封装在操作符中。

可空类型可以表示基础类型的所有值,以及一个额外的null值


可为null的类型可以表示基础类型的所有值以及附加的null值


可为null的类型可以表示基础类型的所有值以及附加的null值


可为null的类型可以表示基础类型的所有值以及附加的null值

那么你想要这个

if (other != null)
    someVariable = other;
你可以做到以下几点,但我认为,由于清晰度和可能的副作用,上述方法更好:

someVariable = other ?? someVariable;
如果
someVariable
是一个属性,并且
get
set
都可能导致副作用,则上述操作可能会导致副作用;如果你的财产遵循普通准则,这一点就不重要了。或者,正如Servy指出的,即使它是一个字段,也可以在多线程应用程序中创建不同的语义。我应该注意到,在第一个示例中,您阅读了两次
other
,这也有可能导致复杂性(尽管可能性比后一个示例小)。

您想要这样吗

if (other != null)
    someVariable = other;
你可以做到以下几点,但我认为,由于清晰度和可能的副作用,上述方法更好:

someVariable = other ?? someVariable;
如果
someVariable
是一个属性,并且
get
set
都可能导致副作用,则上述操作可能会导致副作用;如果你的财产遵循普通准则,这一点就不重要了。或者,正如Servy指出的,即使它是一个字段,也可以在多线程应用程序中创建不同的语义。我应该注意到,在第一个示例中,您阅读了两次
other
,这也有可能导致复杂性(尽管可能性比后一个示例小)。

您想要这样吗

if (other != null)
    someVariable = other;
你可以做到以下几点,但我认为,由于清晰度和可能的副作用,上述方法更好:

someVariable = other ?? someVariable;
如果
someVariable
是一个属性,并且
get
set
都可能导致副作用,则上述操作可能会导致副作用;如果你的财产遵循普通准则,这一点就不重要了。或者,正如Servy指出的,即使它是一个字段,也可以在多线程应用程序中创建不同的语义。我应该注意到,在第一个示例中,您阅读了两次
other
,这也有可能导致复杂性(尽管可能性比后一个示例小)。

您想要这样吗

if (other != null)
    someVariable = other;
你可以做到以下几点,但我认为,由于清晰度和可能的副作用,上述方法更好:

someVariable = other ?? someVariable;

如果
someVariable
是一个属性,并且
get
set
都可能导致副作用,则上述操作可能会导致副作用;如果你的财产遵循普通准则,这一点就不重要了。或者,正如Servy指出的,即使它是一个字段,也可以在多线程应用程序中创建不同的语义。我应该注意到,在第一个示例中,您阅读了两次
other
,这也可能会导致输入复杂性(尽管可能性比后一个示例小)。

要仅在变量不为null时才为其赋值,您需要使用
if
。conditinal和null coalesce操作符都不会这样做

if(somethingElse != null) something = somethingElse;

C#中没有具体的运算符可以做到这一点。

若要仅在变量不为null时为其赋值,则需要使用
if
。conditinal和null coalesce操作符都不会这样做

if(somethingElse != null) something = somethingElse;

C#中没有具体的运算符可以做到这一点。

若要仅在变量不为null时为其赋值,则需要使用
if
。conditinal和null coalesce操作符都不会这样做

if(somethingElse != null) something = somethingElse;

C#中没有具体的运算符可以做到这一点。

若要仅在变量不为null时为其赋值,则需要使用
if
。conditinal和null coalesce操作符都不会这样做

if(somethingElse != null) something = somethingElse;
在C#中没有专门的操作符可以做到这一点。

在Visual Studio 2015 C#中有一个名为的新操作符,可以按照您的要求执行操作。这是什么?。或接线员

int? length = customers?.Length; // null if customers is null 
Customer first = customers?[0];  // null if customers is null
int? count = customers?[0]?.Orders?.Count();  // null if customers, the first customer, or Orders is null

当前可在以前版本的C#中使用,但不是运算符,而是一行程序将使用?:陈述

在Visual Studio 2015 C#中,有一个名为的新操作符,它可以按您的要求执行操作。这是什么?。或接线员

int? length = customers?.Length; // null if customers is null 
Customer first = customers?[0];  // null if customers is null
int? count = customers?[0]?.Orders?.Count();  // null if customers, the first customer, or Orders is null

当前可在以前版本的C#中使用,但不是运算符,而是一行程序将使用?:陈述

在Visual Studio 2015 C#中,有一个名为的新操作符,它可以按您的要求执行操作。这是什么?。或接线员

int? length = customers?.Length; // null if customers is null 
Customer first = customers?[0];  // null if customers is null
int? count = customers?[0]?.Orders?.Count();  // null if customers, the first customer, or Orders is null

当前可在以前版本的C#中使用,但不是运算符,而是一行程序将使用?:陈述

在Visual Studio 2015 C#中,有一个名为的新操作符,它可以按您的要求执行操作。这是什么?。或接线员

int? length = customers?.Length; // null if customers is null 
Customer first = customers?[0];  // null if customers is null
int? count = customers?[0]?.Orders?.Count();  // null if customers, the first customer, or Orders is null

当前可在以前版本的C#中使用,但不是运算符,而是一行程序将使用?:陈述


我打赌这就是你要找的

假设我们有一个函数,
myFunction
,它说明传递的参数
input
是否为null。如果
input
为null,它将返回
“input is null!”
,否则它将返回