Hello programmers, here we are with one another tutorial series on ASP.Net (C#). C# is a graceful language it is an object-oriented programming language. This programming language is very similar to other programming languages that we are already familiar with. The most common languages are Java, C++, and the Visual Basic.Net. Actually, C# is a purely object-oriented programming language just like the Java. So it supports the oops feature of the programming language. By using of C# programming language we can build Application like – Web Application, Mobile Apps, Enterprise Application, Windows Application, Cloud Application, Windows Store App and many more. This is the very popular language just like Java. So, if you are an absolute beginner in C# then you are here in the right place because we are going to demonstrate with very basic with C# Hello World project.
Before looking forward to the C#, you will require the Visual Studio IDE installed on your system. In VB.Net series we have already seen how to install the Visual Studio IDE on the Windows platform. You can follow this below link.
After going with the above link you will have a Visual Studio Configured system. Now, you are ready to start a new project in the C#. So, let’s continue with a new project.
C# Hello World Project
In this post, we will see a common example of hello world application. So start your Visual Studio IDE and create a new project.
Creating a New Project in C#
- Go to the File menu then click on New Project
- Now, in the Template section, under the Other Languages choose the Visual C#.
- Choose the type of project. Here we are going to create a Console Application first. So select the Console Application.
- Then choose the target .NET Framework from the given option.
- Give the project name, choose the location for saving the project file and click on OK.
- It will take few seconds to build the project.
- Once the project has loaded, a code window of C# will appear in front of you.
Library Files in C# Hello World Project
C# library files are the predefined function just like Java or any other programming languages. So, in the console code window, all the basic library file will appear.
Here you will confuse to see these statements. There is a bunch of using statement in the code window. So what is these using statement means?
Don’t worry I’m explaining you everything here.
- Our project name is HelloWorld. So, the Visual Studio creates a namespace of the project name.
- In the C#, namespaces are the predefined names in the library. There is a bunch of namespaces that resides inside the c# application. For example – the variable names are the namespace.
- The very first namespace is System. A namespace contains several other namespaces. In that case, it is called sub-namespace.
- When you write the code in this namespace we have access to any classes defined in this namespace. So if you want to use a class that is defined in different namespaces you will need to import it into the code file. Hence, we use the using statement. In every project of C#, the Visual Studio has its five using statement.
Namespaces in C#
- Using System: Here the System is a namespace in .NET Framework and that’s where we have all these basic utility classes and primitive type. This is the fundamental namespace that is most required to access any other namespaces.
- Generic: This namespace contains the classes and interfaces inside it. It resides under the System.Collections namespace. The System.Collections namespace mainly defines the collection of the object such as list, queue etc.
- Linq: It stands for Language Integrated Query that provides the classes and interfaces to support the queries to work with the data
- Text: This namespace provides the classes and interfaces for the ASCII Unicode to work with text and coding. Mainly it converts the text and binary data to ASCII code which is machine-understandable.
- Tasks: The namespace uses for building the multithreaded application. It provides the feature to stop and start the task.
C# Hello World Program
- In our application, we are going to create a simple application in which we will display a message. So we are not gonna to use any of these four (System.Collections.Generic, System.Linq, System.text, System.Threading.Tasks) namespaces.
- So we will remove it from the code window.
- Once you have done, the code window will look like this.
- Namespace HelloWorld: By default, we have a namespace which is called Program. So every namespace has a class called Program.
- Every class program has a main() method by default.
- static void Main(String args): This method is declared static.
- A method has input and output. So what goes inside the parenthesis is called parameter or argument.
- Parameters are optional and in this case, default template and main method have parameter string.
- The void is the return type of the method. This shows that the method will not return any value.
Note: C# is a case-sensitive language. So this Main has to be Capital “M” otherwise CLR(Common Language runtime, it manages the execution of the programs written in any language that uses the .NET Framework) is not gonna find this method as the interpretation the application.
- The curly braces are important for writing classes, main method and for namespaces in c#.
C# Hello World Source Code
static void Main(string args)
- Here is the sample of written source code.
- Console: it is a class which is used to read data from the console and writes data on the console. It also provides the error stream which occurs during the input and output of the data.
- WriteLine: it is a method and it takes the parameter.
- Readkey(): This method is used to hold the console screen so that the output can display on the screen.
Now let’s run the console application
Executing the C# Hello World Project
- Save the above program and run it using Ctrl + F5 or you can run it by clicking on Start option in the menu bar.
- Once the project has successfully executed, it will print the message that has written inside the WriteLine method.
- Console.Write and the Console.WriteLine both are the same with a little difference. The Console.Write method will print the statement and it will set the cursor position to the same line at the end of the statement.
- But the Console.WriteLine method will print the statement and it will place the cursor position to the next line.
- Here is the output of the above source code. You can see here, the cursor position has placed after printing the statement.
In this post, we have learned the very basic of the C# programming language. This was the basic building block of the C#. The above namespaces will be used in the further projects. We will work with the web form in our upcoming tutorials. Apart from this, we will see how we can connect our project to the database. So that’s all for this post and stay connected with us for the upcoming post in this series. Till Enjoy Coding 🙂