This Tutorial on Introduction to C# & .Net Framework Teaches the Basic Structure of a C# “Hello World” Program & the Basic Features of the Visual Studio .Net Framework:
C# is one of the most commonly used programming languages for.NET frameworks. C# is an object-oriented simple yet powerful language developed by Microsoft. It is pretty similar to C and C++ languages on which it is based.
The name of the language C# (pronounced as c sharp) is inspired by a musical note and at the same time, it also resembles a collection of four “+” sign symbolizing that the language is an evolved version of C++.
=> FREE C# Training Tutorials For All
Table of Contents:
What Are C# And .NET framework?
Although C# can be compiled by the machine code, it is always used in the conjuncture with the .Net framework, hence, any application developed with C# needs .Net framework as a pre-requisite.
The .NET Framework
The .Net framework developed by Microsoft is a powerful platform that helps developers in writing different types of applications including Windows, web and web services. The framework consists of a large library of codes that can be used by languages like C#.
Features Of C#
C-sharp is an object-oriented programming language that offers tons of features to programmers.
Some of which are:
- Simple and easy to understand syntax.
- It offers Interoperability, scalability and is updatable.
- It is based on components.
- It has a rich library and is a structured language.
- It is type-safe i.e. it only allows code to access the memory location for which it has authorized access.
Pre-Requisite
This tutorial has been prepared for the beginners. However, a little knowledge of programming will help you in understanding it better.
Integrated Development Environment For C#.Net
C# can be written in any text editor like notepad and can be compiled using a C# compiler but most programmers prefer to use an Integrated Development Environment (IDE) for writing programs. IDE is basically a tool that helps programmers in writing programs in different languages like C#, Visual Basic, Java, etc.
For C# we will be using the IDE provided by Microsoft.
Microsoft offers its most popular IDE for C# i.e. Visual Studio. Microsoft has also introduced an express version of Visual Studio for newbies and those who wish to use .Net without worrying about the licensing cost. Visual Studio Express Edition works perfectly for writing all types of programs in C#.
=> Download Visual Studio Express Edition
Features Of Microsoft Visual Studio
Visual Studio is one of the most popular IDE that is available for C#.
Some of its features include:
- It can be easily used with Microsoft or any third party. Any third party app can be used by using Nuget packages.
- Easy to add and edit different file types that may be required for developing your application.
- Supports Intellisense.
- A robust and powerful debugger that allows you to debug your code line by line.
Getting Started With Visual Studio And C#.Net
We will start by writing a simple program and will discuss the components that constitute a C# project. In this tutorial, we will discuss the following topics in detail:
- How to create a new project?
- Components and file that makes up the C# project.
Create A New project
To begin with, we will create a simple console application. We will not be working on a complex program, to start with, a simple console application will just work fine for us. A console application looks like a DOS or commands prompt window.
Let’s Begin!!
Open Visual Studio express on your desktop and it will open with the following page.
While looking at the Visual Studio for the first time it may feel a little complex, but as we proceed further through the tutorials we will get familiar with its usage and features.
Once the start page of the application opens, click on the “New Project” link to create your first project.
Or else you can also use, the “File” menu to create a new project.
Once you click on the New Project link another “New Project” dialogue box will open with the list of the types of projects that you want to create. This is the place where we will select the type of project that we want to create.
For now, select your language as “Visual C#” and then select “Windows” from the list under it. This will display a list of the project types available under “Windows” in the right panel. We will select “Console Application” from the list. In the New Project window provide the name of the project and then click on the “OK” button.
This will create a console project with the given name and a basic skeleton structure of the program.
If you look at the right-hand panel you will find the solution explorer. Solution Explorer contains all the files of your project. If solution explorer is not available then you can get it by navigating to VIEW -> Solution Explorer.
In the solution explorer, you will find a “Program.cs” file. This is the file that contains the main method and this will be executed first once the application starts.
Now click on the arrow before “Properties” to expand it. You will find the “AssemblyInfo.cs” file. This file contains information about the selected program. If you double-click on it, it will open up.
It will be something as shown below:
[assembly: AssemblyTitle("ConsoleApplication")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("ConsoleApplication")] [assembly: AssemblyCopyright("Copyright © 2018")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")]
These are editable, hence you can change to values based on your project. You can provide the title, description, copyright details, trademark, etc.
Next, we will look into references. Click on the arrow before “References” to expand the details. It contains the list of services or entities that Visual Studio needs to locate in order to execute your code.
As we have discussed the various components of the program, we will now proceed to write our first line of code. We already have our skeleton code that was generated at the time of the project creation. Hence, we will use the same structure and write the code using that.
We will proceed by printing “Hello! World” using our first C# program in the console application. To do that let’s start writing our command inside the curly braces after “static void Main(string[] args)“. This component of the project is called a Method. The name given to the method mentioned below is “Main”.
Whenever we execute our program in C#, it looks for a method called “Main”. The Main method is used as the starting point of the execution.
Let’s now enter some code inside the main method. Let us write our first line of code. Let’s write a single alphabet “C” between the curly braces. A pop-up menu will appear. This menu will try to guess what you are trying to write. You can use this menu to write your programs quickly. This menu is called “IntelliSense“.
Select “Console” from the menu by either clicking using a mouse or by highlighting it and pressing enter.
Now add a full stop or dot after “Console”. It will again open the “IntelliSense” menu. Type “Write”. It will offer two options, “Write” and “WriteLine”. We will discuss them in detail in our upcoming tutorials. For now, select “WriteLine”.
Now, put a round bracket after WriteLine. This will open up another grey colored menu that will show you the number of ways in which this WriteLine method can be used. As you can see there are about 19 ways in which this method can be used. You can toggle all the 19 ways by clicking on arrows.
Hence, it’s time for us to write the famous hello world statement. Write “Hello! World” inside the round bracket.
Please do include double quotes at the start and end of your statement. This allows C# to recognize this statement as string or text. Once done put a semicolon (“;”) at the end of your statement. This denotes that your statement has ended.
Now, let’s execute this program by clicking on the start button or by pressing the “F5” key.
This will execute this program.
Now, we have executed our first program but you would have noticed one thing. As soon as “Hello! World” is printed the program gets closed. It sometimes doesn’t even give enough time to the user to read the printed message.
Hence, to hold the window for some time, we will add “Console.ReadKey()” method to our current code. This will allow our program to accept user input. The execution will not be completed till the user provides any input, i.e. press any key from the keyboard.
Now, if you will run this program it will remain idle till the user provides value and then it will get closed.
Components That Makeup C# Program
Till now we have created a simple console application for printing “Hello! World”. If you look at the program you will find that there are several components that make up the program.
Let’s discuss them in detail.
@Remya, a picture is missing here
#1) Class Program
Class is a keyword that is (prefixed before the class name) to define a class. The Program, in this case, is the class name. A class is basically a blueprint based on which the objects are created. It also defines a group of behavior or type of data.
#2) Static Void Main(string[] args)
The keyword static defines that the object is non-instantiable thus this class can not be instantiated using the new keyword. The keyword void signifies return type. Void means that the object doesn’t return any value. In this example the name of the method is Main, this means that it is the entry point for C# for executing a program.
Whenever the program is executed, the Main() method will be executed at the beginning. The keyword string[] args is the command-line argument using which the values can be passed.
#3) Console.WriteLine(“Hello! World”)
Here Console is a class name and WriteLine() is the static method of the console class. This WriteLine method is used to print text in the console.
#4) namespace ConsoleApplication
The namespace keyword is used to define the namespace. The console application is the name of the namespace.
Code Snippet
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication { public class Program { static void Main(string[] args) { Console.WriteLine("Hello! World"); Console.ReadKey(); } } }
Conclusion
In this tutorial, we had a brief introduction to the C# programming language and Visual Studio IDE.
We learned the basics of the C# and .Net and also wrote our first “Hello World” program. We also learned the structure of the program and the basic features of the Visual Studio and how the main method is executed at the beginning of the execution.
=> Check For Our Ultimate C# Training Guide Here