C# is a popular cross platform programming language used to creating wide ranging of applications like Web, Desktop, or Mobile.
You can create a C# application using wide range of options like Visual Studio or Visual Studio Code.
Here I am concentrating on .Net CLI with Visual Studio Code.
.Net CLI
.Net CLI (Command Line Interface) can be used to create and build .Net application.
As mentioned in the options dotnet -h or dotnet --help can be use to display .net help.
Creating a .Net CLI Console project.
dotnet new console -o HelloConsole
This command creates a console application within a folder called HelloConsole inside the current folder.
code . opens the current folder which comprises of the project in Visual Studio Code.
Top Level Statement
Program.cs file within project contains WriteLine method of Console class for displaying a message in the command prompt.
C# follows Pascal case. Identifiers should stat with capital letter. So Console's C, WriteLine's W and L should be capital.
Console.WriteLine("Hello Shalvin P D");
Memory Variable
string name = "Shalvin P D";
Console.WriteLine("Hello, {0}", name);
Interpolation
string name = "Shalvin";
Console.WriteLine($"Hello {name}");
Multiple Memory Variable
string name = "Shalvin";
string location = "Kochi";
Console.WriteLine($"Hello {name}, located at {location}.");
Memory Variable Console.ReadLine
string name;
Console.WriteLine("Enter your Name: ");
name = Console.ReadLine();
Console.WriteLine($"Hello {name}");
Multiple String Memory Variables
string name, location;
Console.WriteLine("Enter your Name: ");
name = Console.ReadLine();
Console.WriteLine("Enter your Location: ");
location = Console.ReadLine();
Console.WriteLine($"Hello {name}, located at {location}.");
Integer Memory Variable
int i, j, res;
i = 23;
j = 45;
res = i + j;
Console.WriteLine(res);
Int32.Parse (Simple Calculator)
int i, j, res;
Console.WriteLine("Enter Value 1: ");
i = Int32.Parse(Console.ReadLine());
Console.WriteLine("Enter Value 2: ");
j = Int32.Parse(Console.ReadLine());
res = i + j;
Console.WriteLine(res);
If Statement
string city;
Console.WriteLine("Enter the Name of the City: ");
city = Console.ReadLine();
if (city == "Kochi")
{
Console.WriteLine("Industrial Capital of Kerala");
}
If else
string city;
Console.WriteLine("Enter the Name of the City: ");
city = Console.ReadLine();
if (city == "Kochi")
{
Console.WriteLine("Industrial Capital of Kerala");
}
else
{
Console.WriteLine("I don't know");
}
Multiple else if
string city;
Console.WriteLine("Enter the Name of the City: ");
city = Console.ReadLine();
if (city == "Kochi")
{
Console.WriteLine("Industrial Capital of Kerala");
}
else if (city == "Trichur")
{
Console.WriteLine("Cultural Capital of India");
}
else if (city == "Trivandrum")
{
Console.WriteLine("Capital of Kerala");
}
else
{
Console.WriteLine("I don't know");
}
No comments:
Post a Comment