Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi ,

What is creating Reference and Creating object for a class?

Democlass D;
--what happens when C# complier passes through this ,where Democlass is a class?

Democlass D =new Democlass();
--What happens behind the scenes when complier passes through this statement ?


Kindly,help me out .

Regards
Chaithanya M
Posted

This is the most cursory of explanations, and since I'm not a teacher, I'm not emotionally invested in whether or not you "get it".

C#
DemoClass D;


This merely defines the type and scope associated with the object D so that the compiler knows what to allow disallow with regards to references to that objecft elsewhere in the code.

C#
Democlass D =new Democlass();


When you call new, memory is allocated for any properties that might be declared, and a pointer is returned for the object. This is called "instantiation".

This is pretty basic programing stuff, and you would be better served to find a beginner's reference on google.
 
Share this answer
 
v2
Comments
Dylan Morley 13-Apr-11 10:43am    
5 for the effort - I couldn't be bothered, smelled like homework
#realJSOP 13-Apr-11 10:49am    
I know, but I needed the points (j/k). :)
M.CHAITHANYA 13-Apr-11 11:00am    
Hi,
IS D an object or variable?
regards
Chaithanya
#realJSOP 13-Apr-11 11:26am    
D is a reference to an object.
Democlass D;
Creates a variable called "D" that can hold a reference to an object of type "Democlass", or a class derived from it. Assigns null to "D" - it refers to nothing.

Democlass D = new Democlass();
Does exactly the same as the previous example, except that an instance of DemoClass is created on the managed heap and assigned to "D".

The big difference is that in the first example, attempts to use D.Property or D.Method() will result in an exception being thrown, the second will work.

"Hi ,
Now,,Consider D has some reference ..Will it(variable D) be stored in the stack or heap?"


Depends on where it is declared.
If it is in a method: Stack
If it is in a Class: Heap (because the class is a reference type and goes on the heap, so it's content goes on the heap)
If it is in a Struct: stack (because it is a value type, and they go on the stack)
 
Share this answer
 
v2
Comments
M.CHAITHANYA 13-Apr-11 10:52am    
Hi ,
Now,,Consider D has some reference ..Will it(variable D) be stored in the stack or heap?
OriginalGriff 13-Apr-11 11:01am    
Answer updated
#realJSOP 13-Apr-11 11:27am    
All objects in .Net are pointers. You should google ".Net memory management".
Sergey Alexandrovich Kryukov 13-Apr-11 11:55am    
John, yes, they are all kind of pointers (in the GC sense of this word), but only if you consider all variable names as pointers. If you consider the in this way, reference-type variables are pointers to pointers to heap.
--SA
Sergey Alexandrovich Kryukov 13-Apr-11 11:54am    
Griff, you forgot static memory area for static fields. Please see my Answer for overview of the storage.
--SA
To add to Griff's Answer:

The variables can be stored in three places: stack, heap and the process's static memory area (recommended to keep to minimum). For the reference type, the variable (reference itself) can be stored in all three places, while the memory area pointed by reference is always heap (exclusion is C++/CLI where this limitation is removed due to its unique value semantic for reference types). Structure is a value type, as well as enumeration and primitive types, all other types are reference one.

For simplicity, I will not mention heap below, so just keep in mind the reference is pointing to heap.

Now, it depends on where the variable is declared: if the variable is static, it is stored in static memory area. If the variable is local, it always stored on stack. A field of reference type always goes to stack.

All this rules can be combined, as a structure can have reference-type fields and reference-type static fields, value-type fields and value-type static fields; and a class can have same combination of fields. Figure all possible structures yourself.

—SA
 
Share this answer
 
Comments
OriginalGriff 13-Apr-11 11:59am    
Good addition!
Sergey Alexandrovich Kryukov 13-Apr-11 12:00pm    
Thank you very much, Griff.
--SA
M.CHAITHANYA 13-Apr-11 23:57pm    
Hi,
Class a
{
int[] a;
}

Here array is an reference type.Do you mean(according to the statement :["A field of reference type always goes to stack."]) a has place in the stack in the above example.

Regards
Chaithu
Sergey Alexandrovich Kryukov 14-Apr-11 12:54pm    
Yes. Array uses the technique of dynamic allocation. You variable "a" is a non-static member of s class. When you make an instance "a myA = new a()", myA is a reference somewhere by the myA object is on stack.

But it does not matter for the content of the array. It's elements are always allocated in heap.
Even if a.a was static, the fact it is an array would make use of stack anyway. Even though the variable a.a would be static, the array objects themselves like a[0], a[1], etc. are on stack, because the array is the reference object.

--SA
M.CHAITHANYA 15-Apr-11 0:33am    
Hi SAKryukov,

If it's elements are allocated on the stack ,why it is called reference type(as per your statement above).

Regards
Chaithu

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