对象是引用类型还是值类型?

时间:2013-07-16 09:37:42

标签: c# oop object value-type reference-type

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

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

在这种情况下,它就像一个值类型。如果object是引用类型,那么为什么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 个答案:

答案 0 :(得分:22)

这是一种参考类型

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

  

如果object是引用类型,那么为什么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"

答案 1 :(得分:9)

当你这样做时

obj1 = "NewString";

它实际上将 new 引用保存到另一个内存位置,而不是之前提供给obj2的位置。当您更改位置obj1的内容时,您将在obj2中获得相同的更改。

尝试使用

更改obj1的内容
fixed(char* c = obj1 as string)
{
    c = '0';
}

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

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

答案 2 :(得分:8)

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的文本。

答案 3 :(得分:0)

对象变量始终是引用类型。 类和字符串是引用类型。结构和枚举是一种价值类型。 我已经汇集了各种资源的一个很好的例子。

// 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