Is there a difference between the following two pieces of code?
class Test {
public readonly double Val;
public Test(bool src) {
this.Val = src ? 1 : 0;
}
}
class Test {
public readonly double Val;
public Test(bool src) {
this.Val = src ? 1D : 0D;
}
}
Run Code Online (Sandbox Code Playgroud)
I found that our code base uses the second way of writing.
在C#中定义属性的两种方法
public class Program {
public static bool[] Property1 => new bool[1];
public static bool[] Property2 { get; } = new bool[1];
public static void Main() {
Property1[0] = true;
Property2[0] = true;
Console.WriteLine($"{Property1[0]} {Property2[0]}");
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
这两种方式有不同的结果
假真