C# 中结构体的 null 比较、前缀增量、属性、长度属性
我在 C# 中有一些问题
- 区别(值是任何类型的变量:int、string、float...)
我听说在某些情况下使用前缀增量 ++i 而不是 i++案例将提高程序性能。为什么会这样呢?
我有一个片段代码如下:
private int _number; 公共整数号 { 获取{返回_number} 设置 {_number = 值} } 公开双测 { 得到 { if (null == 数字) 返回1.1; 别的 返回2.2; } }
问题是为什么这里我们使用 null == Number 但不使用 null == _number 或 Number == null 或 _number == null
4. if I have a struct as follow:
public struct Vector
{
public double X;
public double Y;
public Vector(double x, double y)
{
X = x;
Y = y;
}
}
public class Test
{
public Vector Position;
public void StructLength(Test t2)
{
Vector v = this.Position - t2.Position;
if (v.Length > 10)
return false;
}
}
如果我们像上面那样减去 2 个结构体,会得到什么回报? struct 的 Length 属性将返回什么?
有谁愿意解惑我吗? 提前致谢
I have some questions in C#
what are the differences between null comparisons null == value and value == null (value is a variable of any kind: int, string, float,...)
I heard that using prefix increment ++i instead of i++ in some case will enhance the program performance. Why is it so?
I have a snippet code as follow:
private int _number; public int Number { get { return _number} set { _number = value} } public double Test { get { if (null == Number) return 1.1; else return 2.2; } }
the question is why here we use null == Number but not null == _number or Number == null or _number == null
4. if I have a struct as follow:
public struct Vector
{
public double X;
public double Y;
public Vector(double x, double y)
{
X = x;
Y = y;
}
}
public class Test
{
public Vector Position;
public void StructLength(Test t2)
{
Vector v = this.Position - t2.Position;
if (v.Length > 10)
return false;
}
}
if we subtract 2 struct likes above, what will be return? and the Length properties of struct will return what?
Is anyone willing to enlighten me?
Thank in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在大多数情况下,这不会产生任何影响。它不应该有任何区别。如果有人严重重载了 == 运算符,那么可能会发生这种情况。就我个人而言,我更喜欢
if (x == null)
。当你听到这种事情时,你应该询问具体情况。在某些情况下,它可能会产生影响(至少在过去,在 C 语言中),但是当它单独用作语句时,它完全无关紧要 - 使用您认为更具可读性的那个。当以副作用的方式使用时(例如作为方法参数),可能会有很小很小的差异 - 但它永远不可能是显着的。
在这种情况下,使用属性还是局部变量都没有区别。在其他一些情况下,它可能会产生影响,具体取决于属性中的代码。不过,将
int
与null
进行比较总是会给出false
的结果,因此始终会返回 2.2。您的代码目前无法编译 - 您需要重载
Vector
中的-
运算符才能使其工作,此时行为将取决于在-
运算符中的代码上。 Length 属性也是如此。In most cases it won't make any difference. It shouldn't make any difference. If someone overloads the == operator badly it might do. Personally I prefer
if (x == null)
.You should ask for specifics when you hear this sort of thing. In some cases it could make a difference (at least in the past, in C), but when it's used as a statement on its own it's entirely irrelevant - use whichever you find more readable. When used in a side-effecting way (e.g. as a method argument) there may be a tiny, tiny difference - but it's never likely to be significant.
It makes no difference whether you use the property or the local variable in this case. In some other cases it may make a difference, depending on the code in the property. Comparing an
int
withnull
is always going to give a result offalse
though, so 2.2 will always be returned.Your code at the moment won't compile - you'd need to overload the
-
operator inVector
for it to work, at which point the behavior will depend on the code in the-
operator. The same is true for the Length property.1:什么都没有,除非您使用参数
x
/y
定义了一个自定义相等运算符,在这种情况下,在一个示例中x
为 null,在另一个y
为 null2:不在 C# 中
3:两者都不使用;
int
永远不会为空;只需return 2.2;
- 但历史,在 C/C++ 中,首选null == val
以避免错误输入错误null =值
;在 C# 中,这种类型很少(但有时)编译,因此问题不大;val == null
更清晰,IMO 在 C#4 中更常见:除非您提供减法运算符,否则不会编译,在这种情况下,它返回的内容由您的运算符定义
1: nothing, unless you have defined a custom equality operator with params
x
/y
, in which case in one examplex
is null, in the othery
is null2: not in C#
3: use neither;
int
is never null; justreturn 2.2;
- but historically, in C/C++ thenull == val
is preferred to avoid the mistype bugnull = val
; in C# this type rarely (but sometimes) compiles, so it is less of an issue;val == null
is clearer and IMO more common in C#4: that won't compile unless you provide a subtraction operator, in which case what it returns is defined by your operator
命令看出两者之间的区别:
2)如果您不知道的话,可以通过运行以下
2) The difference between the two, if your not aware ca be seen by running
Which gives: