Your First C# code

  1. In this chapter you will learn:
  2. How to write simple C# code?
  3. Understand the C# programming structure?

Well, you have understood where to and how to execute your program. In this section, you will see the detailed explanation of the short C# program. It will help you to implement your logic into the program. So, carefully study the code and get the basic C# programming structure.

Each program requires three important steps to do work. It is Input, Process, and Output. It is the basic concept of almost all the programming language. It is also known as I-P-O cycle. The program requires some input from the user. Next, the program processes the input with the programming language and finally shows the output.

 

Consider the simple C# code, in which you will enter your name and your name will be displayed with some text message in command prompt.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace First_c_sharp_code
{
    class Program
    {
        static void Main(string[] args)
        {
            string name; //Variable for storing string value

            //Displaying message for entring value
            Console.WriteLine("Please Enter Your Good Name");

            //Accepting and holding values in name variable
            name = Console.ReadLine();

            //Displaying Output
            Console.WriteLine("Welcome {0} in your first csharp program", name);

            //Holding console screen
            Console.ReadLine();
        }
    }
}

Please Enter Your Good Name
Steven Clark
Welcome Steven Clark in your first csharp program
_

However, classes and objects are not mentioned in initial level, so you can find Main(string[] agrs) method to writing code.

Explanation:
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;

It is used for including C# class library. C# has huge collection of classes and objects. If you want to use those classes then you will have to include their library name in your program.

string name; //Variable for storing string value It is a string variable that stores value input by the user. A variable is a symbolic name of special data types that are used to store the value in memory temporarily. Here, the name is variable of string data types.


Console.WriteLine("Please Enter Your Good Name");
It is used for displaying message on console.
name = Console.ReadLine();
Storing value in the name variable.
Console.WriteLine("Welcome {0} in your first csharp program", name);

Again used for displaying message on the console. A new thing in this line is {0}. It is pronounced as place holder that is used for displaying variable value. In the above line, {0} print the value of name.

In C#, Console.WriteLine() and Console.ReadLine() are the very important method. Console.WriteLine() : It is used for displaying message on the console. Console.ReadLine() : It is used for accepting user input.

Summary

In this chapter you learned about different section of C# code. You also learned how to write C# code. In next chapter, you will learn about various that are used in C#.

 

Share your thought