Your One-Stop Solution to Methods in C# With Examples

C# has evolved to accommodate new workloads and changing program design methodologies. All this is made possible with the help of methods. A method is an independent collection of logical statements that work together to accomplish a user-defined goal. 

In this 'Methods in C#' tutorial, you will learn the major technical aspects of methods and their operating fundamentals.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

What Are Methods in C#?

Methods in a C# program are blocks of code or statements that allow the user to reuse code, conserve memory, time, and, most importantly, increase code readability.

Syntax: 

<AccessModifier> <returntype> <methodname>(parameter)

{

//Method Body

}

Now that you have a basic understanding of the definition and technicality of the methods in c#, you can now move on to the next segment to understand the operational components of methods.

What Are the Components of Methods in C#? 

C#Method-Component-Method-img1

There are mainly five components of methods in C# as explained below.

  • Access Modifier

It specifies the method's access type, i.e., how it can be accessed in your application.

In C#, there are three types of access modifiers:

Public: All methods have access to the code.

Private: Only the current method has access to the code.

Protected: It is only available within the same class or a subclass of that class.

  • Return Types

The return type specifies the data type of the value returned by the method. The return type is void if the method returns null. In C#, the return type can be int, void, double, string, and so forth.

  • Method Name

It indicates the name of the user-defined method that the user invokes, for example, factorial ().

  • Method Body

It refers to the line of code that describes the duties executed by the method during its execution. It is kept in braces.

  • Parameter List

A comma-separated list of the input parameters is defined within the open parenthesis, preceded by their data type. If there are no parameters, empty parentheses () must be used.

There are three types of parameters: 

  • Value Parameter 
  • Reference Parameter
  • Output Parameter

So far, you took a look at the major components of methods in c# and the access modifiers in c#. Now, you will explore the practical implementation and different ways to pass parameters in a method.

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

Implementation of Methods in C# 

In C#, methods can be implemented in four ways, and accordingly, they are classified based on their return type and input arguments. The four possible cases are discussed below:

1. Without Parameters and Without Return Type:

C#-Method-Parameter1-img1

You will not give any parameters, and the method will be of void data type. 

Code:

using System;

namespace C__method

{

    class Program

    {

        static void print()

        {

            Console.WriteLine("Welcome to Simplilearn");

        }

        static void Main(string[] args)

        {

            print();  

        }

    }

}

C#-Method-Parameter1-img2.

2. Without Parameters and With Return Type:

C#-Method-Parameter2-img1

You will not give any parameters, and the method will have a return data type like int. 

Code:

using System;

namespace C__method

{

    class Program

    {

     static int Product()

     {

           int x = 7, y = 22, res;  

           res = x * y;

           return res;

         }       

        static void Main(string[] args)

        {

         int getresult=Product();

          Console.WriteLine(getresult);

        }

    }

}

C#-Method-Parameter2-img2

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

3. With Parameters and Without Return Type:

C#-Method-Parameter3-img1

You will give parameters, and the method will have a void return datatype.

Code:

using System;

namespace C__method

{

    class Program

    {

        static void circumference(int r)

        {

            Console.WriteLine("circumference:"+ 2*(22/7)*r);

        }        

        static void Main(string[] args)

        {

            int rad=7;

            circumference(rad);

        }

    }

}

C#-Method-Parameter3-img2.

4. With Parameters and With Return Type:

C#-Method-Parameter4-img1.

You will give any parameters, and the method will have a return data type like int.

Code: 

using System;

namespace C__method

{

    class Program

    {

        static int factorial (int n)

        {

            int f1 = 1;

            for (int i = 1; i<= n; i++)

            {

                f1 = f1 * i;

            }

            return f1;

        }

        static void Main(string[] args)

        {

            int n=4, getresult;

            getresult=factorial(n);

            Console.WriteLine(getresult);

        }

    }

}

C#-Method-Parameter4-img2

By now, you must have gained a good grip on the technical aspects of c# methods. Next, look at some benefits of methods in C#.

Benefits of Using Methods in C#

  • The method ensures that the program is well-structured.
  • The use of methods improves the readability of the code.
  • It's an efficient approach for the user to reuse existing code.
  • It reduces the amount of time it takes to execute and the amount of memory it uses.
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Next Steps

"Operators and Expressions in C#" can be your next lesson in learning the journey of C#. Operators are symbols that can be used to do tasks with operands. Operators are used to manipulating variables and values. In C#, an expression is a collection of operands (variables, literals, method calls) and operators. An expression must have at least one operand but may or may not have any operators.

Simplilearn is the world's most popular online Bootcamp for learning digital economy skills, and it's here to help you do just that. Digital marketing and data science are just a few of the subjects it covers in-depth in the online courses.

If you have got any questions or comments regarding this 'Methods in C#' lesson, please leave them in the comments section below. Our experienced team is available 24/7 and will respond to all of your questions as soon as possible.

If you're interested in learning more about software development and possibly working in the field, you've come to the right place. Simplilearn's Software Development courses are delivered in collaboration with the Caltech CTME and the Indian Institute of Technology, Kanpur. Each of these courses covers the fundamentals of data structures and algorithms in addition to more advanced concepts such as Competitive Programming. As a software engineer, you'll learn about data structures such as trees, graphs, and queues, among other things.

Happy learning!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.
  • *According to Simplilearn survey conducted and subject to terms & conditions with Ernst & Young LLP (EY) as Process Advisors