Loading, please wait...

A to Z Full Forms and Acronyms

Hello World in C# | C# Tutorials

Nov 08, 2020 C#, CSharp, 3988 Views
In this there will be illustrations about many things which will include methods, commands, variables and many more. It will show how to print or show things which the user wants. This can be pre-written or asked by the user as an input. There will be methods to add, delete and manipulate data.

Hello World in C# | C# Tutorials

Introduction

In this, there will be illustrations about many things which will include methods, commands, variables, and many more. It will show how to print or show things that the user wants. This can be pre-written or asked by the user as an input. There will be methods to add, delete, and manipulate data.

Pre-requisite

To perform the programs the Visual studio will be used as an integrated development environment. Simply open up the Visual Studio and click on CREATE A NEW PROJECT then select Console App (.Net Core). Name the project then click on Create.

Console.WriteLine()

Once the IDE gets set up and the project is created there will be a default code written there already. The code is for printing Hello World.

Run the pre-existing code and see the results.

In the above code the line Console.WriteLine() is used. This chuck is used to write or display data provided to it via different means such as directly or some integer, character, string, etc, values.

In this, there is less command by the user on the code which means if we alter the code

Console.WriteLine(“Hello”);

Console.ReadLine()

It will show the word Hello only when it will be executed. So in order to change the values or data to be displayed, it is needed to get the data from the user as input.

Right here the code Console.ReadLine() comes in. By putting this code this will ask the user to put some input which can be later on used for different operations and even printing.

NOTE: There is a need for storing the value from this code. It can be into a variable or passed as a parameter directly.

string word;

word = Console.ReadLine();

 

 

  • There is a condition with this code when we try to get some int value or double or anything other than a string value. It will show an error. The reason is that the code ReadLine() has a default to get string values only. In order to get something else, it is needed to be converted.

So we use

Convert.ToInt32(Console.ReadLine());

Here the value from the user will be changed to an integer value of 32 bits. The bits can be changed simply by changing the numbers such as 64, whereas 32 is the default.

This is not restricted to an integer only, we can convert to double and long types too.

using System;




namespace Hello_World

{

    class Program

    {

        static void Main(string[] args)

        {

            string word;

            int num;

            Console.WriteLine("Insert a word ");

            Console.WriteLine("===============");

            word = Console.ReadLine();

            Console.WriteLine("The word is :");

            Console.WriteLine(word);

            Console.WriteLine("Insert a number ");

            Console.WriteLine("===============");

            num = Convert.ToInt32( Console.ReadLine() );

            Console.WriteLine("The number is :");

            Console.WriteLine(num);

        }

    }

}

Working more with this

The job with data reading and writing is not over here. There are ways to manipulate the data even after taking it from the user itself. Using concatenation the values can have something additional too.

Adding String: 
using System;

namespace Hello_World
{
    class Program
    {
        static void Main(string[] args)
        {
            string word;
            int num;
            Console.WriteLine("Insert a word ");
            Console.WriteLine("===============");
            word = Console.ReadLine();
            Console.WriteLine("The word is :");
            Console.WriteLine(word);
            Console.WriteLine($"The word {word} is inserted here while executig the code");
        }
    }

 

Altering int, float and other values:
using System;

namespace Hello_World
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1, num2;
            Console.WriteLine("Insert first Number ");
            Console.WriteLine("===============");
            num1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Insert second Number ");
            Console.WriteLine("===============");
            num2 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine($"Now the numbers {num1} and {num2} will be added and the new sum is {num2 + num1} ");
        }
    }
}

 

A to Z Full Forms and Acronyms