什么< ??>在C#.NET符号是什么意思? [英] What does <??> symbol mean in C#.NET?

查看:459
本文介绍了什么< ??>在C#.NET符号是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  什么是“ ??”的运营商?

我看到一行code的规定 -

I saw a line of code which states -

return (str ?? string.Empty).Replace(txtFind.Text, txtReplace.Text);

我想知道这条线的确切含义(即 ?? 部分)..

推荐答案

这是空合并运算符:返回第一个参数,如果它不空,第二个参数,否则。在你的榜样,海峡?的String.Empty 本质上是用来交换空字符串空字符串。

It's the null coalescing operator: it returns the first argument if it's non null, and the second argument otherwise. In your example, str ?? string.Empty is essentially being used to swap null strings for empty strings.

这是可空类型时特别有用,因为它允许指定一个默认值:

It's particularly useful with nullable types, as it allows a default value to be specified:

int? nullableInt = GetNullableInt();
int normalInt = nullableInt ?? 0;

编辑: 海峡?的String.Empty 可在有条件的经营者而言是海峡!= NULL改写? STR:的String.Empty 。如果没有条件运算符,你必须使用更详细的if语句,如:

str ?? string.Empty can be rewritten in terms of the conditional operator as str != null ? str : string.Empty. Without the conditional operator, you'd have to use a more verbose if statement, e.g.:

if (str == null)
{
    str = string.Empty;
}

return str.Replace(txtFind.Text, txtReplace.Text);

这篇关于什么< ??>在C#.NET符号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆