C# Part 5: Methods In C#

C# Part 5: Methods In C#


Please Subscribe Youtube| Like Facebook | Follow Twitter

Methods in C#

In this article, we will provide a detailed overview of Methods in C# and provide examples of how they are used in C# programming.

One of the most important features of C# is methods. Methods are blocks of code that perform specific tasks, and they are essential building blocks of any program. In this article, we will explore methods in C# in detail. We will cover what methods are, how to create them, and how to use them in your programs.

Methods are used to break down large programs into smaller, more manageable pieces. They can also be reused in different parts of a program or in different programs altogether. Methods take input parameters and return output parameters, making them very versatile.

Syntax

The syntax for creating a method in C# is as follows:

<Access Specifier> <Return Type> <Method Name> (<Parameters>)
{
    // Code to be executed
    return <Value to be returned>;
}

Let’s break down each component of this syntax:

Access Specifier: This is an optional keyword that determines the level of access to the method. It can be public, private, protected, or internal. If no access specifier is specified, the default is private.

Return Type: This specifies the data type of the value that the method returns. It can be any valid C# data type, including user-defined data types.

Method Name: This is the name of the method. It should be descriptive and should indicate what the method does.

Parameters: These are optional input values that the method requires to perform its task. They are enclosed in parentheses and separated by commas.

Code to be executed: This is the block of code that performs the specific task of the method.

Value to be returned: This is the value that the method returns. If the method does not return a value, this part of the syntax can be omitted.

Types of Methods in C#

C# supports two types of methods:

  1. Built-in methods
  2. User-defined methods

Built-in methods: These are pre-defined methods that are part of the .NET framework and can be used directly in your code without having to implement them. Examples of built-in methods in C# include Console.WriteLine, String.Concat, Math.Pow, etc.

User-defined methods: These are methods that are defined by the user to perform specific tasks that are not provided by built-in methods. They can be created within a class or outside a class as a standalone function. User-defined methods can have input parameters, can return a value, or can be void methods that do not return any value.

Creating and Calling a User-defined Method in C#

Here is an example of a method in C#:

using System;

public class Program
{
    public static void Main()
    {
        int num1 = 10;
        int num2 = 20;

        int sum = Add(num1, num2);

        Console.WriteLine("The sum of {0} and {1} is {2}", num1, num2, sum);
    }

    public static int Add(int x, int y)
    {
        int result = x + y;
        return result;
    }
}

Output

The sum of 10 and 20 is 30

In this example, we have defined a method called Add that takes two integer parameters, x and y, and returns their sum. We have also defined a Main method, where we have declared two integer variables, num1 and num2, and assigned them values of 10 and 20, respectively. We then called the Add method, passing in num1 and num2 as arguments, and storing the returned value in a variable called sum. Finally, we used the Console.WriteLine method to print out the result to the console.

In summary, the Add method is an example of a simple C# method that takes input parameters, performs a specific task, and returns a value.

Example Method in C# with no return type and no parameters

using System;

public class Program {
    public static void Main() {
        Program program = new Program();
        program.SayHello();
    }
    
    public void SayHello() {
        Console.WriteLine("Hello, world!");
    }
}

Output

Hello, world!

Passing Parameters to a Method in C#

When calling a method in C#, you can pass parameters to the method to provide input values for the method to operate on. You can pass parameters to a method in C# using two methods:

  1. Pass-by-Value
  2. Pass-by-Reference

Pass-by-Value

In this method, a copy of the value of the parameter is passed to the method. This means that changes made to the parameter within the method do not affect the original value of the parameter outside the method. By default, all value types in C# are passed by value. Here’s an example:

public static void Main()
{
    int num1 = 10;
    int num2 = 20;

    Console.WriteLine("Before calling method: num1 = {0}, num2 = {1}", num1, num2);

    Add(num1, num2);

    Console.WriteLine("After calling method: num1 = {0}, num2 = {1}", num1, num2);
}

public static void Add(int x, int y)
{
    x = x + 5;
    y = y + 10;

    Console.WriteLine("Inside method: x = {0}, y = {1}", x, y);
}

Output

Before calling method: num1 = 10, num2 = 20
Inside method: x = 15, y = 30
After calling method: num1 = 10, num2 = 20

In this example, the Main method declares two integer variables, num1 and num2, and passes them as arguments to the Add method. The Add method takes two parameters, x and y, and adds 5 to x and 10 to y. However, since x and y are passed by value, the changes made to them inside the method do not affect the original values of num1 and num2 outside the method.

Pass-by-Reference

In this method, a reference to the memory location of the parameter is passed to the method. This means that changes made to the parameter within the method affect the original value of the parameter outside the method. In C#, you can pass a parameter by reference by using the ref or out keyword. Here’s an example:

public static void Main()
{
    int num1 = 10;
    int num2 = 20;

    Console.WriteLine("Before calling method: num1 = {0}, num2 = {1}", num1, num2);

    Add(ref num1, ref num2);

    Console.WriteLine("After calling method: num1 = {0}, num2 = {1}", num1, num2);
}

public static void Add(ref int x, ref int y)
{
    x = x + 5;
    y = y + 10;

    Console.WriteLine("Inside method: x = {0}, y = {1}", x, y);
}

Output

Before calling method: num1 = 10, num2 = 20
Inside method: x = 15, y = 30
After calling method: num1 = 15, num2 = 30

In this example, the Main method declares two integer variables, num1 and num2, and passes them as ref arguments to the Add method. The Add method takes two parameters, x and y, as ref parameters and adds 5 to x and 10 to y. Since x and y are passed by reference, the changes made to them inside the method affect the original values of num1 and num2 outside the method.

Conclusion

In conclusion, methods are an essential part of C# programming as they allow developers to encapsulate code and make it reusable. C# supports two types of methods, built-in and user-defined. Built-in methods are provided by the .NET Framework, and user-defined methods are created by the developer.

C# Beginner Tutorial Series

Please Subscribe Youtube| Like Facebook | Follow Twitter


Leave a Reply

Your email address will not be published. Required fields are marked *