更新:2007 年 11 月
对于预定义的值类型,如果操作数的值相等,则相等运算符 (==) 返回 true,否则返回 false。对于
备注
用户定义的值类型可重载 == 运算符(请参见
示例
C# | 复制代码 |
---|---|
class Equality { static void Main() { // Numeric equality: True Console.WriteLine((2 + 2) == 4); // Reference equality: different objects, // same boxed value: False. object s = 1; object t = 1; Console.WriteLine(s == t); // Define some strings: string a = "hello"; string b = String.Copy(a); string c = "hello"; // Compare string values of a constant and an instance: True Console.WriteLine(a == b); // Compare string references; // a is a constant but b is an instance: False. Console.WriteLine((object)a == (object)b); // Compare string references, both constants // have the same value, so string interning // points to same reference: True. Console.WriteLine((object)a == (object)c); } } /* Output: True False True False True */ |