在 C# Linq 查询中丢弃
Discards inside C# Linq queries
我想知道根据 https://docs.microsoft.com/en-us/dotnet/csharp/discards,在 Linq 查询中使用 Discards 是否是好的模式,示例:
public bool HasRedProduct => Products.Any(_=>_.IsRed == true);
使用
的优点/缺点是什么
public bool HasRedProduct => Products.Any(x=>x.IsRed == true);
Discards, which are temporary, dummy variables that are intentionally unused in application code. Discards are equivalent to unassigned variables; they do not have a value. Because there is only a single discard variable, and that variable may not even be allocated storage, discards can reduce memory allocations. Because they make the intent of your code clear, they enhance its readability and maintainability.
这意味着这些变量不会在您的代码中使用,并且正如所写的那样,它们没有价值。所以你的查询应该抛出空引用异常(因为 _
未分配
这不是丢弃 - 它是一个名为 _
的 lambda 表达式参数。落在文章后面的注释中:
Note that _
is also a valid identifier. When used outside of a supported context, _
is treated not as a discard but as a valid variable.
您可以看出它不是丢弃项,因为它的值 没有被丢弃 - 您在其余的 lambda 表达式中使用它。当您 使用该值时,我 强烈 不鼓励使用 _
作为 lambda 表达式参数名称。但是,当您 想要 丢弃它时,使用 _
作为参数名称是可以的,即使从语言的角度来看它在技术上不是丢弃。 _
这个名字被选为丢弃物正是因为它已经在实践中使用了。
那些下划线只是命名为 _
的 lambda 参数,就像它们在 C# 6 中所做的那样。它们不是废弃物。
丢弃在 C# 7 中引入的某些新上下文(例如,out 变量声明)和一些现有上下文(例如,赋值 _ = expression;
)。在后一种情况下,下划线的 C# 6 解释获胜(如果存在)以尊重向后兼容性。
Discards are variables which you can assign to, but cannot read from.
They don't have names. Instead, they are represented by an '_'
(underscore). In C#7.0, they can appear in the following contexts:
- out variable declarations, such as
bool found = TryGetValue(out var _)
or bool found = TryGetValue(out _)
- deconstruction assignments, such as
(x, _) = deconstructable;
- deconstruction declarations, such as
(var x, var _) = deconstructable;
- is patterns, such as
x is int _
- switch/case patterns, such as
case int _:
The principal representation of discards is an _
(underscore)
designation in a declaration expression. For example, int _
in an
out variable declaration or var (_, _, x)
in a deconstruction
declaration.
The second representation of discards is using the expression _
as a
short-hand for var _
, when no variable named _
is in scope. It is
allowed in out vars, deconstruction assignments and declarations, and
plain assignments (_ = IgnoredReturn();
). It is, however, not
allowed in C#7.0 patterns. When a variable named _
does exist in
scope, then the expression _
is simply a reference to that variable,
as it did in earlier versions of C#.
https://github.com/dotnet/roslyn/blob/master/docs/features/discards.md
我想知道根据 https://docs.microsoft.com/en-us/dotnet/csharp/discards,在 Linq 查询中使用 Discards 是否是好的模式,示例:
public bool HasRedProduct => Products.Any(_=>_.IsRed == true);
使用
的优点/缺点是什么public bool HasRedProduct => Products.Any(x=>x.IsRed == true);
Discards, which are temporary, dummy variables that are intentionally unused in application code. Discards are equivalent to unassigned variables; they do not have a value. Because there is only a single discard variable, and that variable may not even be allocated storage, discards can reduce memory allocations. Because they make the intent of your code clear, they enhance its readability and maintainability.
这意味着这些变量不会在您的代码中使用,并且正如所写的那样,它们没有价值。所以你的查询应该抛出空引用异常(因为 _
未分配
这不是丢弃 - 它是一个名为 _
的 lambda 表达式参数。落在文章后面的注释中:
Note that
_
is also a valid identifier. When used outside of a supported context,_
is treated not as a discard but as a valid variable.
您可以看出它不是丢弃项,因为它的值 没有被丢弃 - 您在其余的 lambda 表达式中使用它。当您 使用该值时,我 强烈 不鼓励使用 _
作为 lambda 表达式参数名称。但是,当您 想要 丢弃它时,使用 _
作为参数名称是可以的,即使从语言的角度来看它在技术上不是丢弃。 _
这个名字被选为丢弃物正是因为它已经在实践中使用了。
那些下划线只是命名为 _
的 lambda 参数,就像它们在 C# 6 中所做的那样。它们不是废弃物。
丢弃在 C# 7 中引入的某些新上下文(例如,out 变量声明)和一些现有上下文(例如,赋值 _ = expression;
)。在后一种情况下,下划线的 C# 6 解释获胜(如果存在)以尊重向后兼容性。
Discards are variables which you can assign to, but cannot read from. They don't have names. Instead, they are represented by an '_' (underscore). In C#7.0, they can appear in the following contexts:
- out variable declarations, such as
bool found = TryGetValue(out var _)
orbool found = TryGetValue(out _)
- deconstruction assignments, such as
(x, _) = deconstructable;
- deconstruction declarations, such as
(var x, var _) = deconstructable;
- is patterns, such as
x is int _
- switch/case patterns, such as
case int _:
The principal representation of discards is an
_
(underscore) designation in a declaration expression. For example,int _
in an out variable declaration orvar (_, _, x)
in a deconstruction declaration.The second representation of discards is using the expression
_
as a short-hand forvar _
, when no variable named_
is in scope. It is allowed in out vars, deconstruction assignments and declarations, and plain assignments (_ = IgnoredReturn();
). It is, however, not allowed in C#7.0 patterns. When a variable named_
does exist in scope, then the expression_
is simply a reference to that variable, as it did in earlier versions of C#.
https://github.com/dotnet/roslyn/blob/master/docs/features/discards.md