wkempf wrote:

ElucidWeb wrote:

It will throw a compiler error if you dont give the variable a value during declaration.  But the compiler may miss this depending on how you pass it into the method.

In any case you should always declare your variables as null when using them, why wouldnt you?

string s = null;
if (String.IsNullOrEmpty(s))
   Console.WriteLine("String is null or empty.");

Console.ReadLine();



How do you "throw a compiler error"?

And AFAIK, there's no way the compiler can miss an error here, at least in C#.  Pass it normally, or as a ref, and it will result in a compilation error.  Pass it as an out, and it will result in a compiler error if the called function references the variable prior to assigning to it.  There are no scenarios where you can reference an undefined variable in C#.

And I'm not certain how any of this is relevant to the original post mistakenly thinking IsNullOrEmpty() would throw an exception?


Ok it will generate a compile time error?  Is that better wording for you?  Are we on slashdot?  oh were not..hmm

Aslo here is how you would get around the compiler not generating  an error at compile time:

 class Program
    {
        public static void Main()
        {
            Test test = new Test();
            if (String.IsNullOrEmpty(test.myString))
                Console.WriteLine("String is null or empty.");

            Console.ReadLine();
        }
    }

    class Test
    {
        public string myString;

        public void Run()
        {
            myString = "test";
        }
    }

How is what I posted not relevant to the initial poster, he was saying this little gem (String.IsNullOrEmpty) throws an exception and was wondering why it does that, well it doesnt if you use it correctly as I was demonstrating!!

BTW notice how I never actually use Run?  Well I do that to fool the compiler... lol