4

Possible Duplicate:
C# variable initializations vs assignment

Just like in the title, could someone please explain what is the difference between Initialization and Assignment in C#? I'm preparing for a test and I wanted to know what's the best way to answer this type of question. Thanks

Cheers, n1te

Community
  • 1
  • 1
n1te
  • 945
  • 3
  • 11
  • 20

2 Answers2

7

When you initialize a variable you're declaring it into existence.

PlasticCup mySippyCup = new PlasticCup();

When you assign, you're just saying "this water" goes into "this cup". The cup already exists.

mySippyCup = new PlasticCup();
Only Bolivian Here
  • 35,719
  • 63
  • 161
  • 257
3

Initialization is assigning value while declaring the variable - int a = 1

Assignment is just assigning value to a variable - a = 1

By this definition, some say all initializations are assignments, but all assignments are not initializations.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • Given your example: int a //declaration, a = 1 //initialization, a = 2 //assignment. Why are those 3 steps required, why not just declare and assign a variable and skip initialization? – n1te Sep 18 '11 at 02:58