"-6ren"> "- 这个问题在这里已经有了答案: C# okay with comparing value types to null (11 个答案) 关闭 5 年前。 这个语句似乎有效,但我不确定具体如何 int -6ren">
gpt4 book ai didi

c# - 评估 "null == "

转载 作者:太空宇宙 更新时间:2023-11-03 22:47:34 25 4
gpt4 key购买 nike

<分区>

这个语句似乎有效,但我不确定具体如何

int id = 0; // non-nullable ("primative"/value-type/struct)

_list?.Find(t => { return t?.Id == id; })?.DeptId ?? 0;

如果t null ,然后 t?.Id null ,对吗?

那么这怎么合法:

null == id

更新 (5/8/18)

visual studio中的语句

if (null == 0) {}

在将鼠标悬停在状态上时以绿色突出显示并检查

"The result of the expression is always 'false' since a value of type 'int' is never equal to 'null' of type 'int?'


更新 (5/24/18)

Nullable<T> Struct

Fundamental Properties

The two fundamental members of the Nullable structure are the HasValue and Value properties. If the HasValue property for a Nullable object is true, the value of the object can be accessed with the Value property. If the HasValue property is false, the value of the object is undefined and an attempt to access the Value property throws an InvalidOperationException.

Boxing and Unboxing

When a nullable type is boxed, the common language runtime automatically boxes the underlying value of the Nullable object, not the Nullable object itself. That is, if the HasValue property is true, the contents of the Value property is boxed. When the underlying value of a nullable type is unboxed, the common language runtime creates a new Nullable structure initialized to the underlying value.

If the HasValue property of a nullable type is false, the result of a boxing operation is null. Consequently, if a boxed nullable type is passed to a method that expects an object argument, that method must be prepared to handle the case where the argument is null. When null is unboxed into a nullable type, the common language runtime creates a new Nullable structure and initializes its HasValue property to false.

ref: https://learn.microsoft.com/en-us/dotnet/api/system.nullable-1?view=netframework-4.7.2

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com