Microsoft Visual C#.NET is a powerful and simple programming language developed by Microsoft, aimed primarily to create windows applications using the .NET framework. C# uses C-Style syntax, so if you are familiar with C/C++ or Java, you will find C# quite familiar.
Prerequisites
Before we begin you will need to download and install Visual C#.NET 2010 (or later) Express. This is the IDE you will be using to write your code and applications. You can get it for free from here.
Creating your first .NET application
After downloading and installing Visual C#.NET, start it and select New Project. The New Project dialog should look similar to the following (the bit of difference is because I’m using Visual Studio and have several addons installed).![]()
Select Console application and change the name to CHelloWorld. C stands for console. Console is the DOS style screen, where input is only via a keyboard.
The Visual Studio Environment
After creating the project, an new tab named Program.cs opens in Code view. This is where our program shall start. The solution explorer to the right shows you the current project and files (If it is not visible, use the shortcut Ctrl+W,S , or simply go to View –> Solution Explorer)
Coding the Hello World – The Console Version
Since the best way to learn a programming language is by coding with it, we’ll start away by coding right away, and then we will delve deeper in to the details.
By now the code should be as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace CHelloWorld
{
class Program
{
static void Main(string[] args)
{
}
}
}
The first 4 lines are using statements, which tells the compiler that we will use class and functions from these libraries(.NET framework libraries). The namespace is a named container which helps in sorting and organizing your application. According to the naming convection, your program must be in namespace named as your application name. Don’t worry if you cant quite get the concept now, it will be explained more later on.
Next is a “Class” called program. In C#, you classify every thing into meaningful objects. It allows you to group related functions and data into a single class. To understand the concept more, think of defining a car in C#. You need to determine it’s color, model, engine specs, etc. This should be done by defining a class called Car, and creating the appropriate functions and properties. Every time you need create a “Car” in your code, you just create a new class. We will be writing our own classes later on.
For our code, the program class is where the program is defined. Right now it contains one function, which serves as the starting point of our application:
static void Main(string[] args)
A function a group of code statements that could be called from another place. For example, if you’re writing a code that would be implementing a factorial several times. It would be tedious to write the same chunk of code several times all over again, so you would write a function to implement the factorial and call it whenever needed. What you need to know now is the function is declared as:
[accessibility] [return type] [identifier]([parameters]){
/// code block here
}
- accessibility modifier – is a keyword that defines where the function could be called
- return type – is the type the functions returns
- identifier – is the name of the function
- parameters- the data passed into the function
- {…} – as all C-style languages, all code blocks are contained within curly brackets
Again you don’t need to worry about all these details, we will discuss it in detail later. For the main function void is the return type, which basically return nothing.
Writing our code
As you may have guessed, all our code should go into the main function. For now, enter the following code after the declaration of the function (between the curly
Console.WriteLine(“Hello World!”);
Console.ReadLine();
Your code should now read:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace CHelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.ReadLine();}
}
}
Run the program by pressing F5 or the little green arrow in the toolbar. You should see a black console screen with Hello World! in it.
Let’s see what is done here:
First we use the WriteLine() method of the Console class, one of the .NET framework classes. This function is used to print output to the console. The word in double quotes indicates that this string is to be printed as it is. Also at the end of all statements(a statement is a line of code that performs a task) in C# a semicolon ; must exist.
The second statement simply prevents the console from closing directly, it waits for user input to close. Remove the statement and see what happens
Note to remove a statement without deleting it is by commenting it out. You do this by converting it in to a comment which is not build into your program. A comment in C# is a statement with double slashes (//) at the beginning. By default, a comment is green in Visual Studio.
This is it for the introduction. Hope you liked it, and please leave a comment with any problem and I will gladly oblige.
Wait for the next part!
3aaaaaash yasta
enta htb2a insaaaaan modhesh
tslm ya 7obby