I am a C# developer who has been working with the language for a long time.
I am wondering why the langauge allows nested/inner scopes for functions.
Here is an example of what I mean:
public int InnerScopeMethod()
{
int i = 10;
//Inner Scope
{
int j = 100;//j will not work outside of the scope.
i *= j;
}//Scope Ends Here
//j = 10 /// This Will Cause Error.
return i;
}
As you can see the method body has an anonymous or unnamed scope and it is legal in C# 4.
I want to know why is it here? Is this just for providing small scopes for variables or it has other uses?
Any Help will be appreciated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…