Hello everyone,
I know what happens when we give a syntax like,
class_name obj = new class_name();
The above statement will create an instance with the name obj for the class named class_name. Here the constructor of the class will be automatically called and it will return the total number of space required for that instance and that much space will be created by new operator.
But what happens when i give like,
int x = new int();
and also, what is the difference between the above one and
int x = 10;
Loading
Khaja MoizuddinPosted Jul 3, 2017, 8:13 AM
Pradeep YadavPosted Jul 3, 2017, 4:51 AM
Hirendra SisodiyaPosted Apr 27, 2010, 4:44 AM
MSDN describe about new :
The new operator is used to create objects and invoke constructors, for example:
It is also used to invoke the default constructor for value types, for example:
In the preceding statement,
myIntis initialized to0, which is the default value for the type int. The statement has the same effect as:For more info check this :
http://msdn.microsoft.com/en-us/library/51y09td4(VS.71).aspx
thanks
Please mark as answere if it helps
Jaish MathewsPosted Apr 27, 2010, 3:07 AM
int is a value type, Structure.
Each value type has an implicit default constructor that initializes the default value of that type. When applying "new" keyword on value types this default constructor will be initiated.
So
int x = new int(); Is Equivalent to int x = 0;
int x = 10 is assigning a value 10 to either stack or heap depends on where this value type residing.