C Sharp Programming Language – First Demo Program
This is an introduction to C# Programming Language. In this module, we will understand the basic structure of the C Sharp Programming Language.
- What is a namespace?
- What is the purpose of the ‘main’ method?
Let’s jump straight into a demo open Visual Studio 2017. Now create a new project go to File -> New Project and remember with .Net Framework we can use a variety of different programming languages like C# and there are other programming languages as well as Visual Basic, Visual C++, and F Sharp. Now we will choose a programming language as C Sharp. With .Net you can create different types of applications. You can create web applications, windows applications or WPF etc but we will be creating a console application so the type of application is a console application. We will name a project as ‘IntroductionToCsharp’ and you can change the location of this project as you desire and click OK so this creates a C Sharp Console project for us.
We will be talking about ‘Solution Explorer’, ‘Properties’ window toolbox etc in a later session so let’s leave them from now and focus on our code. I am gonna make this program as simple as possible. So, let’s leave only the basic required lines of code within this program and get rid of the others. So, just remove all using statements from the top of your Program.cs file except ‘using System’. Also, remove the main method argument list. Now, you look at this particular program it’s a very simple program.
Purpose of this C Sharp Program – C Sharp Programming Language
I want this program to print a very simple message onto a console screen. I want C sharp program (because we are using C sharp programming) to print that. I am gonna make use of the classes that are
present inside the .Net framework. Now if you’re new to dot net framework please check my ASP.NET Section to know about it. I am using ‘Console’ class from .Net library and will call its methods to get my task done. This class basically used to write on and read from the console. Let’s see how does this work.
Write ‘Console.WriteLine(“Welcome to C Sharp Programming Language Course….!!”);’ in curly braces of main function. You will see that intellisense shows two methods one is WriteLine and the other is Write. The difference between them is WriteLine automatically pass the cursor to the next line while Write continues from the same line.
How to Run a C# Program – C Sharp Programming
Just hold ‘Ctrl’ and press ‘F5’ then release both keys. Your program will start building and after some time console will appear. Now, Here we go….your message has appeared.
Building Blocks of a C# Program – C Sharp Programming
- using System: This is a very special line and needs to be present in almost every C# Program. This is a declaration of ‘System’ namespace. A ‘namespace’ is a collection of classes which are related to that namespace. These classes are defined in that namespace. Dot net Framework has a collection of namespaces and everyone serves a different purpose. In this program, we are using ‘Console’ class which is declared and defined in ‘System’ namespace. If you remove using statement then the error will occur.
- Program Class: If you have a programming background then you know that C# and Java are totally object-oriented programming Languages. If you don’t then do not worry we will be discussing classes and objects in detail but until then just get over it. Which means that everything must be declared in a class. That’s why we are having ‘Main’ function in the class.
- Main Method: ‘Main’ method is the entry point in your program. A program contains only one ‘Main’ method. When you press F5 all namespaces and code get compiled but this has nothing to do with your output. Main method has the control of your program flow. Program execution starts from the ‘Main’ open brackets and terminates with closing brackets.
Point to Remember: If your Program has more than one methods still then program execution will be from ‘Main’ method.