First C Sharp Code

This first C# code will help you to implement  logics into program and clarify how the code and C# programming structure works..

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.. The program requires some input from the user. Next, the program processes the input with programming language and finally shows the output
    
 Basic C# Example:
using System;

namespace my_prog
{
    class Hello
    {
        static void Main()
        {
            Console.WriteLine("Hello World");
        }
    }
}


How to debug or Execute:          

      Using Notepad

Step 1: Open Notepad and write the following c# code carefully.
Step 2: Now save this program as my_prog.cs. You must choose All Files in save as type box.
Step 3: Go to Start > All Programs > Microsoft Visual Studio 2010 or 2012 > Visual Studio Tools > Visual Studio 2010 or 2012 Command Prompt.
Step 4: Set Path in command prompt where your Chapter1.cs file is saved. Here we are using D:/Csharp>
Step 5: Now compile your program by following command:
  csc my_prog.cs


You will see some information like compiler version number and framework information. There is no error, so you won’t get any error.
Step 6: Now, it’s turn to C sharp program. Just write your file name without extension and hit enter.
  my_prog
Step 7: You will get the output as Hello World
Setting environment for using Microsoft Visual Studio 2008 x86 tools.

C:\Program Files\MIcrosoft Visual Studio 9.0\VC>d:

D:\>cd csharp/

D:\csharp>csc Chapter1.cs
Microsoft <R> Visual C# 2008 Compiler version 3.5.30729.1
for Microsoft <R> .NET Framework version 3.5
Copyright <C> Microsoft Corporation. All rights reserved.


D:\csharp>my_prog
Hello World

D:\csharp>

      Using Visual Studio 2010 or 2012

Visual Studio is easiest way to handle C# code. To execute the program on visual studio, go through the following steps:
Step 1: Launch Visual Studio and go to File > New Project
Step 2: Select Visual C# in left pane and then choose Console Application.
Step 3: Select your location where you want to save the file and then click OK.
Step 4: Visual Studio will open a code editor window including some necessary code.
Step 5: Now write the above mentioned code and press Ctrl+F5 to run C# code.
Step 6: You will see the same output in Visual Studio Command Prompt.
OR
Simply Press F5 to execute your program

Output:
              Hello World
                                                                                                              

Advanced C# Example:
using System;

namespace my_prog
{
    class Program
    {
        static void Main(string[] args)
        {
            string f_name, l_name; //Variable for storing string value

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

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

            Console.WriteLine("Please Enter Ypor Last Name");

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

            //Displaying Output
            Console.WriteLine("Your Full Name is {0} {1}", f_name, l_name);

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


Now Press F5 to execute your program

Output:
              Please Enter Your first Name 
         DotNet
         Please Enter Your Last Name
         Bird
             Your Full Name is DotNet Bird


You can Press F11 continuously to see how your program executes.
After completion of each segment, must do exercise. It will help you clear your concept and improve your programming skills.
C# programming is very easy so never be bored with this language. After sometime, when you will cover basic segment, you will be surely confident in C# programming.


       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  f_name , l_name; //Variables for storing string value
It is a string variable that stores value input by user. A variable is a symbolic name of special data types that is used to store value in memory temporarily. Here, name is variable of string data types.

Console.WriteLine("Please Enter Your First Name");   used for displaying message on console.

 f_name = Console.ReadLine();    Storing value in the f_name variable.
 l_name = Console.ReadLine();    Storing value in the l_name variable.
Console.WriteLine("Please Enter Your last Name");     used for displaying message on console.

Console.WriteLine("Your Full Name is {0} {1}",f_name, l_name);
Again used for displaying message on the console. A new thing in this line is {0} and {1}. It is pronounced as place holder that is used for displaying variable value. In the above line, {0} print the value of f_name and {1} prints the value of l_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.


Comments