TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Environment.NewLine

These C# programs use the Environment.NewLine property. NewLine equals \r\n.

Environment.NewLine.

A newline on Windows equals \r\n. To add newlines in a C# string, there are a couple options. There is a constant from the base class library and the actual string literal. We review these newline options.

Environment

Example. Here we need to create a string with a line break in the middle of it, which will form a two-line string. We will use the Environment.NewLine constant for this, which is defined by the .NET Framework and could vary by platform.

Based on:

.NET 4.5

C# program that uses Environment.NewLine

using System;

class Program
{
    static void Main()
    {
	//
	// Use string concat to combine two literals
	// with the newline constant.
	//
	string s = "First line" +
	    Environment.NewLine +
	    "Second line";
	Console.Write(s);
	Console.Read();
    }
}

Output

First line
Second line

Example 2. Next, we see that you can simply use a string literal to add a line break. You don't need to use Environment.NewLine as in the first example. You can use the "\r\n" constant directly—this can actually be a better choice.

Note: This constant changes the MSIL generated. It is effective on Windows platforms and is, in practical aspects, the same.

C# program that uses newline constant

using System;

class Program
{
    static void Main()
    {
	//
	// Use "\r\n" string in string concat.
	//
	string s = "One line" +
	    "\r\n" +
	    "Another line";
	Console.Write(s);
	Console.Read();
    }
}

Output

One line
Another line

Implementation. As we noted before, Environment.NewLine is simply a constant property in .NET, and it could be tied to the current executing environment or platform. But almost all C# program deployments use Windows.

Call instruction

L_000b: call string [mscorlib]System.Environment::get_NewLine()

Implementation of get_NewLine

.method public hidebysig specialname static string get_NewLine()
    cil managed
{
    .maxstack 8
    L_0000: ldstr "\r\n"
    L_0005: ret
}

The above IL is generated when you call Environment.NewLine. The "get_" part simply indicates that the value is fetched from a property. This is inlined so has no performance penalty.

ILProperty

Next, we look into get_NewLine and its implementation. You can see it simply returns the "\r\n" string literal. You can find more information on the usage and implementation of string literals in the C# language on this website.

String Literal

Note: It is possible that a new character set could have another constant as a line break.

Summary. We used the Environment.NewLine constant in the C# language. Some developers find the Environment.NewLine constant has more clarity in their code. But it is unlikely that we will forget what \r\n or \n mean, so the clarity is no better.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf