Reputation: 304
What is the Difference in between 2 statements
int main()
{
A a = new A();
A a;
}
Please explain this two object creation statements.
Upvotes: 0
Views: 110
Reputation: 30695
The first command allocates a variable on the stack (A a
), and initializes on the heap (new A()
).
The second one only allocates the variable on the stack. It is not initialized, and therefore cannot be used until you assign it, either by a return value from a function or calling the class constructor.
Side Note: When your program is compiled and being run, it doesn't even remotely resemble the code you wrote. Variables are loaded right before you need them. The code you have above would roughly look like this:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 8 (0x8)
.maxstack 1
.locals init ([0] class DERP.Program/A a, // This code here declares a local
// variable: A a;
[1] class DERP.Program/A b) // another local variable: A b;
nop
newobj instance void DERP.Program/A::.ctor() // This is: new A()
stloc.1 // this loads the new A() we created
// into A b; (stloc.1 means to store
// the last item we created into the
// local variable at index [1]
ret
} // end of method Program::Main
It's okay if you don't completely understand what each of those commands are, but I've commented it to try and make it as straight forwards as possibly. Since we never assign
A a
to anything, it just sits on the local stack, hanging out, doing nothing. We can't use it because it doesn't point to any object.
There are many different reason in code you might see the declaration separated from the actual assignment.
For example, sometimes you need to declare a variable outside of a try {} catch {}
clause. Lets say your class takes a value in its constructor. You have a function that gets that data, say from a database. However, since its a DB call, you want to catch the exception, and if its thrown, initialize the class with a default value, instead of the returned value from the DB call.
Due to the way scoping works in C#, variables declared inside a try {} catch {}
are not accessible outside of it, hence you would need to declare the variable earlier in code before you initialize it.
Upvotes: 3
Reputation: 17556
A a = new A();
This will create a new object of type A
. The value of a
will be a new instance of A
.
A a;
This will just declare a as a type of A
. The value of a
will be null
.
Upvotes: 2
Reputation: 2453
Only your first statement actually creates a new object.The second one as others have mentioned just allocates a variable of type A on your stack.
the first one creates a new object on the manged heap stores the reference on it on the variable a of Type A
created on the stack.
Upvotes: 0
Reputation: 21366
In first statement it will create a new object instance of A class and it will be assigned to a variable
In second statement it is only creating a reference .
Upvotes: 0
Reputation: 76198
The first one creates a new insatnce and assigns it to variable a
.
The second one doesn't create any instance, so a
is null
.
Upvotes: 0