1
vote
3answers
210 views

Are the two ways of declaring variables the same?

Are the two ways of declaring variables shown below the same? int aa= 0, bb= 0, cc= 0, dd= 0; int aa= 0; int bb= 0; int cc= 0; int dd= 0;
1
vote
8answers
101 views

c# Variable uses [on hold]

I using c# winforms and wanted to know how it's better to write and why. if(txtName.Text == "John") ; or String name = txtName.Text if (name == "John") ; Edit: Thanks guys you helped me ...
0
votes
1answer
85 views

Declare different variables according to property value

I would like to know how can I create different variables according to the value of a property. Example: foreach (DataColumn column in entry.Columns) { if(column.ColumnName.Contains("weight") ...
2
votes
6answers
187 views

Using var for type declaration instead of explicitly setting interface type

I'm not sure if I'm overthinking this but in the past, I've done something like this when declaring a class: IMyService myService = new MyService(); Jumping into myService will take you to the ...
3
votes
2answers
765 views

Does moving variable declaration outside of a loop actually increase performance?

I'm writing very processor-intensive cryptography code (C#), so I'm looking for any performance gains, no matter how small. I've heard opinions both ways on this subject. Is there any performance ...
1
vote
5answers
1k views

In C#, is there way to define an enum and an instance of that enum at the same time?

Looking for a code optimization in c# that allows me to both define an enum and create a variable of that enum's type simultaniously: Before: enum State {State1, State2, State3}; State state ...
1
vote
3answers
737 views

C#: variable declaration inside loop

Is the following code correct? foreach (int i in MyList) { MyObject m; } Can you declare a variable more than once?
33
votes
5answers
6k views

Why doesn't C# let you declare multiple variables using var?

Given the following: // not a problem int i = 2, j = 3; so it surprises me that this: // compiler error: Implicitly-typed local variables cannot have multiple declarators var i = 2, j = 3; ...
32
votes
18answers
2k views

Use of “var” type in variable declaration

Our internal audit suggests us to use explicit variable type declaration instead of using the keyword var. They argue that using of var "may lead to unexpected results in some cases". I am not aware ...
5
votes
4answers
217 views

will declaring variables inside sub-blocks improve performance?

In C#, would there be any difference in performance when comparing the following THREE alternatives? ONE void ONE(int x) { if (x == 10) { int y = 20; int z = 30; // do other stuff } ...
6
votes
6answers
789 views

Should variable declarations always be placed outside of a loop?

Is it better to declare a variable used in a loop outside of the loop rather then inside? Sometimes I see examples where a variable is declared inside the loop. Does this effectively cause the program ...
2
votes
3answers
152 views

Does the order of readonly variable declarations guarantee the order in which the values are set?

Say I were to have a few readonly variables for filepaths, would I be able to guarantee the order in which the values are assigned based on the order of declaration? e.g. static readonly string ...
2
votes
2answers
357 views

Lambda closure or class level variable?

Just a general question about what the best practice is: public void Foo() { int x = 5; myControl.Click += (o, e) => { x = 6; }; } Notice, I'm using the x variable inside ...