C# Methods :

What Is a "Method" ?

A method is a basic part of a program. It can solve a certain problem, eventually take parameters and return a result.

A method represents all data conversion a program does, to resolve a particular task. Methods consist of the program’s logic. Moreover they are the place where the “real job” is done. That is why methods can be taken as a base unit for the whole program. This on the other hand, gives us the opportunity, by using a simple block, to build bigger programs, which resolve more complex and sophisticated problems. Below is a simple example of a method that calculates rectangle’s area:

static double GetRectangleArea(double width, double height)
{
double area = width * height;
return area;
}


How to Declare, Implement and Invoke a Method?

This is the time to learn how to distinguish three different actions related to existing of a method: declaring, implementation (creation) and calling of a method.

  • Declaring a method we call method registration in the program, so it can be successfully identified in the rest of the program.
  • Implementation (creation) of a method is the process of typing the code that resolves a particular task. This code is in the method itself and represents its logic.
  • Method call is the process that invokes the already declared method, from a part of the code, where a problem, that the method resolves, must be solved.

Method Declaration :

To declare a method means to register the method in our program. This is shown with the following declaration:

[static] <return_type> &l<method_name>([<param_list>])

To clarify the elements of method’s declaration :

static void Main(string[] args)

Note : In the C# language, a method can be declared only between the opening "{" and the closing "}" brackets of a class.


Implementation :

After a method had been declared, we must write its implementation. As we already explained above, implementation (body) of the method consists of the code, which will be executed by calling the method. That code must be placed in the method’s body and it represents the method’s logic.

Method body we call the piece of code, that is placed in between the curly brackets "{" and "}", that directly follow the method declaration.

static <return_type> <method_name>(&l<parameters_list>)
{
// … code goes here – in the method's body …
}


method body example :

static void PrintLogo()
{// Method's body starts here
Console.WriteLine("Microsoft");
Console.WriteLine("www.microsoft.com");
} // … And finishes here


Note : Method can NOT be declared inside the body of another method.


Invoking a Method :

Invoking or calling a method is actually the process of execution of the method’s code, placed into its body. It is very easy to invoke a method. The only thing that has to be done is to write the method’s name <method_name>, followed by the round brackets and semicolon ";" at the end:

<method_name>();

Later will see an example for when the invoked method has a parameter list(in the case here the method has no parameters).

To clarify how method invocation works, the next fragment shows how the method PrintLogo() will be called:

PrintLogo();

Result of method’s execution is:

Microsoft
www.microsoft.com

Computer Science Engineering

Special Notes

It's a special area where you can find special questions and answers for CSE students or IT professionals. Also, In this section, we try to explain a topic in a very deep way.

CSE Notes