Search code examples
c#classisset

How to tell whether a variable has been initialized in C#?


I know this is a dumb question and I guess it must have been asked before. However I am unable to find an answer to my question.

Here is some sample code (which of course does not compile) to outline my problem:

class test
{
     int[] val1;
     string val2;

     static bool somefunction(test x, test y)
     {
         dosomestuff()

         test result;

         while(result is nothing)
         {
              if(somecondition){result=new test(something);}
         }
     }
}

The problem which I have is in the following line:

while(result is nothing)

This is the syntax from VB, which of course is not what the C# compiler accepts. Could somebody tell me how to resolve the problem?


Solution

  • The syntax you are looking for is:

    while (result == null)
    

    You also have to set result = null; to start with also