Click here to Skip to main content
15,885,435 members
Home / Discussions / C#
   

C#

 
AnswerRe: Variable declaration. Pin
Eddy Vluggen17-Oct-12 22:40
professionalEddy Vluggen17-Oct-12 22:40 
AnswerRe: Variable declaration. Pin
V.17-Oct-12 22:41
professionalV.17-Oct-12 22:41 
GeneralRe: Variable declaration. Pin
Sibasisjena17-Oct-12 22:51
professionalSibasisjena17-Oct-12 22:51 
GeneralRe: Variable declaration. Pin
V.17-Oct-12 22:58
professionalV.17-Oct-12 22:58 
AnswerRe: Variable declaration. Pin
Pete O'Hanlon17-Oct-12 22:49
mvePete O'Hanlon17-Oct-12 22:49 
AnswerRe: Variable declaration. Pin
Shanmuga Hariharan17-Oct-12 22:55
Shanmuga Hariharan17-Oct-12 22:55 
GeneralRe: Variable declaration. Pin
Pete O'Hanlon17-Oct-12 23:22
mvePete O'Hanlon17-Oct-12 23:22 
AnswerRe: Variable declaration. Pin
BobJanova18-Oct-12 1:13
BobJanova18-Oct-12 1:13 
Variables should always be defined in the tightest scope that they are needed for. This is an extension of the principle that says you should have class instance variables not static or global ones, and that you should have function/procedure local variables not instance ones.

So if the variable's only used inside the block, declare it inside. That's true whether it's a try, an if, a while or whatever.

You can even scope sections of code simply to create a localisation region, e.g.

void SomeMethod(){
 {
  int a = 1;
  Console.WriteLine(a); // 1
 }
 {
  string a = "test";
  Console.WriteLine(a); // test
 }
}


That is not generally something that people recommend, though.
GeneralRe: Variable declaration. Pin
Pete O'Hanlon18-Oct-12 1:22
mvePete O'Hanlon18-Oct-12 1:22 
GeneralRe: Variable declaration. Pin
BobJanova18-Oct-12 1:36
BobJanova18-Oct-12 1:36 
GeneralRe: Variable declaration. Pin
DaveyM6918-Oct-12 22:20
professionalDaveyM6918-Oct-12 22:20 
QuestionC# setup to a sql server Pin
sc steinhayse17-Oct-12 19:13
sc steinhayse17-Oct-12 19:13 
AnswerRe: C# setup to a sql server Pin
code-frog18-Oct-12 17:57
professionalcode-frog18-Oct-12 17:57 
QuestionText to Code 128 Pin
be05x517-Oct-12 10:06
be05x517-Oct-12 10:06 
AnswerRe: Text to Code 128 Pin
fjdiewornncalwe17-Oct-12 10:21
professionalfjdiewornncalwe17-Oct-12 10:21 
QuestionConceptual/lessical question (basic) Pin
PozzaVecia17-Oct-12 9:57
PozzaVecia17-Oct-12 9:57 
AnswerRe: Conceptual/lessical question (basic) Pin
fjdiewornncalwe17-Oct-12 10:25
professionalfjdiewornncalwe17-Oct-12 10:25 
GeneralRe: Conceptual/lessical question (basic) Pin
PozzaVecia17-Oct-12 10:36
PozzaVecia17-Oct-12 10:36 
GeneralRe: Conceptual/lessical question (basic) Pin
fjdiewornncalwe17-Oct-12 10:41
professionalfjdiewornncalwe17-Oct-12 10:41 
GeneralRe: Conceptual/lessical question (basic) Pin
PozzaVecia17-Oct-12 10:53
PozzaVecia17-Oct-12 10:53 
GeneralRe: Conceptual/lessical question (basic) Pin
Ravi Bhavnani18-Oct-12 3:45
professionalRavi Bhavnani18-Oct-12 3:45 
AnswerRe: Conceptual/lessical question (basic) Pin
V.17-Oct-12 21:48
professionalV.17-Oct-12 21:48 
AnswerRe: Conceptual/lessical question (basic) Pin
Orjan Westin19-Oct-12 4:38
professionalOrjan Westin19-Oct-12 4:38 
QuestionC# console log file Pin
sc steinhayse17-Oct-12 8:08
sc steinhayse17-Oct-12 8:08 
AnswerRe: C# console log file Pin
fjdiewornncalwe17-Oct-12 9:17
professionalfjdiewornncalwe17-Oct-12 9:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.