tags:

views:

118

answers:

7

Hi there. My question is: can I check whether a variable (string or int/double type) or an array (string or int/double type) is initialized in C#?

Thanks in advance.

A: 

No. However, you will have a compiler error if it is a local variable. If it is a class member then it is automatically initialized to the default (0 for ints, null for objects, etc.)

tster
If the instances are created dynamically, and the null only happens in certain cases, then the compiler wont pick it up. If you don't include it in your test cases then you may end up with an error.
Lily
@Lily, what are you talking about "instances are create dynamically" and "null only happens in certain cases"? Are you talking about static variables, or instance variables? What happens if the "compiler [doesn't] pick it up"? Do you get a run time error if you try and use the variable?
tster
+2  A: 

You are guaranteed some sort of initialization. For any static or instance members, all variables are automatically initialized when the type or instance is constructed, either explicitly or implicitly (in which case default(Type) is the value, so 0 for numeric types, null for strings and other reference types, etc.).

For local variables, they cannot be used before declaration, so if you can check it, it's been initialized.

Adam Robinson
Thanks for your post. However, my problem is that the possible value of the variable could be 0 which is same as the initiated value.
@user208080: The point is that the variable *is initialized*. If you mean *user-initialized*, then no, there is no way to check that unless you keep track of it yourself.
Adam Robinson
If you want to have the possibility to check if such a value is set for primitive types like Int32, use the nullable type of it. Then you will have a null reference as long as your code has not set it to another value.
HCL
A: 

tongue in cheek, but accurate answer

Scan your source code and find all usages and declarations of the variable to verify that it is initialized either at declaration, or else somewhere guaranteed before using it.

John Weldon
...or you could just try to compile it, as the compiler does this work for you.
Adam Robinson
+1 @Adam; makes sense.
John Weldon
There are moments where the compiler wont detect it, unless you go through every path in your code. In these cases a check done programatically is necessary. For instance, an array of objects, where not all the objects are instantiated. You do a for loop and call each object... You do the math
Lily
@Lily: No, there are no instances where the compiler will fail to detect an uninitialized object. There *are* cases where it will say it's uninitialized when it is, but never the opposite. As pointed out earlier, you're equating "non-null" with "uninitialized". These are not the same.
Adam Robinson
+1  A: 

Try This, :

If var = NULL Then
MsgBox ('Not initialized')
End If
dotNET
A: 

Yes you can.

For types that require instances (string or arrays, as you asked), you can verify if they are null.

You could do this many ways but one way is :

if (myObject == null)
{
    //initialize it here
}

Primitive data types do not require instancing. For example:

int i;

wont be equal to null, it will be equal to 0.

Lily
Actually, you'll get a compile-time error if you try to access this (the `int i;`) since c# *requires* that you initialize all variables before accessing them.
Donnie
"uninitialized" is *not* the same as "non-null".
Adam Robinson
+1  A: 

C# requires that all variables be initialized to some value before you read them.

The code block:

int i;
if(i == 0)
{
  // something...
}

Will generate a compile-time error because you're trying to access the value of i before assigning it. This also applies to objects (although you can initialize them to null to begin with).

If you are wanting to know if you have modified from your initial assignment, then no, there is no way of telling that directly unless the initial assignment is to a sentinel value that will not be repeated by a subsequent assignment. If this is not the case you will need an extra bool to track.

Donnie
A: 

Thank you guys. I got it.

Good to hear, though please don't post comments to you question as answers.
Adam Robinson