c sharp

C# New Line

In C# programming, we can add a new line in the program. We don’t need to write any complex code for can do this using simple methods. In this tutorial, we are going to explain how we add a new line to our code. We are performing different examples in Ubuntu 20.04.

Methods to add a new line in C# Programming in Ubuntu 20.04

We have different ways to insert a new line in our C# program. Some are given below:

  • Write the new line in the next line using Console.WriteLine and put “;” at the end of each statement.
  • By using “\n”
  • By using “\x0A” or “xA”.
  • By using Console.WriteLine();

Now, we are going to use all these methods in our C# program. We will demonstrate different examples in which we are using these methods and will explain all these methods/ways in detail.

Example 1

Now, we are going to demonstrate example one here in where we add a new “Console.WriteLine” statement for printing the new line in the next line and put “;” at the end of each “Console.WriteLine” statement. We are performing these examples in Ubuntu 20.04 text editor. By using this text editor, you must save your file with the name of your own choice and put the “.cs” extension with this filename. Now, look at the example given in the image given below:

We start our C# program with the “using System” statement. For specifying the resources, we use this “using System” statement.  The “using System” is a library in C# programming. With the help of this statement, we will get all the required functions and classes which are used in this C# code. In the next line, we have the “using. System.IO” statement. It is a namespace containing the standard input and output types.

After this statement, we are using the “namespace” which is a keyword in C# programming. It is named “Program” in this code. For organizing our C# program elements, we use this “namespace” keyword. Now, we are going to declare a class with the name “newLine”. Inside this class, we invoke a method or function which is the “main” function. Here, “public static void Main (String [ ] args)” is used and it is the main method of this C# code.

This “main” method is “static” and “public” also. We can say that it is the entry point of our C# code. The first method which is called when we start this program is the “main” method. It must be used in our C# program. Inside this, we are going to print our line. For rendering the line on the output screen, we use the “Console.WriteLine” statement in our C# program.

Now, we use this statement to print one line which is “My C# Program..!!”, then we want to print the other line in the next line. For this, first we use the “Console.WriteLine” statement and put the above line in (“ ”) these brackets and quotations marks and also put a semicolon “;” at the end of this line. Then for printing the new line, we use the new “Console.WriteLine” statement and write the next line in this new statement with the same method as the above line.

After that, we close all the brackets. Now, we are going to show you the output of this code. We use commands for getting the output of the code. We use the “mcs” and “mono” commands and use “.cs” and “.exe” file extensions with the filename respectively. When this “mcs” command runs, it creates an executable file for us. After this, we use the “mono” command which executes our C# code.

In this image, you can see that it prints both lines on a separate line. After printing the first line, it adds a new line and prints the second line in the new line.

Example 2: By using “\n”

In this code, we are going to use “\n” for the new line in C# programming in Ubuntu 20.04. We explain the working of this “\n” in detail, in this given example.

The first line of our C# code is “using System” which is the starting point of our program and it is a library. Then, we have “using System.IO” and the namespace, which we discussed in detail in our previous code. The name of the “namespace” is “example” here. We are declaring a class with the name “CSharp” and invoked a “main” function inside this class.

“public static void Main (String [ ] args)” the “(String[ ] args)” is used for our arguments. Then, we have the “Console.WriteLine” statement. We have only one statement but we want to write the data in three lines using this one “Console.WriteLine” statement. So, for the new line, we use “\n”. We add this “\n” inside this “Console.WriteLine” statement where we want to add a new line and print the remaining text in the new line. It is always written inside the quotation marks “\n”. As here, we add “\n” after “WELCOME” so the next text will move to the next line. Each time when we use this “\n” our text which we write after this will move to the next line. We also show the output of the above code in this image

Just because we use “\n” inside the “Console.WriteLine” statement. When we use this “\n”, it moves to the next line and prints the text in the new line, as you see in this output.

Example 3: By using “\x0A” and “\xA”

We have another method for the new line in C# programming, which we are going to explore in this example. With the help of “\x0A” and “\xA” we can add a new line in our C# code.

We start our code with the same “using System” and “using System.IO”. The “namespace” is created with the name “CSharpProgram” and the class is declared as “class newLine”. We also invoked the “main” method. Then, we write first the “Console.WriteLine” statement and want to print this statement in three different lines. For this, we use “\x0A” in between this statement. When we use “\x0A” it will add a new line. Then we have another “Console.WriteLine” statement, which prints the data in the next line and has a semicolon at the end of each “Console.WriteLine” statement.

After this, we have a third “Console.WriteLine” statement and use “\xA” inside this statement, so the third statement will print in two lines. To better explain, look at the output image given below.

As you know, we only use three “Console.WriteLine” statements in our code but here in the output the text is printed in seven lines just because of these two “\x0A” and “\xA”. It adds a new line to our code and our one-line code is printed in three separate lines.

Conclusion

We explain the concept of the new line in C# programming in Ubuntu 20.04, in this tutorial. We explore different examples and methods to add the new line in our C# program. We demonstrate examples for you and explain all elements and methods which we use in our codes. We can do this by using simple elements, as you see we use in our examples.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.