Method
Thus a method or function is a program module (part of program) used simultaneously different instances in a program to perform specific task.
A method is a code block that contains a series of statements. A program causes the statements to be executed by calling the method and specifying any required method arguments. In C#, every executed instruction is performed in the context of a method. The Main method is the entry point for every C# application and it’s called by the common language runtime (CLR) when the program is started. In an application that uses top-level statements, the Main method is generated by the compiler and contains all top-level statements
using System; namespace Method { class Program { // method declaration public void display() { Console.WriteLine("Hello World"); } static void Main(string[] args) { // create class object Program p1 = new Program(); //call method p1.display(); Console.ReadLine(); } } }
Advantages of using methods
Some of the advantages of using Methods are mentioned below:
- The main advantage of code its reusability. You can write a method once and the same code is used for multiple times.
- It breaks a complex code into smaller methods making the problems comparative easier.
- It allows to develop user friendly code that becomes easier to debug.
- It occupies less memory space.
Types of methods In C#
- Pure virtual method.
- Virtual method.
- Abstract method.
- Partial method.
- Extension method.
- Instance method.
- Static method.
Construct of Method
It is important to know the construct of syntax of defining a method that distinguish from general programming. The format is illustrated as shown below:
<Modifier><Return type><Method name><Parameter list>
{
………………………………………
………………………………………
Statements
………………………………………
………………………………………
<return value>
}
For example,
Constructor
Constructor is a member method defined with the same name as class name and is used to initialize the instance variables of the class. In the sample program shown earlier, the constructor is as defined alongside. We will discuss about constructor in details later in this chapter. In the previous Part of this chapter, we have discussed about a specific programming structure of C# programming called class. As you know that class is simply a description about data members and member methods. When different objects of a class is created then each of them contains a separate copy of the instance variables. Moreover, the member methods are commonly allocated for all the objects.
A member method is mainly used to manipulate the instance variables of an object. It may happen that the instance variables are not initialized and are used in the execution of a member method, may yield incorrect result. This is why the initialization of the instance variables is necessary. may happen that the instance variables are not initialized and are used in the execution of a member method, may yield incorrect result. This is why the initialization of the instance variables is necessary. You can define a separate member method to initialize the instance variables. A program is illustrated below:
using System; namespace Constructor { class Car { // parameterless constructor Car() { Console.WriteLine("Car Constructor"); } static void Main(string[] args) { // call constructor new Car(); Console.ReadLine(); } } }
Constructor is a member method defined with the same name as class name and is used to initialize the instance variables of an object.
Features of a Constructor
Constructor has the following features:
- It is member method with the same name as class name.
- Constructor is automatically called at the time of creating an object.
- It is used only to initialize the instance variables of an object.
- Constructor does not perform any arithmetical or logical operation hence, it will never return a value. Thus, header of the constructor is declared without any return data type.
- If a class, includes a number of constructions then all of them will have same name but with different types of parameters. Hence, constructors are automatically overloaded.
- A constructor is always public.