What happens if we refer a variable which in not initialized in C#

A) Compiler throws warning message
B) Compiler throws error message
C) Compiles the application with out any problem
Explanation: Variable needs to be initialized before use

Showing Answers 1 - 14 of 14 Answers

ishita

  • Jan 3rd, 2006
 

Ans-B

  Was this answer useful?  Yes

nagi

  • Feb 19th, 2006
 

Class level variables need not be initialized...They are automatically initialized to default values

So (c) is right answer. Hope u ppl agree

  Was this answer useful?  Yes

Maruti Tathe

  • Mar 11th, 2006
 

Sisira,open ur book and read some basics. Why wasting time. Misguiding others.....

Sreerenj

  • Mar 20th, 2006
 

If the variable is class variable then it will be assigned the default value(in case of integer it is 0). But if the variable is a local variable (like variable declared inside a function) and not initialized then the compiler will throw an error message.

The question should be reframed.

  Was this answer useful?  Yes

Raju

  • Mar 27th, 2006
 

Yes I agreee with u

  Was this answer useful?  Yes

Vishal

  • Aug 6th, 2006
 

If i am right then if the variable is a class variable then it will be initialized by the default constructor or custom constructor provided by the class itself.Now, if the variable we are referencing is a local variable within the Main() mehtod and is referenced without being initialized then at the compilation time, the compiler throws warning message. In other words compilation will be successful with a warning message. Now when you run the program, i think it will throw the run time check failure error. It will show the garbage values.Let me know if i am wrong. Thanks,Vishal.

  Was this answer useful?  Yes

Gourab

  • Nov 2nd, 2006
 

Sisira is right. it will generate compilation error.

int i;

MessageBox.Show(i.ToString());

Error Code : CS0165

Error Message: Use of Un-assigned local variable.

  Was this answer useful?  Yes

John Jiang

  • Jul 4th, 2008
 

There is no veriable in a class only fields or properties. when we say variables that means they are in a method. so it will throw a error!!!

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions