1

I would like to know the differences between these two ways of declaring variables.

Type 1

private string procedure_add = "";
private string procedure_update = "";
private string procedure_delete = "";

Type 2

private string procedure_add = "", procedure_update = "", procedure_delete = "";

Does this give the same effect?. Is the memory consumption the same?

Emond
  • 50,210
  • 11
  • 84
  • 115
Sagotharan
  • 2,586
  • 16
  • 73
  • 117

5 Answers5

4

There is no any difference. it's all about the accessibility. the way how the code looks. suppose if you have 10000+ line of code, while editing you may get stumped by identifying the "," in declaration .

  1. if you have one or two variable, then declare it in a single line
  2. writing each declaration in separate line will look code cleaner, and better to debug.

this method which i prefer to use.

private string yourVar = String.Empty;
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
1

There is no any difference. Just coding style.

EDIT

As @Aphelion mantioned in first case you can modify accessibility. From the code generation point of view the both version produce exactly the same IL

MyClass..ctor:
IL_0000:  ldarg.0     
IL_0001:  ldstr       ""
IL_0006:  stfld       UserQuery+MyClass.procedure_add
IL_000B:  ldarg.0     
IL_000C:  ldstr       ""
IL_0011:  stfld       UserQuery+MyClass.procedure_update
IL_0016:  ldarg.0     
IL_0017:  ldstr       ""
IL_001C:  stfld       UserQuery+MyClass.procedure_delete
IL_0021:  ldarg.0     
IL_0022:  call        System.Object..ctor
IL_0027:  ret    
Tigran
  • 61,654
  • 8
  • 86
  • 123
0

There is no difference, the one is just a short-hand.

janhartmann
  • 14,713
  • 15
  • 82
  • 138
0

I think no different, from my understanding second type is to minimize the line of code.

Siti
  • 163
  • 5
  • 20
0

In addition to the overall "They are the same, but differ in readability" I would like to add 3 remarks:

  1. In Visual Basic 6 (and earlier) Dim x , y as Integer would result in y being an Integer and x a Variant
  2. In C int *ip, i; would result in ip being a pointer to an int and i to be an int.
  3. In unsafe C# code int *ip, i; will result in ip being a pointer to int and i ALSO being a pointer to int.
Emond
  • 50,210
  • 11
  • 84
  • 115