Bruce is correct. This seems to be a common question in the newsgroups so
I'll try to clarify the rules about definite assignment. I'm pretty
confident these are correct. The formal rules are in the C# specification,
section 5.3
1. Local variables of any type must be initialized before they are first
used. A compiler error occurs if they are not.
public void foo ( )
{
int nVar;
if (nVar == 0) //Compiler error
}
2. Member variables of a class are default initialized when the instance is
created. For references it is null. For all other types it is the
equivalent of 0. Boolean is false. This is unless you use an initialization
expression in the declaration in which case it will have the expression's
value. Even then however I think it will still first be default initialized.
public class Test
{
int m_nVar; //Initialized to 0 when the instance is
created
int m_nVar2 = 4; //Initialized to 4 when the instance is created
}
3. Member variables in a struct are always default initialized. You must
assign all variables in a struct a value within any custom constructor you
create prior to invoking any property or method of the struct. The compiler
will generate an error if you don't.
public struct TestStruct
{
public TestStruct ( bool dummy )
{
if (m_nVar == 0) //Error, must initialize all vars first
}
int m_nVar; //Default initialized
int m_nVar2 = 4; //Error, struct vars can't be initialized
}
4. Static member variables are initialized prior to their first reference.
However if the owning type has a static constructor then the static members
are initialized when the static constructor is called.
public class TestStatic
{
static int m_nVar; //Initialized prior to referencing
m_nVar
}
public class TestStatic2
{
static TestStatic2 ( ) { }
static int m_nVar; //Initialized when static constructor
called
}
5. Out parameters are not default initialized. They must be assigned a
value prior to the function returning.
public void foo ( out string parm )
{
return; //Error, must initialize out parameter
}
There is one special case where you can actually access a variable before it
is initialized. It is a boundary case that you should really avoid anyway.
I don't remember the details exactly but it has something to do with
initializing member variables with other member variables before they are
truly initialized. In this rare case you'll see a member variable's value
before it is truly initialized. Even in this case however I think it is
going to be default initialized.
public class TestWeird
{
public static int m_nVar = m_nVar2 + 2; // Value is 2
public static int m_nVar2 = m_nVar + 2; // Value is 2
}
Hope this clarifies,
Michael Taylor, MCP, MCAD - 6/29/05
"Bruce Wood" wrote:
First of all, if it's a local variable in a method, like
public void MyFunc()
{
bool unInitialized;
if (unInitialized) { ... }
}
this will not compile. The compiler will complain that it is
uninitialized.
That said, it is possible to create scenarios in which you can get at
uninitialized _properties_ and the compiler won't complain. In this
situation, every value type in the Framework has a default value to
which it is guaranteed to be initialized. For reference types (and
strings, which are also reference types) it is null. For numeric types,
it is 0. I believe that for booleans it is false, although I'm open to
being corrected on that.