对象是引用类型还是值类型
本文关键字:类型 引用类型 对象 | 更新日期: 2023-09-27 18:22: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
的类型就是你把它框在里面的类型,它既可以是引用类型,也可以是值类型。这是关于你在里面装了什么。我说得对吗?
它是一个引用类型
用字符串做一个例子并不是很有启发性,因为字符串是也是引用类型(很明显,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"
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的文本。
进行时
obj1 = "NewString";
它实际上保存了一个新的引用,指向另一个内存位置,而不是之前给obj2
的位置。当您更改位置obj1
的内容时,您将在obj2
中得到相同的更改。
尝试用更改obj1
的内容
fixed(char* c = obj1 as string)
{
c = '0';
}
您的两个字符串现在都将是"0ldString"
。
这是因为对象是引用类型。
对象变量始终是引用类型。类和字符串是引用类型。结构和枚举是一种值类型。我从各种资源中收集了一个很大的例子。
// 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