常见的 C# 习惯用法包括合并?操作员

发布于 2024-08-23 05:39:22 字数 454 浏览 1 评论 0原文

每个人都至少知道两种常见的 C# 习惯用法,包括合并运算符:

单例运算符

return _staticField = _staticField ?? new SingletonConstructor();

和链运算符:

notNullableResult = nullable1 ?? nullable2 ?? nullable3 ?? default(someType);

它可读、一致、值得使用并且在代码中可识别。

但不幸的是,这就是全部。有时需要扩展或改变。有时,当我看到特定情况时,我会使用它们 - 并且我总是犹豫是否使用它,因为我不知道其他程序员是否真的会轻松阅读它。

你还认识其他人吗?我希望有更具体的用法:例如 Asp.net、EF、LINQ 等任何东西 - 其中合并不仅可以接受,而且非常出色。

Everyone knows at least two common c# idioms including coalesce operator:

a singleton one:

return _staticField = _staticField ?? new SingletonConstructor();

and a chain one:

notNullableResult = nullable1 ?? nullable2 ?? nullable3 ?? default(someType);

it's readable, consistent, worth using and recognizable in code.

But, unfortunately, this is all. Sometimes it will need expanding or change. Sometimes i use them when i see a particular case - and always i hesitate to use it because i dont know if any other programmer will really read it easy.

Do you know any others? I would love to have more specific usages: e.g. Asp.net, EF, LINQ, anything - where coalesce is not only acceptable but remarkable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-08-30 05:39:22

对我来说,它转换为 SQL 本身就足够了:

from a in DB.Table
select new {
             a.Id,
             Val = a.One ?? a.Two ??a.Three
           }

这导致数据库端的 COALSCE 在大多数情况下可以实现非常更干净的 SQL,但实际上并没有任何好的选择。我们正在使用 Oracle,我宁愿不阅读 Nvl(Nvl(Nvl(Nvl( 嵌套,谢谢...我将使用我的 COALSCE 并运行 我也

将它用于这样的属性,我想只是个人口味:

private string _str;
public string Str { 
  get { return _str ?? (_str = LoaderThingyMethod()); } 
}

For me, where it translates to SQL is good enough by itself:

from a in DB.Table
select new {
             a.Id,
             Val = a.One ?? a.Two ??a.Three
           }

This resulting in COALSCE on the database side makes for tremendously cleaner SQL in most cases, there just isn't any great alternative. We're using Oracle, I'd rather not read though Nvl(Nvl(Nvl(Nvl( nesting thank you...I'll take my COALSCE and run with it.

I also use it for properties like this, just personal taste I guess:

private string _str;
public string Str { 
  get { return _str ?? (_str = LoaderThingyMethod()); } 
}
穿透光 2024-08-30 05:39:22

C# 2.0 中引入空合并运算符的原因是有一种方法可以将默认值分配给 可空类型,从而轻松地将可空类型转换为不可空类型。如果没有 ?? ,任何转换都需要冗长的 if 语句。以下引用摘自 MSDN

可为 null 的类型可以包含一个值,
或者它可以是未定义的。这 ??
运算符将默认值定义为
当可为空类型时返回
分配给不可为 null 的类型。如果
你尝试分配一个可为空的值
类型为不可空值类型
不使用 ??运营商,你
将产生编译时错误。如果
您使用强制转换和可为空值
类型当前未定义,
InvalidOperationException异常
将会被抛出。

您可能会认为以下简化“非常出色”:

int? x = null;
int y = x ?? -1;

而不是

int? x = null;
int y = -1;

if (x.HasValue)
{
    y = x.Value;
}

The reason that the null-coalescing operator has been introduced in C# 2.0 was to have an way to assign default values to nullable types and thus easily convert a nullable type into a non-nullable type. Without ?? any conversion would require a lengthy if statement. The following quote is taken from MSDN:

A nullable type can contain a value,
or it can be undefined. The ??
operator defines the default value to
be returned when a nullable type is
assigned to a non-nullable type. If
you try to assign a nullable value
type to a non-nullable value type
without using the ?? operator, you
will generate a compile-time error. If
you use a cast, and the nullable value
type is currently undefined, an
InvalidOperationException exception
will be thrown.

You might consider the following simplification "remarkable":

int? x = null;
int y = x ?? -1;

Instead of

int? x = null;
int y = -1;

if (x.HasValue)
{
    y = x.Value;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文