C# Programming Tutorial for Beginners
C# is Microsoft’s language for developing a variety of Windows OS applications. The applications developed in C# range from a simple desktop calculator to a large enterprise level information management systems. C# is an integral part of Microsoft’s .NET framework. The C# language can be used as a back-end programming language for ASP.NET applications, Windows Form Based applications, WPF Applications and now for Windows Phone applications as well.
C# owes a lot to Java for its advanced features and to C++ for its robustness. C# was developed with the intent to integrate security and low level programming features of C++ and the more advanced modules of Java. However, C# is considered a high level language and it still lags behind Java in market share owing to its portability issues. C# is a purely object-oriented language like Java. C# is the best place to start for all those programmers looking to enter the Microsoft’s application development arena. This article is a basic C# programming tutorials for absolute beginners who are looking to get themselves acquainted with Microsoft’s programming stack. The article contains few basic examples that might not look fascinating but are good enough to impart the basic concept.
Interested in learning more about C#? Check out this course at Udemy.com.
C# Hello World Console Application
As with every language, it is always convenient to start with a simple application that only displays “Hello World” on the screen. This article follows that trend. The first example contains a simple C #application that displays a message “Hello World” on the console output:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CSharpExample { class Program { static void Main(string[] args) { Console.WriteLine("Hello World"); } } }
The above code might look extremely simple and straight forward; however, a lot of things are happening here. First of all on the top of the code, there are some statements starting with a keyword “using”. These statements are namespaces. Namespaces are similar to packages in Java. Namespaces are used to package classes with similar functionalities. For instance, all the classes that are used for drawing purposes can be packaged into a namespace like System.Drawings. Every class in C# has to be in some namespace.
Another important thing to note here is that all the functional code in C# resides in a class. In the above example, there is a class named Program and this class resides in custom namespace called CSharpExample. Inside the Program class, there is a static method named Main which takes a String type array args as argument. Every C# program needs an entry point; the static Main method serves as entry point to the code. Since it is static method, therefore it can be called without creating object of type Program. (A class is often referred as a type).
Inside the Main method, WriteLine method of the class Console has been called which is used to write text to the console output. The Console class belongs to System namespace. This is where the importance of importing a namespace via keyword “using” can be highlighted. Since, the System namespace has already been imported; therefore all the classes in the System namespace can be directly used like Console class. However, if the System namespace had not been imported, a fully qualified name i.e. System.Console would have been used, which is tedious and time consuming.
To learn more about C#, take a course at Udemy.com.
C# Objects, Member Variables and Methods
Though, Hello World application explained in the last section was good starting point, yet it was only a drop in the vast ocean of C# language. In this section, three basic building blocks of C# application have been explained. These are objects, variables and methods.
Objects
Classes itself are nothing but blue prints. They are similar to the map of a house that has not been built yet. When a class is defined in C# application, no space is reserved in the memory for that class. The actual house which physically exists in the memory of a computer is the object. The object is nothing but a physical representation of class. One class can have multiple objects similar to multiple houses been built on one map.
Member Variables
Variables can be seen as a properties or characteristics of an object of a particular class. For instance, there is a class named Vehicle that represents a vehicle, this class can have variables such as vehicle_number, vehicle_type etc.
Methods
Methods are functions that a particular class can perform. Methods are also called functions in some languages. Methods can take parameters and have a return type. For example, for a vehicle class, the methods can be StartVehicle, StopVehicle etc.
In the following example, the concept of C# objects, member variables and methods have been explained.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CSharpExample { class Vehicle { public int vehicle_number; public string vehicle_type; public void StartVehicle() { Console.WriteLine("Vehicle Number "+ vehicle_number + " has been started"); } public void StopVehicle() { Console.WriteLine("Vehicle Number "+ vehicle_number + " has been stopped"); } } class Program { static void Main(string[] args) { Vehicle v1 = new Vehicle(); Vehicle v2 = new Vehicle(); v1.vehicle_number = 4852; v1.vehicle_type = "Car"; v2.vehicle_number = 7421; v1.vehicle_type = "Truck"; v1.StartVehicle(); v2.StopVehicle(); Console.ReadLine(); } } }
Here in the CSharpExample namespace, above the Program class. Another class named Vehicle has been added. It contains two member variables vehicle_number and vehicle_type of type integer and string respectively. The Vehicle class also contains two methods named StartVehicle and StopVehicle. Both of these methods do not take any parameter and have return type of void which means they do not take any parameter.
Creating an object of a class is a simple process. Inside the main method of Program class, two objects of Vehicle class have been created namely v1 and v2. To create a new object, new keyword is used followed by the constructor of the class which is Vehicle() in this case.
To access member variable, a dot “.” Operator is used as has been used to set the vehicle_number and vehicle_type in the above case. To call a method, again a dot “.” operator is used to separate the object name and the method to be called. The member variables and methods of the Vehicle objects v1 and v2 have been accessed in the following lines of the last example:
v1.vehicle_number = 4852; v1.vehicle_type = "Car"; v2.vehicle_number = 7421; v1.vehicle_type = "Truck"; v1.StartVehicle(); v2.StopVehicle();
The output of the above code would be as follows:
Vehicle Number 4852 has been started
Vehicle Number 7421 has been stopped
For more detailed C# programming tutorials, check out courses at Udemy.com.
Recommended Articles
Top courses in C# (programming language)
C# (programming language) students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.