简体   繁体   English

空比较,前缀增量,属性,C#中struct的Length属性

[英]null comparison, prefix increment, properties, Length properties of struct in C#

I have some questions in C# 我在C#中有一些问题

  1. what are the differences between null comparisons null == value and value == null (value is a variable of any kind: int, string, float,...) null比较null == value和value == null(value是任何类型的变量:int,string,float等)之间的区别是什么?

  2. I heard that using prefix increment ++i instead of i++ in some case will enhance the program performance. 我听说在某些情况下使用前缀增量++ i代替i ++可以提高程序性能。 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 问题是为什么在这里我们使用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;
        }
    }

if we subtract 2 struct likes above, what will be return? 如果我们减去上面的2个struct,将返回什么? and the Length properties of struct will return what? 而struct的Length属性将返回什么?

Is anyone willing to enlighten me? 有人愿意启发我吗? Thank in advance 预先感谢

  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) . 我个人更喜欢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. 在某些情况下,它可能会有所作为(至少在过去的C语言中是这样),但是当将它单独用作语句时,则完全无关紧要-使用更易读的内容。 When used in a side-effecting way (eg 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. 尽管将intnull比较总是会false的结果,所以将始终返回2.2。

  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. 目前,您的代码无法编译-您需要在Vector重载-运算符才能使其正常工作,这时的行为将取决于-运算符中的代码。 The same is true for the Length property. Length属性也是如此。

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 1:什么都没有,除非您已使用参数x / y定义了一个自定义相等运算符,在这种情况下,一个示例x为空,而另一个y为空

2: not in C# 2:不在C#中

3: use neither; 3:都不使用; int is never null; int永远不会为null; just return 2.2; 只需return 2.2; - but historically , in C/C++ the null == val is preferred to avoid the mistype bug null = val ; -但历史上看 ,在C / C ++的null == val优选避免打字错误错误null = val ; in C# this type rarely (but sometimes) compiles, so it is less of an issue; 在C#中,这种类型很少 (但有时)会编译,因此问题不大。 val == null is clearer and IMO more common in C# val == null更清晰,IMO在C#中更常见

4: that won't compile unless you provide a subtraction operator, in which case what it returns is defined by your operator 4:除非提供减法运算符,否则不会编译,在这种情况下,它返回的内容由运算符定义

2) The difference between the two, if your not aware ca be seen by running 2)如果您不知道可以通过运行看到两者之间的差异

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

Which gives: 这使:

1
2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM