jimmythefloyd Posted April 10, 2011 Posted April 10, 2011 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. 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(); } } } Quote
Leaders snarfblam Posted April 10, 2011 Leaders Posted April 10, 2011 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. [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. [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. Quote [sIGPIC]e[/sIGPIC]
jimmythefloyd Posted April 10, 2011 Author Posted April 10, 2011 Thanks dude, I think I get it. Is it really just this simple? 1. string cats; NULL 2. string cats =� �; EMPTY 3. string cats = "hates dogs"; NOT NULL OR EMPTY Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.