 |
BOOK: Beginning Visual C# 2012
 | This is the forum to discuss the Wrox book Beginning Visual C# 2012 by Karli Watson, Jacob Vibe Hammer, Jon Reid, Morgan Skinner, Daniel Kemper, Christian Nagel, ; ISBN: 978-1-118-31441-8 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning Visual C# 2012 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

June 10th, 2014, 04:43 AM
|
|
Authorized User
|
|
Join Date: May 2014
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
struct & class
hi,
I read something about stuct and class, it says stuct's type is value type, and class's type is reference type, a bit hard to understand to me. The below code's result both are 20, I wonder why. please let it easy to understand, thanks!
Code:
MyClass objectA = new Myclass();
MyClass objectB = objectA;
objectA.val = 10;
objectB.val = 20;
Console.WriteLine("objectA.val = {0}", objectA.val);
Console.WriteLine("objectB.val = {0}", objectB.val);
|
|

June 11th, 2014, 06:29 AM
|
|
Registered User
|
|
Join Date: Jun 2014
Posts: 3
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Quote:
Originally Posted by chinaNoob
hi,
I read something about stuct and class, it says stuct's type is value type, and class's type is reference type, a bit hard to understand to me. The below code's result both are 20, I wonder why. please let it easy to understand, thanks!
Code:
MyClass objectA = new Myclass();
MyClass objectB = objectA;
objectA.val = 10;
objectB.val = 20;
Console.WriteLine("objectA.val = {0}", objectA.val);
Console.WriteLine("objectB.val = {0}", objectB.val);
|
yes structs are value types and classes are Reference types here you create instance of class(Myclass()) so then you assign that instance (objectA) to new veriable of that classs you create two variable pointing to one object. But remember in this scenario you create two variable whose point to same object(Myclass) and as you said that classes are referene type soo when you change objectB.val to 20 it also change objectA.val 20;
example
MyClass objectA = new Myclass(); // objectA is variable which point to Myclass object
MyClass objectB = objectA; // here you create new variable which is also point same object
if you create variable objectB like this
Myclass objectB = new Myclass(); // it mean it create new totaly new object in heap
objectA.val = 10;// now objectA.val =10
objectB.val = 20;// now objectB.val = 20
Console.WriteLine("objectA.val = {0}", objectA.val);//10
Console.WriteLine("objectB.val = {0}", objectB.val);//20
|
|

June 11th, 2014, 11:20 PM
|
|
Friend of Wrox
|
|
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
|
|
The major difference between a struct and a class is were the allocation takes place. A struct being a value type is created on the system stack when defining it. A class on the other hand being a reference type is created on the heap.
There are several other difference like, stucts are passed to functions by value meaning a copy of struct is created and passed into a function. Classes are passed by reference. Stucts can only contain data elements meaning no functions.
Also wanted to make note that your examples above dont include any stucts.
Last edited by mmorgan30; June 11th, 2014 at 11:28 PM..
|
|

June 13th, 2014, 03:07 AM
|
|
Authorized User
|
|
Join Date: May 2014
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Hi babarqb,
objectA has initialized when created, it has being assigned space on system stack, so I thought when objectB = objectA, it will copy all to objectB.
from what you said, it'll only copy its "pointer" to objectB, right?
|
|

June 13th, 2014, 03:15 AM
|
|
Authorized User
|
|
Join Date: May 2014
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by mmorgan30
Also wanted to make note that your examples above dont include any stucts.
|
lol, yes, I cut it. another, where the class instance created on?
|
|

June 14th, 2014, 05:27 PM
|
|
Registered User
|
|
Join Date: Jun 2014
Posts: 3
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Quote:
Originally Posted by chinaNoob
Hi babarqb,
objectA has initialized when created, it has being assigned space on system stack, so I thought when objectB = objectA, it will copy all to objectB.
from what you said, it'll only copy its "pointer" to objectB, right?
|
no you are wrong here. when object instantiate it will create object in heap and variable which hold that object create in stack. sooo when you assign already created a variable to another variable(objectB= objectA) it mean you can't create new object in heap so you only create another variable which is point to same object. new object only create when you use ( 'new') keyword and instantiate the class. and remember classes are reference type. and in other word struct are total different then class because they are value type and they live in stack and when you assign (objectB= objectA) as struct. you make a carbon copy of objectA value and assign into objectB value it mean when you change value of objectB or objectA they not effect each other because they are hold there own value. i hope you understand and sorry for my bad english. if still have doubt ask again.
for example
int i = 20;
int j = i;
j = 22;
Console.WriteLine("value of :{0} , value of:{1}",i,j);
Result: value of: 20, value of: j 22
in other word
var obj1 = new MyClass();
obj1.name = "Sam";
var obj2 = obj1;
obj2.name = "Jhone";
Console.WriteLine("Name of obj1:{0} , Name of obj2{1}",obj1,obj2);
Result: Name of obj1:Jhone ,Name of obj2:jhone
var st1 = new MyStruct("Sam");
var st2 = st1;
st2.Name = "Jhone";
Console.WriteLine(st1.Name + " :" + st2.Name);
Result: Name of st1:Sam,Name of st2:jhone
|
|
The Following User Says Thank You to babarqb For This Useful Post:
|
|
|

June 16th, 2014, 02:56 AM
|
|
Authorized User
|
|
Join Date: May 2014
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Hi babarqb,
Your english is OK, mine is real bad. I read it again and again, and finally I got all you described. "when object instantiate it will create object in heap and variable which hold that object create in stack", that's what made me clear where am I wrong before. and your example, that's helps a lot. Thanks! ^_^
|
|

June 17th, 2014, 05:46 AM
|
|
Registered User
|
|
Join Date: Jun 2014
Posts: 3
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Nice to see you that you have no doubt know. Good luck in future and feel free to ask every time you stuck. Happy Coding
|
|
 |
|