Blog Tools
Edit your Blog
Build a Blog
RSS Feed
View Profile
« November 2005 »
S M T W T F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
You are not logged in. Log in
Entries by Topic
All topics  «
.Net
ASP.Net
CSharp
VBA
Renugopal's Programming Blog
Wednesday, 23 November 2005
Why Structue is called Value-Type and Class is called Reference Type?
Topic: .Net
Every one know Structue is called as Value Type and Class is called as
Reference Type in .net Framework.
For Example:

// value-type
struct TestStruct { int s; }

// reference-type
class TestClass { int c; }

TestStruct ts1 = new TestStruct(10);
TestStruct ts2= ts1;
ts2.s = 20;

TestClass tc1 = new TestClass(10);
TestClass tc2 = tc1;
tc2.c = 20;

if you print ts1,ts2,tc1 and tc2 value you will get the Following ouput:

ts1->10 ts2->20
tc1->20 tc2->20


so , when you copy from ts1 to ts2, the value is passes as py value
where as when you copy from tc1 to tc2 the reference of tc1 is passwed to tc2.
when you set new value to tc2 object it also modify tc1 address, because of passing reference.
now your very clear about why structure is Value type and class is reference Type

Posted by Renugopal D at 10:43 AM
Post Comment | Permalink

View Latest Entries