C# Methods

Tutorials PanelPosted by

In this article, you will learn about how to create a method in C# and call it. Also, you will learn about various type of passing parameter to a method, and much more.

Method in C#

A method (or function) is a block of code that can be declare inside a class.  Methods are useful if you want to perform an action in more than one place in your code. They can be called anywhere in the solution or referenced to be used in other solutions.

Syntax of method in C#

        <Access Specifier> <Return Type> <Method Name>(Parameters)
            {
             //Method Body
            }

Example of a method in C#

 static  int Example1(int x, int y)
        {
            return x + y;
        }
        // Call the method
         static  void Main(string[] args)
        {
            int result;
            result = Example1(1, 2);
            Console.WriteLine(result);
            Console.ReadKey();
        }

Output

3

Void methods in C#

In a simple word, a void method is a method that does not return a value, but perform an action, such as adding two numbers and print the result out to the output.

Example of void method in C#

        // define the function 
        static void Example2(int x, int y)
        {
            Console.WriteLine(x + y);
        }
        // call the function 
        static void Main(string[] args)
        {
            Example2(5, 4);
            Console.ReadKey();
        }

Output

9

Passing parameter By Value

In C#, the default method to pass an argument is by value. When you pass a parameter by value to a function, a copy of the value is kept in the memory. Any changes you made to the copy of this value will not affect the original variable’s value in the caller.

Example of passing parameter by value in C#

        static void ExampleByValue(int x)
        {
            x = 5;
        }
        static void Main(string[] args)
        {
            int a = 0;
            ExampleByValue(a);
            Console.WriteLine(a);
            Console.ReadKey();
        }

Output

0

Notice that the value of ‘a’ remains ‘0’ in the main methods, regardless of changing its value in the fucntion ExampleByValue

Passing parameters By Reference

So far we have learnt that passing parameter by value will not change the value of the parameter in the caller. In the other hand, passing parameter by reference will create a reference for that parameter in the memory. Any change made to the parameter in the function will affect the original parameter’s value in the caller.

Example of passing parameter by reference in C#

        static void ExampleByReference(ref int x)
        {
            x = 5;
        }

        static void Main(string[] args)
        {
            int a = 0;
            ExampleByReference(ref a);
            Console.WriteLine(a);
            Console.ReadKey();
        }

Output

5

The parameter ‘a’ in the main method has changes as it was assigned a new value on the function ExampleByReference

C# Out Parameter

In C#, the out keyword causes the arguments to a method by reference. It’s similar to the ref argument, except the out parameter can be passed without any value to be assigned to it. The argument passed using the out keywords must be defined in the caller before being return from the method being called.

Example of out parameter in C#

        static void ExampleOutParam(out int x)
        {
            x = 5; // must be defined 
        }

        static void Main(string[] args)
        {
            int a; // no need to be defined 
            ExampleOutParam(out a);
            Console.WriteLine(a);
            Console.ReadKey();
        }

Output

5

Now the output is similar as the passing parameter by reference, except the parameter is not defined in the caller, but in the method being called

Leaving the parameter in the method being call undefined will cause the exception below:

The out parameter ‘x’ must be assigned to before control leaves the current method

Leave a Reply