Loading, please wait...

A to Z Full Forms and Acronyms

Methods in C# | C# Tutorials

Nov 30, 2020 C#, CSharp, 2956 Views
Methods are group of sentences that perform a particular task. They can be user defined and some of them are already existing in the programming language. They are very mush useful in many aspects. Every C# program has at least one class with a method named Main.

Methods in C# | C# Tutorials

Methods are groups of sentences that perform a particular task. They can be user-defined and some of them are already existing in the programming language. They are very much useful in many aspects. Every C# program has at least one class with a method named Main.

Methods have their own advantages

  • They are reusable:

This is why it is used extensively in most of the programs. When we operate with methods they can be used anywhere as many times as we want.

  • Easy to test
  • Modification to a method does not affect the calling program.

Declaring Method

First thing is to declare a method. Once it is declared then it can be called in the program. The declaration of methods includes several elements.

The syntax for declaring method is

<Access Specifier> <Return Type> <Method Name>(Parameter List) {

   Method Body

}

  • Access Specifier: It is for giving visibility to the method. Whether it will be accessible to other classes or not.
  • Return Type: This is to return the value of the method after executing the statements. Its returns to where it was called.
  • Method Name: This should be unique and case sensitive. In C#, it is good practice to start with an uppercase letter when naming methods, as it makes the code easier to read.
  • Parameter List: The parenthesis in the syntax of declaring methods, are to pass data and receive data from the method. It is optional.

Example:

static void MyMethod() {  
    Console.WriteLine("I was called by method!");
} 

static void Main(string[] args){  
    MyMethod();
}

NOTE: void is for no return type.

Calling Methods

Calling a method required nothing more than the name of the method followed by parenthesis (). While calling the method we can parameters for the method.

For example:

MethodName( x, y );

NOTE: Methods can be called more than one time by repeating the same syntax.

While passing parameter there is a way to pass optional parameters too. While defining a method, the coder can specify some default values for optional parameters. What it does is that, in case the user calls the method with not sufficient parameters required by the method, it will use the optional parameters.

NOTE: The optional parameters are always mentioned after actual parameters.

So if anything is missing the optional parameters comes into use.

The parameters can be passed to the method in three ways.

  1. Value parameters: This is just a copy-paste of values from the argument to formal parameters. In case if there are any changes in the values it will not have any effect on the argument.
  2. Reference parameters: This method passes the address of the value which is to be sent, to the formal parameters. Any changes in the value will change the corresponding address arguments.
  3. Output parameters: This method helps in returning more than one value.
A to Z Full Forms and Acronyms