• Register
100 points
3

In C#, the System namespace provides a static class Console which have several methods which are responsible for perfroming read/write action in console. To do these action, we need to create a console application. If you installed SDK for .NET or have Visual Studio installed as IDE, then create a simple .NET console Application with C#.

Write a string to console

Go to your Program.cs file, Here you find that, the template of SDK for Console Application already used Console.WriteLine() method. Build your application and run it.

class Program
{
    static void Main(string[] args)
    {
         Console.WriteLine("Hello World!");
    }
}

It will print Hello Word! in your Console. Just change the string parameter of the Console.WriteLine() method "Hello World!" with your desired string. Build and run it again. Now It will print your desired string in your console instead of printing Hello World!
 

Read a string from console

Inside your main method, let add a new statement for reading a line from console using Console.ReadLine() method. It will return a string value from the standard read stream. So that store it in a local variable myStr of string type. Print the value of your variable in console using Console.WriteLine() method by adding a "Hi ” before it.

class Program
{
    static void Main(string[] args)
    {
       string myStr =  Console.ReadLine();
       Console.WriteLine("Hi " + myStr);
    }
}

Input:

Kodlogs

Output:

Hi Kodlogs

 

100 points
3