class Test { static void Main() { string s = "Test"; string t = string.Copy(s); Console.WriteLine(s == t); Console.WriteLine((object)s == (object)t); } }各位高手能不能詳細講解一下這兩個比較多的區別。

解决方案 »

  1.   


    class Test { 
         static void Main() { 
                        string s = "Test"; 
                        string t = string.Copy(s); 
                        Console.WriteLine(s == t); 
                        Console.WriteLine((object)s == (object)t); 
                             } 
               }
      

  2.   

    == 运算符(C# 参考)  发送反馈
    对于预定义的值类型,如果操作数的值相等,则相等运算符 ( ==) 返回 true,否则返回 false。 对于 string 以外的引用类型,如果两个操作数引用同一个对象,则 == 返回 true。 对于 string 类型, == 比较字符串的值。 备注用户定义的值类型可重载 == 运算符(请参见 operator)。 用户定义的引用类型也可重载 == 运算符,尽管在默认情况下,无论对于预定义的引用类型还是用户定义的引用类型, == 的行为都与上面描述的相同。 如果重载 ==,则还必须重载 !=。 在枚举时通常允许整型运算。示例VBC#C++F#JScript以带有颜色区分的格式查看复制到剪贴板打印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
    */
      

  3.   

    能 啊
    結果為 
    True
    False
      

  4.   

    s == t  判断值是否相等
    object)s == (object)t  装箱后判断在堆栈的地址是否相同