Tuesday 21 January 2014

Reading and writing to a console in C#

                                                                                                                                       Precious......
                                                                                                                                                    Next......



In this session we will learn Basic structure of a C#  programme.
In part 1  we simply print a message by using "Console.writeline" keyword. But in this part we print a message which is printed by user. If  user print  "NetQube" then it return "Hello NetQube".

Two ways to write to console
     a) Concatenation
     b) Place holder syntax – Most preferred 



Code samples used in the demo


using System;
class Program
{
    static void Main()
    {
       
        Console.WriteLine("enter your name");
        // Read the name from console
        string UserName = Console.ReadLine();
        // Concatenate name with hello word and print
        Console.WriteLine("Hello " + UserName);  // here UserName should be same because C# is a case sensitive 
        //Placeholder syntax to print name with hello word 
        //Console.WriteLine("Hello {0}", UserName);
       // here {0} is a place holder when we print user name then it substitute at {0} position
    }
}


We can Better understand by using another example
using System;
class Program
{
    static void Main()
    {
       
        Console.WriteLine("enter your FirstName");
        string FirstName = Console.ReadLine();
         Console.WriteLine("enter your  SecondName");
         string SecondName = Console.ReadLine();
        Console.WriteLine("Hello " +  FirstName , SecondName); 
        //Placeholder syntax to print name with hello word 
        //Console.WriteLine("Hello {0} , {1}", FirstName , SecondName);
       
    }
}


                                                                                                                                       Precious......
                                                                                                                                                    Next......






No comments:

Post a Comment

C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...