C# Methods / Functions with Examples

In c#, Method is a separate code block and that contain a series of statements to perform a particular operations and methods must be declared either in class or struct by specifying the required parameters.

 

Generally, in c# Methods are useful to improve the code reusability by reducing the code duplication. Suppose if we have a same functionality to perform in multiple places, then we can create a one method with required functionality and use it wherever it is required in application.

Syntax of C# Methods

As discussed, in c# Methods must be declared either in a class or struct by specifying the required access level, return type, name of the method and any method parameters like as shown below.

 

class class_name

    {

        ...

        ...

        <Access_Specifier> <Return_Type> Method_Name(<Parameters>)

        {

            // Statements to Execute

        }

        ...

        ...

    }

If you observe above syntax, we defined a method in class with various parameters, those are

 

Access_Specifier - It is used to define an access level either public or private, etc. to allow other classes to access the method. If we didn’t mention any access modifier, then by default it is private.

 

Return_Type - It is used to specify the type of value the method can return. In case, if method is not returning any value, then we need to mention void as return type.

 

Method_Name - It must be a unique name to identify the method in a class.

 

Parameters - The method parameters are used to send or receive a data from method and these method parameters are enclosed within parentheses and are separated by commas. In case, if no parameters are required for a method then, we need to define a method with empty parentheses.

 

In c#, both methods and functions are same, there is no difference and these are a just different terms to do the same thing in c#.

Following are the examples of defining the different type of methods in c# programming language.

 

class Users

    {

        public void GetUsers() {

        // Statements to Execute

        }

        private void InsertUserDetails(string name, int age) {

        // Statements to Execute

        }

        protected string GetUserDetails(int userid)

        {

            // Statements to Execute

        }

    }

If you observe above example, we defined a different methods with different access modifiers, return types and with different parameters based on our requirements.

 

Now we will see the complete example using the methods in c# programming language.

C# Methods Example

Following is the example of using methods in c# programming language.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            Program p = new Program();

            string result = p.GetUserDetails("Suresh Dasari", 31);

            Console.WriteLine(result);

            Console.WriteLine("Press Enter Key to Exit..");

            Console.ReadLine();

        }

        public string GetUserDetails(string name, int age)

        {

            string info = string.Format("Name: {0}, Age: {1}", name, age);

            return info;

        }

    }

}

If you observe above example, we created a method called GetUserDetails and passing a parameters to perform required operations and we are accessing GetUserDetails method by creating an instance of Program class in Main method to show the result.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Methods or Functions Example Result

 

This is how we can use methods in c# applications based on our requirements.

C# Static Methods

In c#, if we create a methods with static, then we can directly invoke those methods from class level without creating an object.

 

If you observe above example Main method is static and we are invoking GetUserDetails method by creating an instance of Program class. Instead, if we create a method with static, then we can directly access those methods in Main() method without creating an instance of object.

 

Following is the example of creating a methods with static to access those methods directly in Main() method without creating an instance of object in c#.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            string result = GetUserDetails("Suresh Dasari", 31);

            Console.WriteLine(result);

            Console.WriteLine("Press Enter Key to Exit..");

            Console.ReadLine();

        }

        public static string GetUserDetails(string name, int age)

        {

            string info = string.Format("Name: {0}, Age: {1}", name, age);

            return info;

        }

    }

}

If you observe above example, we created a method GetUserDetails() with static so we are able to access that method directly in Main() method without creating an instance of class object.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Static Methods or Functions Example Result

 

This is how we can create static methods in c# applications to access without creating an instance of object based on our requirements.

C# Methods with No Return Type and Parameters

As per our requirements, we can create a methods in c# applications with or without return types and parameters. If we use void as return type for the method, then that method won’t return any value.

 

Following is the example of creating a method without having any return value and parameters in c# programming language.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            GetUserDetails();

            Console.WriteLine("Press Enter Key to Exit..");

            Console.ReadLine();

        }

        public static void GetUserDetails()

        {

            string name = "Suresh Dasari";

            int age = 31;

            Console.WriteLine("Name: {0}, Age: {1}", name, age);

        }

    }

}

If you observe above example, we created a method called “GetUserDetails” without having any return type (void) and parameters.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Methods with No Return Type and Parameters Example Result

 

This is how we can create methods in c# without having any return type and parameters based on our requirements.

C# Passing Parameters to Methods

Suppose if we create a method with parameters in c#, then we need to pass a parameters to that method while calling it in our application.

 

In c#, we have a different ways to pass a parameters to the method, those are

 

ParametersDescription
Value Parameters These are called as “input parameters” and these parameters will pass a copy of original value instead of original parameters. So the changes made to the parameters in called method will not have an effect on the original values when control returns to the caller.
Reference Parameters These are called as “input/output parameters” and these will pass a memory reference of original parameters. So the changes made to the parameters in called method will have an effect on the original values when control returns to the caller.
Output Parameters These are called as “output parameters” and these are more like reference type parameters but only difference is we don’t need to initialize it before passing.

In next chapters, we will learn more about parameter passing to methods or functions in detailed manner with examples in c# programming language.