المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Can I make an entire class nullable?



C# Programming
01-26-2010, 01:20 PM
Hi all,

Let's say I have a class called myClass, the following code is fine:
myClass myInstance = new myClass();
if (myInstance == null)
//some code here that will obviously never be executed

However, the following code will not compile:
myClass myInstance;
if (myInstance == null)
//some code here that I do want to execute if the variable has not been instantiated
The above will give a compile error (Use of unassigned local variable 'myInstance')

My question is, can I make a class nullable so that the above code will compile and work?