pcbway




First Code in C# Tutorials, c# console, c# tutorials, console writeline, console readlineHello everyone, I hope you all are fine and having fun with your lives. Today, we are gonna design our First Code in C#. In the previous tutorial, we have seen a basic Introduction to C#, and have also created our first project.

We are gonna use the same project, created in last lecture and we will add some code in our Console application. So, let’s get started with our first code in C# Tutorial series.

First Code in C# Tutorial

  • In the first lecture, we have created our first project in C# that was printing “Hello World!” on the Console, shown in the below figure:

Introduction to c sharp, c# basics, basics of c#, introduction to c sharp, basics c sharp

  • Solution Explorer contains all files of your project and Program.cs is the actual code file in which I am going to write our code.
  • So, let’s add the below code in our Main function of Program.cs File:
Console.WriteLine("What are you learning ???");
string subject = Console.ReadLine();
Console.WriteLine("I am learning {0}", subject);
  • Now your Program.cs file will look something as shown in below figure:

First Code in C# Tutorials, c# console, c# tutorials, console writeline, console readline

  • In the first line of code, I am printing “What are you learning ???” on the console.
  • In the second line, I am waiting for incoming data from console, which user has to enter, and I am saving that data in a string variable called subject.
  • In the third line, I am printing the data entered by the user on the Console.
  • You must have noticed {0}, it’s one way of adding data in a string and is called Place Holder Syntax.
  • You can add as many datas as you want, separated by commas and can print them as {0} {1} {2} and so on.
  • Let’s run our code and we will get a similar output, as shown in below figure:

First Code in C# Tutorials, c# console, c# tutorials, console writeline, console readline

  • I have entered C# and it has printed it back, as you can see in the console.
  • Now when you press any key, the console will stop.
  • You can also use Concatenation instead of Place Holder Syntax, let’s have a look at the third line, in below code:

First Code in C# Tutorials, c# console, c# tutorials, console writeline, console readline

  • So, now I have used + sign instead of curly brackets {0}, it’s called concatenating a string.
  • If you run your code, you will get the same output, as shown in above figure.

That’s all for today. I hope you guys have got something out of it. In the next C# tutorial, we will have a look at Introduction to Data Types in C#. So stay tuned and have fun !!! 🙂

Leave a Reply