A code structure question

  • Thread starter Thread starter Zoury
  • Start date Start date
Z

Zoury

Hi there!

I'm Coming from a classic VB background. In VB *any* declaration in a given
scope are made when the given scope is "entered".

What about the following C# code snippet?
//***
private void MyFunc(string s)
{

if (s.Length == 0) {return;}

string s2 = "a string";
int i = 0;

// code code code...

}
//***

Would that prevent the variables from being declared if not necessary or is
it the same as :
//***
private void MyFunc(string s)
{

string s2 = "a string";
int i = 0;

if (s.Length == 0) {return;}

// code code code...

}
//***


What's the best pratice? and for those who knows... is VB.NET working the
same way as C#?
 
I'm Coming from a classic VB background. In VB *any* declaration in a given
scope are made when the given scope is "entered".
What about the following C# code snippet?

Would that prevent the variables from being declared if not necessary or is
it the same as :

<snip>

I believe there'll be no difference in stack size - but when the
initializers are run *will* depend on where they're declared. I would
be surprised if that were different in VB.NET, actually, given that the
initializers could well have a significant effect on what happens.
What's the best pratice? and for those who knows...

I personally believe it's best to declare variables as late as you can,
and in the innermost scope that you can - in other words, closest to
use.
 
Back
Top