Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.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空条件运算符_C# - Fatal编程技术网

C# c空条件运算符

C# c空条件运算符,c#,C#,我正在尝试使用空条件运算符?但我不太确定它到底应该放在哪里分隔符。我想说的是,如果textLine[index-1]不是null,那么继续。一些帮助?第二个例子应该能回答您的问题: Customer first = customers?[0]; // null if customers is null 这不是工作方式 Null条件运算符,如果父级中有一个以前缀“”标记,则仅返回Null而不是异常?is==null 例如: 你需要的是一个简单的IF条件 如果要在数组中的值为null时不调

我正在尝试使用空条件运算符?但我不太确定它到底应该放在哪里分隔符。我想说的是,如果textLine[index-1]不是null,那么继续。一些帮助?

第二个例子应该能回答您的问题:

 Customer first = customers?[0];  // null if customers is null  
这不是工作方式

Null条件运算符,如果父级中有一个以前缀“”标记,则仅返回Null而不是异常?is==null

例如:

你需要的是一个简单的IF条件


如果要在数组中的值为null时不调用contains方法,则必须先检查它

// requires possible bounds checking
char? test = textLine?[index-1];
if (test != null && separaters.Contains(test.Value))
使用linq:

// does not require bounds checking
char test = textLine?.Skip(index-1).FirstOrDefault() ?? default(char);
if (test != default(char) && separaters.Contains(test))

你的意思是如果表达式不为null,调用Contains方法,否则不?……是的,在其他情况下,程序应该如何操作?很遗憾,这不能做到,你需要使用if-statement。运算符用于表达式,控制流用于语句。您需要控制流,例如所有字符的第一个不能为null。你是想写短信吗?[index-1];相反?是的,这就是我现在意识到的@M.kazemakhgary这是正确的,我不确定他是指textLine将为null还是数组中的值可能为null,我得到了一个超出范围的例外,你可以使用linq。字符测试=文本行?.Skipindex-1.FirstOrDefault??defaultchar。然后针对defaultchar进行测试
// requires possible bounds checking
char? test = textLine?[index-1];
if (test != null && separaters.Contains(test.Value))
// does not require bounds checking
char test = textLine?.Skip(index-1).FirstOrDefault() ?? default(char);
if (test != default(char) && separaters.Contains(test))