Introduction

C# (pronounced as C Sharp) is an object-oriented programming language that runs on the .NET Framework. It allows developers to create robust and secure applications. Let’s talk about the basic concepts of C#.

Variables: Variables are used in the program to store data. When a variable is created, it reserves a memory location to store values in it.

Data Types: The information that can be stored in a variable is defined by data type. There are different data types for information used in the program.
Built-in Data Types: C# offers a number of built-in data types. Most commonly used are:

  • int – integer.
  • float – floating point number.
  • double – double precision version of float.
  • char – a single character.
  • bool – it can have only one of two values: True or False.
  • string – a sequence of characters.

Here are some examples of data types used in C#:

int x = 12;
double pi = 3.14;
char a = 'F';
bool HasPArent = true;
string firstName = "Sam";

Setting up an Integrated Development Environment (IDE):

In order to create your first C# program, you need to install Visual Studio Community that offers an IDE with coding and debugging tools. You can download it from here:
https://www.visualstudio.com/downloads/

Choose the default configuration after installing it, then select File > New > Project and then click Console application as shown below:

Enter your project’s name and click OK.
Visual Studio will automatically generate some set of statements for you which look like this:

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

namespace TutorialsPanel
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

Don’t confuse yourself with lots of statements here; we will discuss each and every line of code in further tutorials. For now, just remember that every C# console application must contain a function called Main. Main is the point from where our program starts execution. To run your program, press Ctrl+f5. The output is generated on the console window.

Printing text on the screen

C# display text on console window by using Console.Write or Console.WriteLine. The only difference between these two is that Console.WriteLine terminates the line, i.e. it moves the cursor to the next line after displaying the output.
Example:

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

This program will generate output “Hello World!” to the console window.

We can also display variable values in the output. See the example below:

namespace TutorialsPanel
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 12;
            Console.WriteLine(x);
            Console.ReadKey();
        }
    }
}

Output

12

Also, we can display variable values by using formatted string. Like this:

namespace TutorialsPanel
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 12;
            int y = 20;
            Console.WriteLine("x={0}; y={1}", x,y);
            Console.ReadKey();
        }
    }
}

Output

X=12; y=20

Here, {0} is replaced by the value of x, and {1} is replaced by the value of y.

Getting User Input

The function Console.ReadLine takes input from the user and stores it in the string variable.

Example:

namespace TutorialsPanel
{
    class Program
    {
        static void Main(string[] args)
        {
            string FirstName;
            Console.WriteLine("Enter your First Name:");
            FirstName = Console.ReadLine();
            Console.WriteLine("You wrote: {0}", FirstName);
            Console.ReadKey();
        }
    }
}

Here, Console.WriteLine first prompts the user to input data. Next, Console.ReadLine function waits for the user to input data, and save it to the string variable ‘yourName’. The next statement is the formatted string in which Console.WriteLine displays “You wrote:” along with user input. For example, if you entered ‘Sam’ the output will be “You wrote: Sam”.

The function Console.ReadLine returns a string value by default. If you want to enter a data type other than string, you need to convert it using Convert.ToXXX, where XXX is the name of the data type we want to convert to, such as Convert.ToBoolean, Convert.ToDouble etc.

Let’s convert the data type to integer. It can be done by using three alternatives based on the bit size of the integer: Convert.ToInt16, Convert.ToInt32, and Convert.ToInt64. In C#, the default integer type is 32 bit.

Here is an example for you:

namespace TutorialsPanel
{
    class Program
    {
        static void Main(string[] args)
        {
            int age = Convert.ToInt16(Console.ReadLine());
            Console.WriteLine("You are {0} years old", age);
            Console.ReadKey();
        }
    }
}

In this code, Console.ReadLine takes input from the user, convert it to 32-bit integer, and store it to the variable ‘age’ of type integer. In the next line, the formatted string displays the output by replacing the value of 0 by variable age. For example, if you have input your age 20, the output will be “You are 20 years old”.

Now you know how to print text on the screen and how to take input from the user and display it on the Console Window. Happy Coding!

Related Articles

C# ‘for’ Loop

C# Variables

C# while Loop

Last modified: March 6, 2019

Comments

Write a Reply or Comment

Your email address will not be published.