The method is a set of programming statements which performs a specific task. In C# Main () method is the entry point of the program and it is called by CLR.

Methods help to maintain the logic based upon the task means we can create different methods to write and read files. It also helps to reuse code; once the method is defined we can call it whenever necessary.

The method takes zero or more inputs and returns zero or one output value. It is best practice to divide your program into small methods.

Method in C#

Defining method:

The syntax for defining method is as below –

 

<access_specifier> <return_type>Method_Name(parameter_list) 
{ 
   //Statements to be executed 
}

 

Here, access_specifier is optional part and it decides which part of the program can access this method. If it’s not given, then it is private and we can access method only in the class in which it is defined. If it’s public it is accessible from anywhere.

The second part of method definition is return type which specifies the type of value method can return. The return type can be any valid C# data type including user defined classes. If the method is not returning any value then return type must be void.

The next part is a method name, it is any identifier name given to the method. The input values are given to the method in the form of the parameter list, which is enclosed within brackets. These three parts are together called as the signature of the method.

The body of methods is enclosed in curly braces.

 

Calling Methods:

Once a method is defined we can call it many times whenever required. We can call the method by just method name and passing parameters like below in same class.

Recommended Post   Anonymous Classes and Methods in C#

Method_Name();

 

To access instance method in other class we need a class object. And in the case of the static method, we need the only class name.

 

Object_Name.Method_Name();// Accessing instance method outside of class 
Class_Name.Method_Name();//Accessing static method of class

 

When above statement is executed control goes to the method and executed all the statements in method body and return to next line after the method call.

Following program shows defining and calling methods –

 

using System; 
namespace MethodDemonstraion 
{ 
    class MehodDemo 
    { 
         public void Show() 
         { 
              Console.WriteLine("This is Show() method."); 
         } 
         public void Display() 
         { 
              Show(); //Calling method inside same class 
              Console.WriteLine("This is Display() method."); 
         } 
    } 
     class ProgramEntry 
    { 
         static void Main(string[] args)
        { 
            MethodDemo demo1 = new MethodDemo(); 
            demo1.Display();    //Calling method in other class 
            Console.ReadLine(); 
        } 
    }
}

 

The output of above program is –

 

This is Show() method. 
This is Display() method.

 

Returning Value:

 A method can return only single value. It may be any inbuilt in data type value or object of the user-defined class. The data type of value must be same as the return type of method otherwise, the compiler throws the error.

Recommended Post   C# Language Fundamentals

To return value keyword ‘return’ is used. Once return statement executed then control goes out of the method to the next line where from the method is called. And all the statements after return are skipped.

using System; 
namespace ReturningValues 
{
    class Reutrning_Value_Demo 
   {
       int value = 120; 
       public int ReturnValue() 
       {
            Console.WriteLine("In ReturnValue method"); 
            return value;   //Returning value 
            Console.WriteLine("After returning value"); 
       } 
    } 
    class Program 
    {
         static void Main(string[] args) 
        { 
            Reutrning_Value_Demo demo = new Reutrning_Value_Demo(); 
            Console.WriteLine(demo.ReturnValue()); 
            Console.ReadLine(); 
        }
     } 
}

 

The output of above program is –

In ReturnValue method 
120

Non-Parameterized Methods:

The methods which are not taking any input from parameter list are called as non-parameterized methods. In above example ReturnValue() method is non-parameterized method.

 

Parameterized Methods:

A parameterized method takes one or more input values and performs some operation on it. The method signature defines the number and type of parameters required.

When calling a method, the number of passed values and their type must match the signature of the method. The values that are passed to parameters in the method call is known as arguments.

 

Example: 


using System; 
namespace ParameterizedMethod 
{ 
    class ParameterizedMethod_Demo 
    { 
        public void Addition(intFirstNum, int SecondNum)
        { 
            Console.WriteLine("Addition is {0}",(FirstNum+SecondNum)); 
        } 
    }
    class Program
    {
         static void Main(string[] args) 
         { 
             ParameterizedMethod_Demo demo = new ParameterizedMethod_Demo(); 
             demo.Addition(12,23);   //Calling Method 
             Console.ReadLine(); 
         } 
     }
}

 

Recommended Post   C# Boxing and Unboxing

The output of above program is –

 

Addition is 35

 

Optional Arguments:

While using parameterized methods it is necessary to pass values for all parameters, and if we don’t provide all values in method parenthesis then the compiler will raise an error. Using optional arguments we provide some default value to parameters.

If that parameter is omitted from method call then compiler takes default value and if the value is provided in method call then the default value is replaced with provided value. The optional parameters must be at the end of the parameter list.

 

Example: 

 

using System; 
namespace OptionalArgument 
{ 
    class OptionalArgument_Demo 
    { 
        public void Multiplication(int x,int y=1)   //Method with defalut argument 
        { 
            Console.WriteLine("Multiplication is {0} ",(x*y)); 
        } 
    } 
    class Program
    { 
        static void Main(string[] args) 
        { 
            OptionalArgument demo = new OptionalArgument (); 
            demo.Multiplication(4, 5);  //Method call with both arguments 
            demo.Multiplication(3);     //Method call with only one argument 
            Console.ReadLine();
        }
     }
}

 

The output of above program is –

 

Multiplication is 20 
Multiplication is 3

 

SHARE
Previous articleWhat is Domain?
Next articleC# Classes & Objects