Hi guys ))
Please tell me - how to check that a variable is initialized ?
Thanks in advance )

Recommended Answers

All 9 Replies

It depends on the type and scope of the variable. Can you be more specific?

commented: +++ +1

yes)I need to check string and int types ))

int types are always initialized (to zero). You can check if a string type is null ( if (myString == null) { // Not initialized .

In general, a null reference means the variable doesn't have an object.

>int types are always initialized (to zero).
This misconception is why I asked for a more specific question. As an example, local variables are not automatically initialized, which means there's no way to check them without relying on compiler warnings/errors.

static void Main(string[] args)
{
    int i; // Not initialized (value type)
    string s; // Not initialized (reference type)

    // ...
}

Also, you should use the static String.IsNullOrEmpty method to check for uninitialised strings.

Also, you should use the static String.IsNullOrEmpty method to check for uninitialised strings.

but what is benefit of using static string?

I meant the static method of the String class IsNullOrEmpty , not that the string itself is static. This static method checks whether the string is unitialised or initialised to String.Empty .

commented: +++ +1

I meant the static method of the String class IsNullOrEmpty , not that the string itself is static. This static method checks whether the string is unitialised or initialised to String.Empty .

hmm got it..

commented: ++ +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.