Need help understanding the following...

jimmythefloyd

Newcomer
Joined
Dec 24, 2010
Messages
4
Location
Madison, WI
I am new to programming. Things have been going fairly well with these entry level concepts, but I am having a hard time understanding this one. My hope is that someone can explain it in a way that makes it click for me. The following is the whole project. My specific difficulty lies with the "while (line != null)" and the "if (line =! null)" sections.

In English, we are saying, do this block of code while the variable "line" is not empty. My problem is, we declared the variable line = ""; isn't an empty string "" equal to null? So why does this block of code fire? Doesn't that make it empty, so it's not "not empty" when it tests the condition? I can't wrap my arms around it. I can see that it does.. but I don't understand why it does.

When we go underneath it to the "if" block, we are also saying that if (line != null) do some array stuff and then print it to screen. That part also makes little sense to me, the stream has gotten to the end of the file, it finds a null character, but it doesn't change the value of the variable to empty..
I am obviously crossing some wires here, but I'm not sure where.

Code:
namespace HomeworkDay2
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader myReader = new StreamReader("DecodeThis.txt");
            string line = "";

            while (line != null)
            {
                line = myReader.ReadLine();
                
                if (line != null)
                {
                    char[] myCharArray = line.ToCharArray();
                    Array.Reverse(myCharArray);
                    Console.WriteLine(myCharArray);
                    
                }
                                      
            }

            Console.ReadLine();
        }
  
    }
}
 
Just like any other reference type, a string can be null. There is, in fact, a difference between a null string and an empty string.

An empty string is a string object that has a length of zero. It might not contain anything, but it is still an object. You can still access it.
Code:
[COLOR="Green"]// Assuming line is empty...[/COLOR]
int len = line.Length;[COLOR="Green"] // Returns zero[/COLOR]
char[] chars = line.ToCharArray(); [COLOR="Green"]// Returns an empty array.[/COLOR]
string padded = line.PadLeft(2); [COLOR="Green"]// Returns a string containing two spaces[/COLOR]
A string variable can also be null. That means that the variable doesn't point to any string object (not even an empty object). Although this seems like a pointless distinction, it makes quite a difference in what happens with our code.
Code:
[COLOR="Green"]// Assuming line is NULL...[/COLOR]
int len = line.Length;[COLOR="Green"] // Throws NullReferenceException[/COLOR]
char[] chars = line.ToCharArray(); [COLOR="Green"]// Throws NullReferenceException[/COLOR]
string padded = line.PadLeft(2); [COLOR="Green"]// Throws NullReferenceException[/COLOR]


There is a school of thought that believes variables should never be allowed to be null unless you declare it as nullable. Accidentally using a null variable can cause so many headaches, and in many cases there is no advantage to having null variables. On the flip side, being able to have null as a default value for variables has performance advantages and simplifies some scenarios.

There are many cases where you need to distinguish between an empty string and no string at all. The ReadLine function returns an empty string when it encounters an empty line in a file, and it returns null when it reaches the end of the file. In this case, the distinction is very important.
 
Thanks dude,
I think I get it. Is it really just this simple?
Code:
1. string cats;
NULL
Code:
 2. string cats =” “;
EMPTY
Code:
3. string cats = "hates dogs";
NOT NULL OR EMPTY
 
Back
Top