Class instances differences

Posted by krrishbiju-15589 under C# on 7/25/2013 | Points: 10 | Views : 779 | Status : [Member] | Replies : 7
Hi ,
I have class ClassA.
What is the difference between
ClassA ob=new ClassA(); and
ClassA ob=null;


Thanks and Regards
krrish




Responses

Posted by: Satyapriyanayak on: 7/25/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
In C#, the new keyword can be used as an operator or as a modifier.
new Operator

The new operator is used to create objects and invoke constructors, for example:

Class1 MyClass = new Class1();

The null keyword is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables. Ordinary value types cannot be null. However, C# 2.0 introduced nullable value types



http://msdn.microsoft.com/en-us/library/51y09td4%28v=vs.71%29.aspx

http://msdn.microsoft.com/en-us/library/edakx9da.aspx



If this post helps you mark it as answer
Thanks

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ssj_Kumar on: 7/25/2013 [Member] Starter | Points: 25

Up
0
Down
Adding some more interesting points
Class1 MyClass=new Class1();
Memory will be created in Heap
This is reference type

Class1 MyClass=null;
Memory will be created in Stack
This is Value type

Regards,
Jayakumar Selvakani

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: krrishbiju-15589 on: 7/25/2013 [Member] Starter | Points: 25

Up
0
Down
Interesting....can you pleases explain when and why these two used.....
Thanks
krrish

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ssj_Kumar on: 7/27/2013 [Member] Starter | Points: 25

Up
0
Down
Have a look on below link, it may explain some extend
http://forums.asp.net/t/1906391.aspx/1?difference+between+class+variable+and+class+object+c+

Regards,
Jayakumar Selvakani

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jitendrasoft09 on: 7/28/2013 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi Krrishbiju,

Nice question. Look into below details for class instance differences-

Class1 MyClass=new Class1(); To create objects and invoke constructors.

Class1 MyClass=null; - Does not invoke constructor. A null reference does not point to any storage location.

Hope you have understood.

Jitendra Kumar
If my post helps you, plz mark as an answer.

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: aswinialuri-19361 on: 7/30/2013 [Member] Starter | Points: 25

Up
0
Down
hi,
ClassA ob=new ClassA(); it creates new instance and default constructor
ClassA ob=null; it is null reference and it doesn't create any constructor.

Mark as Answer if it helps you
Thanks&Regards
Aswini Aluri

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ssj_Kumar on: 7/31/2013 [Member] Starter | Points: 25

Up
0
Down
Class1 MyClass=new Class()
Object will be created in Heap and address will be saved in Stack, In this case we are using both Heap and StacK
In this case this is reference type

Class1 MyClass=null;
Object will not be created so it won't create your Heap, Only Stack memory will be created and it hold Null Value
In this case this is Value type

Regards,
Jayakumar Selvakani

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response