3 回答

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個贊

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個贊
int?
表達(dá)式的結(jié)果總是‘false’,因?yàn)椤甶nt’類型的值永遠(yuǎn)不等于‘int’類型的‘null’?
using System;struct MyValue{ private readonly int value; public MyValue(int value) { this.value = value; } public static bool operator ==(MyValue x, MyValue y) { return x.value == y.value; } public static bool operator !=(MyValue x, MyValue y) { return x.value != y.value; }}class Program{ static void Main() { int i = 1; MyValue v = new MyValue(1); if (i == null) { Console.WriteLine("a"); } // warning if (v == null) { Console.WriteLine("a"); } // no warning }}
Main
MyValue(1)
.method private hidebysig static void Main() cil managed{ .entrypoint .maxstack 2 .locals init ( [0] int32 i, [1] valuetype MyValue v) L_0000: ldc.i4.1 L_0001: stloc.0 L_0002: ldloca.s v L_0004: ldc.i4.1 L_0005: call instance void MyValue::.ctor(int32) L_000a: ret }
private static void Main(){ MyValue v = new MyValue(1);}

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超8個贊
Int32 x
null
.
如果您將一個int與NULL進(jìn)行比較,那么使用兩個int?s的比較操作符是適用的。
“為什么將值類型與空值進(jìn)行比較是一種警告?”
- 3 回答
- 0 關(guān)注
- 622 瀏覽
添加回答
舉報(bào)