Monday, May 24, 2010

Help me in general C# understandings...?

Hi, I want to know about "new" keyword, and difference bettween defining with that and without that.





Look, I have this code in a local scope:





{


TheClass MyClass;


TheClass MyNewClass = new TheClass();





MyClass = MyAnotherInstanceOfTheClass;


MyNewClass = MyAnotherInstanceOfTheClass;





}





In the code above I defined two same type variables, but one using new keyword. but if i change the properties of each one, the properties of the refrence class (MyAnotherInstanceOfTheClass) change.





And note that if I define a class instance in no keyword, that can not be customized directly and it's properties are not changeable, because of there is no memory assigned to. But if I define an instance with "new" keyword, that can be changed and this means that the memory was assigned to.





According to above descriptions, Why changing in each two instances (MyNewClass,MyClass) cause the changing in the refrence class.Why the variable created by new keyword still points to refrence class?

Help me in general C# understandings...?
"new" keyword is used to create an instance of a class or an object. You cannot create objects in C# without using "new" except in case of value types like int, char, string etc.





I'll break your code like this:





TheClass MyClass;


- This will only create a reference variable. No object is created and no object is assigned to this variable.





TheClass MyNewClass = new TheClass();


- MyNewClass is reference variable. A new instance of TheClass is created through "new" and the object is assigned to the variable MyNewClass.





MyClass = MyAnotherInstanceOfTheClass;


- MyAnotherInstanceOfTheClass object is assigned to the reference variable MyClass. No new memory location is allocated.





MyNewClass = MyAnotherInstanceOfTheClass;


- MyAnotherInstanceOfTheClass is assigned to the reference variable MyNewClass. The previous object that it was pointing to gets "lost". The garbage collector (CLR memory manager, whatever) detects this and frees momory where the instance of TheClass which we created in 2nd line was residing. Therefore from this line on you cannot access the object we created in 2nd line, and both the variables point to MyAnotherInstanceOfTheClass.
Reply:Okay, let me comment on your code and you will understand.





TheClass MyClass;-----------------(1)


In the above you are saying MyClass is a variable that can reference(point to) types of TheClass. At this stage, MyClass equals null i.e it is not referencing anything (but if it should reference anything later, this thing should be of type TheClass).





TheClass MyNewClass = new TheClass();


The above is equivalent to


TheClass MyNewClass; ---------------(2)


MyNewClass = new TheClass();-----(3)


(2) is clear (as in the above explanation of (1) )


In (3) You are saying, create an instance of type TheClass and let MyNewClass reference it (point to it).





MyClass = MyAnotherInstanceOfTheClass;---


-----(4)


In (4) you are saying, let MyClass point to where MyAnotherInstanceOfTheClass is pointing. After this statement is excuted, the two variables MyClass and MyAnotherInstanceOfTheClass end up referencing the same memory location (instance).





MyNewClass = MyAnotherInstanceOfTheClass; --- (5)


In (5) You are saying. Make MyNewClass reference the same instance as MyAnotherInstanceOfTheClass





Note: Remember to distinguish between the instance itself which is a memory location and the reference to it.


If the instance changes, then accessing the properties of the instance via any reference will yield the new properties





Thank You.


No comments:

Post a Comment