Table of contents
1.
Introduction
2.
What is Method?
3.
Defining Methods in C#
4.
Calling Methods in C#
5.
Types of Methods
6.
Difference between Method and Function
7.
FAQS
8.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Methods in C#

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

 A method is a block of code consisting of a series of statements. A program causes the statements to be executed by calling the method and specifying any required arguments. In C#, each instruction is executed within the context of a method.

The Main method is the entry point for every C# application, and it is called when the program is launched by the common language runtime (CLR). The compiler generates the Main method in an application that uses top-level statements and contains all top-level statements.

What is Method?

A method is a code that contains a series of statements that work together to complete a task. Every C# program contains at least one class with the method Main.

To use a method, you must first-

  • Define the method
  • Call the method

Defining Methods in C#

When you define a method, you declare the elements that make up its structure. In C#, the syntax for defining a method is as follows:

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

   Method Body

}

The following are the various components of a method:

Access Specifier specifies the method's access type, i.e., where it can be accessed in your application. Types of access modifiers in C#: public, protected, and private.

Return type: A method may return a value as its return type. The return type is the data type of the value returned by the method. If no values are returned by the method, the return type is void.

Method name: A method name is a case-sensitive one-of-a-kind identifier. It cannot be identical to any other identifier declared in the class.

Parameter list:  Within the enclosed parenthesis, a comma-separated list of the input parameters is defined, preceded by their data type. If there are no parameters, empty parentheses () must be used.

Method Signature:  It is defined primarily by two parameters (the number of parameters, the type of parameters, and the order of the parameters). The first is Method Name, and the second is its Parameter list.

Method Naming: The name of a method or function in any programming language, whether C++, Java, or C#, is essential and is primarily used to call that method for execution. A user should follow the following pre-defined rules when naming methods:

  • The name of the method must be a Noun or a Verb.
  • Its naming should be done so that it describes the method's purpose.
  • The initial letter of the method name can be a small letter or a capital letter; however, the capital letter is preferred.

These guidelines are not mandatory, but they are recommended. In general, a method has a unique name within the class in which it is defined, but because method overloading is permitted in C#, a method may have the same name as other method names within the same class.

Method body: This contains the instructions for completing the required activity.

In the below Example, GetUserDetails is the Method Name, and it takes two parameters: a string called name and an integer called age and returns a string called info.

using System;
namespace Tutlane
{
     class Program
     {
         public string GetUserDetails(string name, int age)
         {
            string info = string.Format("Name: {0}, Age: {1}", name, age);
            return info;
         }
     }
}

Calling Methods in C#

You can invoke a method by using its name along with the parameter. To execute the function, you must call it in the Main() method after creating it. First, create an object of the containing class to call a method, then call the method using the dot(.) operator. If a method is static, there is no need to create an object; instead, you can call it directly followed by the class name. The following example demonstrates this.

In the Below Example, we call the same function we declared earlier.

using System;
namespace Tutlane
{
     class Program
     {
         static void Main(string[] args)
         {
            Program p = new Program();
            string result = p.GetUserDetails("Amisha Purswani", 20);
            Console.WriteLine(result);
         }
         public string GetUserDetails(string name, int age)
         {
            string info = string.Format("Name: {0}, Age: {1}", name, age);
            return info;
         }
     }
}

Output:

Name: Amisha Purswani, Age: 20

Types of Methods

  1. Method Overloading: Method Overloading is a term used to describe a method with the same name but a distinct signature. 
  2. Virtual Method: Virtual methods are implemented in the base class and can be overridden in the derived class.
  3. Anonymous Method: An anonymous method is a code method with no name. It's made with the delegate keyword and doesn't need a name or a return type.
  4. Abstract Method:  A method with an abstract modifier is an abstract method. Because abstract methods have no implementation, they are preceded by a semicolon rather than a typical method block. All abstract methods must be implemented by derived classes of the abstract class.
  5. Static Method: A static method may be invoked without first generating a class object. It is referred to by the class name or a reference to the class's object.

Difference between Method and Function

The following table describes the difference between methods and functions.

Methods

Functions

Functions are independent, implying they may be defined independently of the class. The main() method in C,C++ is an example.  Methods do not exist in isolation; they are always defined in the context of a class. In C#, for example, the main() function is defined within a class.
Functions are called independently. Methods are called using instance or object.
Functions are the self-describing units of code. Methods are used to manipulate instance variables of a class.

We are done with the blog. Let's move to faqs.

FAQS

  1. What is a method in C#?
    A method is a piece of code that is only executed when it is called.
  2. Why are methods used? 
    Methods are used to carry out specific tasks.
  3. What is the difference between method and function?
    Since C# is an OOP language with no functions declared outside of classes, all functions in C# are methods.
  4. Can a method not have a return type?
    Yes, if the method returns no values, the return type is void.
  5. What is the syntax for writing a method?
    In C#, the syntax for defining a method is as follows:

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

   Method Body

}

Key Takeaways

In this article, we have extensively discussed methods in C# and have seen their syntax and examples.

Related links:

We hope that this blog has helped you enhance your knowledge regarding Methods in C# and if you would like to learn more, check out our articles in the code studio library. Do upvote our blog to help other ninjas grow. Happy Coding!
 

Live masterclass