I am new to C# file handling, and I am making a very simple program. The code is as follows:
class MainClass
{
public static void Main()
{
var sw = new StreamWriter("C:\\Users\\Punit\\Desktop\\hello.txt");
sw.Write("HelloWorld" +Environment.NewLine);
sw.Write("ByeWorld");
sw.Close();
Console.ReadLine();
}
}
The above code produces the following expected result in the text file:
HelloWorld
ByeWorld
I also wrote somewhat modified version of the code like this:
class MainClass
{
public static void Main()
{
var sw = new StreamWriter("C:\\Users\\Punit\\Desktop\\hello.txt");
sw.Write("HelloWorld\n");
sw.Write("ByeWorld");
sw.Close();
Console.ReadLine();
}
}
Here instead of using the
Environment.Newline
I directly added "\n" to the "HelloWorld" line. This produced the following output (int the text file):
HelloWorldByeWorld
My question is that why is the second piece of code not working? (Not producing the newline in the text file)