Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi,

I declare a variable of type int in the first case and in the second case i instantiate the same,Below are the 2 cases :

case1:

int a;
a=10;

Now I instantiate a variable of type int

Case 2:

int a =new int();
a=10;



what is the difference between the above two cases,what exactly happens internally.


Regards
Chaithu
Posted
Comments
CodeHawkz 3-May-11 4:15am    
Please, refer to the community content in the below link. Might help you

http://msdn.microsoft.com/en-us/library/s1ax56ch

I believe that in case 1, the variable is declared without a value and then assigned '10'.

In case 2 the variables is declared with it's default value (which is '0') and then assigned '10'.

But that's just my 2 cents.
 
Share this answer
 
Comments
Albin Abel 3-May-11 5:46am    
Right I get your 2 cents and sell a vote of 5
To explain your question, look at the example below.

C#
struct Person
{
    public Person(int age)
    {
        this.Age = age + 10;
    }
    public int Age;
}


And then access it like

C#
Person person =new Person();
person.Age = 10;

Person person1 = new Person(10);


What you expect Person's age is 10, but Person1's value will be 20. Using the new key word you can call a specific constructor. For your second case you are calling a default construtor which assign a 0 to the variable and then you are assigning value to it. In the first case the int will accept a compatible value which implicitly to the value type.

For the int, the both ways you are calling are same, because you are using simply a default constructor.

Hope this helps.

Note: value types are different from reference types In the above example if the person is a class then

Person1= Person; Person1.Age=30; then Person.Age also equals 30. But in case of value type Person.Age will have the original value. Value types stores on the stack and a new variable will be in a new memory address. Reference variable pointers are in the stack which points to an address in the heap. So two variable can hold a common address in the heap.

Good luck
 
Share this answer
 
v2
Comments
Sandeep Mewara 3-May-11 2:48am    
Straight away 5!
Albin Abel 3-May-11 5:54am    
Thanks S Mewara. How you prefer us to call you Sandeep / Mewara /Sandeep Mewara or S Mewara
Sandeep Mewara 3-May-11 7:22am    
Sandeep is fine as such! Calling either of the rest or just SM too would do! :)
Ashishmau 3-May-11 2:52am    
Perfect my5
Albin Abel 3-May-11 5:52am    
Thanks Ashismau
 
Share this answer
 
Comments
Albin Abel 3-May-11 5:45am    
In your link the discussion is about a wrapper object on the primitive type. However my 5 for add on information to OP.
Apart from what Albein already said,
When you use a new expression with a structure, the object is intialised to Zero (and nulls if the structure include fields that were reference types).

So when you write this -
C#
int a = new int();

a is implicitly initialised to 0.
Consider this example,
C#
int a;
a = a + 10;

This will give you a compiler error - use of unassigned local variable 'a'

But for the code below,
C#
int a = new int();
a = a + 10;


It works fine. As a was by default assigned 0.

Hope this helps as well!
 
Share this answer
 
Comments
Albin Abel 3-May-11 5:43am    
Right. My 5
Ankur\m/ 3-May-11 5:46am    
Thanks, Albin!

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