Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference between Declaring a variable (as new) and then initializing it and direct initializing it?

Tags:

c#

.net

asp.net

I have seen people use a couple of different way of initializing arrays:

string[] Meal = new string[]{"Roast beef", "Salami", "Turkey", "Ham", "Pastrami"};

or another way, also called initializing is:

string[] Meats = {"Roast beef", "Salami", "Turkey", "Ham", "Pastrami" };

What is the best way, and what is the major difference between both ways (including memory allocation)?

like image 466
foo-baar Avatar asked Apr 19 '13 14:04

foo-baar


People also ask

What is the difference between declaring a variable and initializing a variable?

Declaration tells the compiler about the existence of an entity in the program and its location. When you declare a variable, you should also initialize it. Initialization is the process of assigning a value to the Variable. Every programming language has its own method of initializing the variable.

What is the difference between declaring and initializing a variable in JavaScript?

Declaration: Variable is registered using a given name within the corresponding scope (e.g., inside a function). Initialization: When you declare a variable it is automatically initialized, which means memory is allocated for the variable by the JavaScript engine.

What happens if I use a variable before initializing it to a value?

Notice that a variable that is not initialized does not have a defined value, hence it cannot be used until it is assigned such a value. If the variable has been declared but not initialized, we can use an assignment statement to assign it a value.

What does initializing a variable mean?

To initialize a variable is to give it a correct initial value. It's so important to do this that Java either initializes a variable for you, or it indicates an error has occurred, telling you to initialize a variable.


Video Answer


1 Answers

There is no difference in both cases. Compiler generates the same bytecode (newarr OpCode):

public static void Main()
{
   string[] Meal = new string[] { "Roast beef", "Salami", "Turkey", "Ham", "Pastrami"};
   string[] Meats = { "Roast beef", "Salami", "Turkey", "Ham", "Pastrami" };
}

MSIL:

.entrypoint
  // Code size       100 (0x64)
  .maxstack  3
  .locals init ([0] string[] Meal,
           [1] string[] Meats,
           [2] string[] CS$0$0000)
  IL_0000:  nop
  IL_0001:  ldc.i4.5
  IL_0002:  newarr     [mscorlib]System.String
  IL_0007:  stloc.2
  IL_0008:  ldloc.2
  IL_0009:  ldc.i4.0
  IL_000a:  ldstr      "Roast beef"
  IL_000f:  stelem.ref
  IL_0010:  ldloc.2
  IL_0011:  ldc.i4.1
  IL_0012:  ldstr      "Salami"
  IL_0017:  stelem.ref
  IL_0018:  ldloc.2
  IL_0019:  ldc.i4.2
  IL_001a:  ldstr      "Turkey"
  IL_001f:  stelem.ref
  IL_0020:  ldloc.2
  IL_0021:  ldc.i4.3
  IL_0022:  ldstr      "Ham"
  IL_0027:  stelem.ref
  IL_0028:  ldloc.2
  IL_0029:  ldc.i4.4
  IL_002a:  ldstr      "Pastrami"
  IL_002f:  stelem.ref
  IL_0030:  ldloc.2
  IL_0031:  stloc.0
  IL_0032:  ldc.i4.5
  IL_0033:  newarr     [mscorlib]System.String
  IL_0038:  stloc.2
  IL_0039:  ldloc.2
  IL_003a:  ldc.i4.0
  IL_003b:  ldstr      "Roast beef"
  IL_0040:  stelem.ref
  IL_0041:  ldloc.2
  IL_0042:  ldc.i4.1
  IL_0043:  ldstr      "Salami"
  IL_0048:  stelem.ref
  IL_0049:  ldloc.2
  IL_004a:  ldc.i4.2
  IL_004b:  ldstr      "Turkey"
  IL_0050:  stelem.ref
  IL_0051:  ldloc.2
  IL_0052:  ldc.i4.3
  IL_0053:  ldstr      "Ham"
  IL_0058:  stelem.ref
  IL_0059:  ldloc.2
  IL_005a:  ldc.i4.4
  IL_005b:  ldstr      "Pastrami"
  IL_0060:  stelem.ref
  IL_0061:  ldloc.2
  IL_0062:  stloc.1
  IL_0063:  ret
like image 156
Dzmitry Martavoi Avatar answered Oct 05 '22 10:10

Dzmitry Martavoi