jimmythefloyd
Newcomer
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.
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();
}
}
}