Sagotharan
Sagotharan

Reputation: 2626

What is difference between single and multiple declarations in one line?

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?

Upvotes: 1

Views: 516

Answers (5)

Ravi Gadag
Ravi Gadag

Reputation: 15861

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;

Upvotes: 4

Emond
Emond

Reputation: 50672

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.

Upvotes: 0

Siti
Siti

Reputation: 163

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

Upvotes: 0

janhartmann
janhartmann

Reputation: 15003

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

Upvotes: 0

Tigran
Tigran

Reputation: 62246

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    

Upvotes: 1

Related Questions