C# 中结构体的 null 比较、前缀增量、属性、长度属性

发布于 2024-08-16 06:01:10 字数 1101 浏览 3 评论 0原文

我在 C# 中有一些问题

  1. 区别(值是任何类型的变量:int、string、float...)

  2. 我听说在某些情况下使用前缀增量 ++i 而不是 i++案例将提高程序性能。为什么会这样呢?

  3. 我有一个片段代码如下:

     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#

  1. what are the differences between null comparisons null == value and value == null (value is a variable of any kind: int, string, float,...)

  2. I heard that using prefix increment ++i instead of i++ in some case will enhance the program performance. Why is it so?

  3. 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 技术交流群。

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

发布评论

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

评论(3

忆悲凉 2024-08-23 06:01:10
  1. 在大多数情况下,这不会产生任何影响。它不应该有任何区别。如果有人严重重载了 == 运算符,那么可能会发生这种情况。就我个人而言,我更喜欢 if (x == null)

  2. 当你听到这种事情时,你应该询问具体情况。在某些情况下,它可能会产生影响(至少在过去,在 C 语言中),但是当它单独用作语句时,它完全无关紧要 - 使用您认为更具可读性的那个。当以副作用的方式使用时(例如作为方法参数),可能会有很小很小的差异 - 但它永远不可能是显着的。

  3. 在这种情况下,使用属性还是局部变量都没有区别。在其他一些情况下,它可能会产生影响,具体取决于属性中的代码。不过,将 intnull 进行比较总是会给出 false 的结果,因此始终会返回 2.2。

  4. 您的代码目前无法编译 - 您需要重载 Vector 中的 - 运算符才能使其工作,此时行为将取决于在 - 运算符中的代码上。 Length 属性也是如此。

  1. 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).

  2. 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.

  3. 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 with null is always going to give a result of false though, so 2.2 will always be returned.

  4. Your code at the moment won't compile - you'd need to overload the - operator in Vector 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.

樱花坊 2024-08-23 06:01:10

1:什么都没有,除非您使用参数 x / y 定义了一个自定义相等运算符,在这种情况下,在一个示例中 x 为 null,在另一个 y 为 null

2:不在 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 example x is null, in the other y is null

2: not in C#

3: use neither; int is never null; just return 2.2; - but historically, in C/C++ the null == val is preferred to avoid the mistype bug null = 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

逆夏时光 2024-08-23 06:01:10

命令看出两者之间的区别:

    static void Main(string[] args)
    {
        int i = 1;
        Console.WriteLine(i++);
        i = 1;
        Console.WriteLine(++i);
        Console.Read();
    }

2)如果您不知道的话,可以通过运行以下

1
2

2) The difference between the two, if your not aware ca be seen by running

    static void Main(string[] args)
    {
        int i = 1;
        Console.WriteLine(i++);
        i = 1;
        Console.WriteLine(++i);
        Console.Read();
    }

Which gives:

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