Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
From what I have tested, it appears that when you use an assignment operator for a control object:
Panel p = new Panel();
p = Form1.panel1;


p is actually a pointer to Form1.panel1, even though I've said Panel p = new Panel(); Why is this? Why is p not a whole new control?

From what I understand, this is markedly different from:
int a = 4;
int b = a;


I'm quite confused to the point where I don't even know how to articulate this question properly. Is this a type vs. object thing? Where can I read more about this?
Posted

Technically, I suppose you're right, though I've never really thought about it. Objects in C# are reference types, so p would be a pointer to the address reserved for the new instance of your panel. The new operator does, in fact, create a new instance, so it isn't necessary to worry about it = the runtime sorts it all out.

Simple types are value types, and contain actual values assigned. The only time you need to be concerned about the difference is when passing values around. When you pass a reference type, such as p to a method, any changes the method makes to it are retained when the method exits. When you pass a value type to a method, the method receives a copy of the data, and changes made to it by the method are lost when the method exits.
 
Share this answer
 
Comments
Nish Nishant 13-Jul-10 23:02pm    
Reason for my vote of 5
Worth 5!
In addition to what Roger already said, be aware of structured types, i.e. the ones declared using the keyword struct instead than class; they works like basic types.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900