Tag: commutativity

带有Nullable 的’==’的参数顺序

以下两个C#函数的区别仅在于将参数的左/右顺序交换为equals运算符== 。 ( IsInitialized的类型是bool )。 使用C#7.1和.NET 4.7 。 static void A(ISupportInitialize x) { if ((x as ISupportInitializeNotification)?.IsInitialized == true) throw null; } static void B(ISupportInitialize x) { if (true == (x as ISupportInitializeNotification)?.IsInitialized) throw null; } 但是第二个的IL代码似乎要复杂得多。 例如, B是: 36个字节(IL代码); 调用其他function,包括newobj和initobj ; 声明四个本地人而不是一个。 IL用于function’A’…… [0] bool flag nop ldarg.0 isinst [System]ISupportInitializeNotification dup brtrue.s L_000e pop […]