25

我仍然对object. 它是任何东西的主要基类,任何类。但它是引用类型还是值类型。或者喜欢这些行为中的哪一个呢?我需要澄清这一点。我很难理解。

     object obj1 = "OldString";
     object obj2 = obj1;
     obj1 = "NewString";
     MessageBox.Show(obj1 + "   " + obj2);
     //Output is  "NewString   OldString" 

在这种情况下,它就像一个值类型。如果对象是引用类型,那么为什么 obj2 值仍然是“OldString”

   class SampleClass
    {
        public string Text { get; set; }
    }

    SampleClass Sample1 = new SampleClass();
    Sample1.Text="OldText";         

    object refer1 = Sample1;
    object refer2 = refer1;

    Sample1.Text = "NewText";

    MessageBox.Show((refer1 as SampleClass).Text +  (refer2 as SampleClass).Text);
    //OutPut is "NewText   NewText"   

在这种情况下,它就像引用类型

我们可以推断出它object的类型就是你在里面装的东西。它既可以是引用类型,也可以是值类型。这是关于你在里面装的东西。我对吗?

4

4 回答 4

27

它是参考类型

用字符串做一个例子并不是很有启发性,因为字符串也是一个引用类型(SampleClass显然是这样);您的示例包含零“拳击”。

如果对象是引用类型,那么为什么 obj2 值仍然是“OldString”

为什么不呢?当您创建一个新字符串时,这不会将旧引用更改为指向新字符串。考虑:

 object obj1 = "OldString";
 // create a new string; assign obj1 the reference to that new string "OldString"

object obj2 = obj1;
 // copy the reference from obj1 and assign into obj2; obj2 now refers to
 // the same string instance

 obj1 = "NewString";
 // create a new string and assign that new reference to obj1; note we haven't
 // changed obj2 - that still points to the original string, "OldString"
于 2013-07-16T09:38:50.777 回答
10

当你这样做

obj1 = "NewString";

它实际上持有一个的引用,指向另一个内存位置,而不是你之前给的那个位置obj2。当你改变位置的内容时obj1,你会得到同样的改变obj2

尝试改变obj1with的内容

fixed(char* c = obj1 as string)
{
    c = '0';
}

你的两个字符串现在都是"0ldString".

这是因为对象是引用类型。

于 2013-07-16T09:39:24.740 回答
9

object变量始终是引用类型。

可以object通过拳击的力量“引用”一个值类型。该框是object变量所引用的值的引用类型包装器。

int x = 10;     // a value-type
object o = x;

该变量o是对包含x- 值的框的引用,但它不是x

x = 20;
MessageBox.Show( string.Format( "x:{0} o:{1}", x, o ) );

这可能对可变值类型更有启发性:

struct SampleClass
{
    public string Text { get; set };
    public override string ToString() { return Text; }
}

var x = new SampleClass{ Text = "Hello" };
object o = x;
x.Text = "World";
MessageBox.Show( string.Format( "{0} {1}", x, o ) );

o是对 的盒装引用x,因此更改x的值对 没有影响o

更改SampleClass为类而不是结构(引用类型而不是值类型)会改变行为:该行object o = x;会使 o 引用与 x 相同的东西,并且更改 x 的文本也会更改 o 的文本。

于 2013-07-16T10:39:28.113 回答
2

对象变量始终是引用类型。类和字符串是引用类型。结构和枚举是一种值类型。我从各种资源中汇总了一个大例子。

// PrintedPage is a value type
//this is a struct
struct PrintedPage
{
    public string Text;
}

// WebPage is a reference type
class WebPage
{
    public string Text;
}

struct SampleClass
{
    public string Text { get; set; }
    public override string ToString() { return Text; }
}

void Main()
{
        // First look at value type behaviour
        PrintedPage originalPrintedPage = new PrintedPage();
        originalPrintedPage.Text = "Original printed text";

        // Copy all the information
        PrintedPage copyOfPrintedPage = originalPrintedPage;

        // Change the new copy
        copyOfPrintedPage.Text = "Changed printed text";

        // Write out the contents of the original page.
        // Output=Original printed text
        Console.WriteLine ("originalPrintedPage={0}",
                           originalPrintedPage.Text);


       //-------------------------------------------------------------------
        // Now look at reference type behaviour
        WebPage originalWebPage = new WebPage();
        originalWebPage.Text = "Original web text";

        // Copy just the URL
        WebPage copyOfWebPage = originalWebPage;
        // Change the page via the new copy of the URL
        copyOfWebPage.Text = "Changed web text";

        // Write out the contents of the page
        // Output=Changed web text
        Console.WriteLine ("originalWebPage={0}",
                           originalWebPage.Text);

        // Now change the copied URL variable to look at
        // a different web page completely
        copyOfWebPage = new WebPage();
        copyOfWebPage.Text = "Changed web page again";

         Console.WriteLine ("originalWebPage={0}",
                           originalWebPage.Text);
        Console.WriteLine ("copyOfWebPage={0}",
                           copyOfWebPage.Text);


       //-------------------------------------------------------------------
        //string are reference type too
         object obj1 = "OriginalString"; // create a new string; assign obj1 the reference to that new string "OriginalString"
         object obj2 = obj1;// copy the reference from obj1 and assign into obj2; obj2 now refers to // the same string instance
         obj1 = "NotOriginalString";// create a new string and assign that new reference to obj1; note we haven't // changed obj2 - that still points to the original string, "OriginalString"
        /*   When you do obj1 = "NewString"; it actually holds a new reference, to another memory location, not the same location you gave to obj2 before. 
           IMP -  When you change the content of the location obj1, you will get the same change in obj2.
        */
         Console.WriteLine(obj1 + "   " + obj2);

       //-------------------------------------------------------------------
         object onj11 = 2; 
         object obj12 = onj11;
         onj11 = 3; //you assigned boj11 to a new reference but obj12 reference did not change
         Console.WriteLine(onj11 + "   " + obj12);

       //-------------------------------------------------------------------     
         /*look below - it's possible for object to "reference" a value-type by the power of boxing. The box is a reference-type wrapper around a value, to which the object variable refers.*/
         int i = 2; //int is value type
         object j = i; //variable j is a reference to a box containing the value of i- but it's not i
         i = 3;  
         Console.WriteLine(i + "   " + j);       

       //-------------------------------------------------------------------
        var x = new SampleClass{ Text = "Hello" };
        object o = x;
        x.Text = "World";
        Console.WriteLine(x.Text + "   " + o);

       //-------------------------------------------------------------------
        SampleClass x1 = new SampleClass{ Text = "Hello" }; //sample class is of type struct which is value type; it is was of type class then the data would be copied over and result would be World World
        SampleClass o1 = x1;
        o1.Text = "World";
        Console.WriteLine(x + "   " + o);
    }

参考资料 - http://jonskeet.uk/csharp/references.html

于 2016-11-25T19:06:18.103 回答