16

I want to know what exactly happens inside when we declare a variable, like this:

string tr;

string tr = null;

While debugging, I noticed for both values that it was showing null only. But when using ref tr without initializing null it will give error while the second line doesn't.

Please help me to know about it in depth

2
  • You need an Explicit initialisation to use variable as an ref parameter, even null assignmenet is fine.that's not happening in your first variable declaration. Commented Mar 18, 2014 at 15:02
  • It is just to make sure, you did not forget to assign a value to the variable -> if you would forget it, you could run into problems Commented Mar 18, 2014 at 15:04

3 Answers 3

9

Your first statement is just declaration and your second statement is Declaration + Initialization.

string tr; // Just Declaration

string tr=null; //Declaration + Initialization. 

You may get compile time errors if you try to use tr with just declaration. (first case) for example:

string tr; // Just Declaration
if (tr == "")   //Use of unassigned local variable
{
}

Same error will be generated for ref keyword, which requires the field to be explicitly assigned something (Declaration + Initialization). With out keyword though, you can use just declaration, but the method would be responsible for making sure that some value is assigned to the out parameter.

Sign up to request clarification or add additional context in comments.

6 Comments

but this error wont be there in case of int ,long.That is we can use a variable declaring like int i; without initializing ,where compiler will assign zero by default. but why not not in case of string.
@keerti_h, error would be same, try int i; if (i == 0){} or int i; Console.WriteLine(i); , you would get the same error, also try it with a method with ref keyword. So the error will be there.
@keerti_h, although string is a reference type whereas the others you mentioned are value types, but that will not change the error.
,i did try that with ref, but i declared that as global that's why it assigned Zero probably. i will try with local also
@keerti_h, that is a different thing, By global I guess you meant at class level, in that case it is a field not a local method variable and fields are assigned default value at the time of object instantiation (if they are just declared and not initialized with any value).
|
6

While debugging, I noticed for both values that it was showing null only

That's right because string is a reference type and the default value for all reference types is null.

But C# compiler doesn't allow the use of uninitialized variables.

Your first example is just a variable declaration. But your second one is variable initialization.

That's why if you write;

string tr;
Console.WriteLine(tr);

you get compiler error like;

Use of unassigned local variable 'tr'

From ref (C# Reference)

An argument that is passed to a ref parameter must be initialized before it is passed.

Although variables passed as out arguments don't have to be initialized before being passed.

Comments

4

The compliler requires you to explicitly set the null (or any other) value to the variable. It wants no misuderstandings and less runtime errors in your program so it gently infers that you might not initialized a variable before using it.

The exception is using a method with out parameter (which itself guarantees initialization) as uninitialized variable.

1 Comment

While it is Visual Studio that reports the errors to the users, it's the compiler's requirements and not VS'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.