C# Method Examples – Programming, Pseudocode Example, C# Programming Example
C# C# Console Methods

C# Method Examples

In this tutorial, we will learn about the C# method with the help of examples.

A C# method is a collection of statements that are grouped together to perform an operation. When you call the Console.WriteLine() method, for example, the system actually executes several statements in order to display a message on the console.

Now you will learn how to create your own methods with or without return values, invoke a method with or without parameters, and apply method abstraction in the program design.

Creating Method

Considering the following example to explain the syntax of a method −

Syntax

Here,

  • public static − modifier
  • int − return type
  • methodName − name of the method
  • a, b − formal parameters
  • int a, int b − list of parameters

Method definition consists of a method header and a method body. The same is shown in the following syntax −

Syntax

The syntax shown above includes −

  • modifier − It defines the access type of the method and it is optional to use.
  • returnType − Method may return a value.
  • nameOfMethod − This is the method name. The method signature consists of the method name and the parameter list.
  • Parameter List − The list of parameters, it is the type, order, and number of parameters of a method. These are optional, method may contain zero parameters.
  • method body − The method body defines what the method does with the statements.

Example

Here is the source code of the above defined method called min(). This method takes two parameters num1 and num2 and returns the maximum between the two −

For using a method, it should be called. There are two ways in which a method is called i.e., method returns a value or returning nothing (no return value).

The process of method calling is simple. When a program invokes a method, the program control gets transferred to the called method. This called method then returns control to the caller in two conditions, when −

  • the return statement is executed.
  • it reaches the method ending closing brace.

The methods returning void is considered as call to a statement. Lets consider an example −

The method returning value can be understood by the following example −

Following is the example to demonstrate how to define a method and how to call it −

Example

The following program shows an example of passing parameter by value. The values of the arguments remains the same even after the method invocation.

Output:

Method Overloading

When a class has two or more methods by the same name but different parameters, it is known as method overloading. It is different from overriding. In overriding, a method has the same method name, type, number of parameters, etc.

Let’s consider the example discussed earlier for finding minimum numbers of integer type. If, let’s say we want to find the minimum number of double type. Then the concept of overloading will be introduced to create two or more methods with the same name but different parameters.

The following example explains the same −

This will produce the following result −

Output

Overloading methods makes program readable. Here, two methods are given by the same name but with different parameters. The minimum number from integer and double types is the result.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.