Lesson 2 C# Console Application
Lesson 2 C# Console Application
Here, you will learn to create a simple console application in C# and understand the
basic building blocks of a console application.
Open Visual Studio (2017 or later) installed on your local machine. Click on File ->
New Project... from the top menu, as shown below.
From the New Project popup, shown below, select Visual C# in the left side panel
and select the Console App in the right-side panel.
Page 1 of 12
Select Visual C# Console App Template
In the name section, give any appropriate project name, a location where you want
to create all the project files, and the name of the project solution.
Click OK to create the console project. Program.cs will be created as default a C# file
in Visual Studio where you can write your C# code in Program class, as shown
below. (The .cs is a file extension for C# file.)
C# Console Program
Page 2 of 12
Every console application starts from the Main() method of the Program class. The
following example displays "Hello World!!" on the console.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharpTutorials
class Program
Console.WriteLine(message);
Try it
The following image illustrates the important parts of the above example.
Page 3 of 12
C# Code Structure
Let's understand the above C# structure.
1. Every .NET application takes the reference of the necessary .NET framework
namespaces that it is planning to use with the using keyword, e.g., using
System.Text.
2. Declare the namespace for the current class using the namespace keyword,
e.g., namespace CSharpTutorials.FirstProgram
4. The Main() is a method of Program class is the entry point of the console
application.
Note:
To see the output of the above C# program, we have to compile it and run it by
pressing Ctrl + F5 or clicking the Run button or by clicking the "Debug" menu and
clicking "Start Without Debugging". You will see the following output in the console:
Page 4 of 12
Output:
Hello World!!
Casting is the process of converting one data type into another. In C#, casting
can be done in two ways: implicit and explicit casting.
1. Implicit Casting
Implicit casting (also known as automatic conversion) happens when the
compiler can perform the conversion automatically without the risk of data
loss. It is typically possible between compatible types and where the target
type has a larger capacity.
2. Explicit Casting
Explicit casting, sometimes called type casting or forced conversion, is
necessary when there's potential for data loss - typically when you're
attempting to convert a larger type to a smaller one.
Page 5 of 12
Type Conversion in C#
Type conversion in C# can be performed using built-in methods. This is
generally needed when converting between incompatible types, such as from
a string to an integer.
Here's an example of type conversion using the Convert class:
namespace MathExampleOne
{
class Program
{
static void Main(string[] args)
{
double pi = 3.14159;
double radius = 2.5;
double area = pi * radius * radius;
Console.WriteLine("The area of a circle with radius " +
radius + " is " + area);
Console.WriteLine("Hit any key to end...");
Console.ReadKey();
Page 6 of 12
}
}
}
output
The area of a circle with radius 2.5 is 19.6349375
Hit any key to end...
using System;
namespace VariablesExampleTwo
{
class Program
{
static void Main(string[] args)
{
double pi = 3.14159;
Console.Write("Please enter a value for the radius: ");
double radius = double.Parse(Console.ReadLine());
double area = pi * radius * radius;
Console.WriteLine("The area of a circle with radius " +
radius + " is " + area);
Console.WriteLine("Hit any key to end...");
Console.ReadKey();
}
}
}
Output
Please enter a value for the radius: 2.5
The area of a circle with radius 2.5 is 19.6349375
Hit any key to end...
Page 7 of 12
Remarks
Console.Write("Please enter a value for the radius: ");
double radius = double.Parse(Console.ReadLine());
code Analysis
First we prompt the user for a value. This ensures that the user
realizes that the program is waiting for input. Then we read
a double into the variable named radius. This radius is used to
compute the area of the circle. Note that this program will not deal
with any errors that could be generated by the Parse() method.
Operators are symbols that can be used with operands to perform
tasks.
Operators are symbols that can be used with operands to perform
tasks.
Page 8 of 12
Page 9 of 12
Page 10 of 12
Logical Operator
Practice Exercises
1. Use user input and the Math namespace to generate the sin and
cos of a user assigned value.
2. Use user input and the Math namespace to find the maximum
value of two user assigned values.
3. Write a program to calculate area of triangle. Take required input
from user and then calculate the area of triangle.
4. What will be the output of the following C# relational operator
code?
int n = 2;
int p = 4;
int q = 5;
int w = 3;
if ( !((p * q) /n <= (q * w) + n/p ))
{
Console.WriteLine( ++p + w++ + " " + ++n);
Console.WriteLine("b");
}
else
{
Console.WriteLine(--p + q-- + " " + --n);
Console.WriteLine("a");
Page 11 of 12
}
Page 12 of 12