Method Return Types and Parameters in C#

Learn what is method return types and parameters in C# with code example and simple explanations.

You will be learning…

  • how to return int and string data types in C# from a method.
  • Using void keyword to prevent returning data from a method.
  • methods parameters and how to call them by supplying the values known as arguments.

Method Return Types in C#

We have learned what is method in C# with Syntax and definition already in previous post and have learned basics about it. Now, lets learn about return type of a method in C#.

Syntax of method in C#

modifier returnValueType methodName(list of parameters) {
			 // Method body;
			//Group of statements
			}

Return type: void – The method returns nothing

We use “void” keyword if we want a method not to return anything but perform operations only / Execute group of statements.

public static void myMethod() {
		
//no return statement as return type of method is void
	}

NOTE: if return type is anything except void, then method must have “return “statement.

Return type: int – The method returns int data type

public static int myMethod() {
		
		//return int value 5
		return 2+3;
	}

NOTE: If you have return type “void”, then you don’t need to write “return” statement. As we have written return statement e.g. return 2+3; in above method that is returning int data type of value.

Return type: String – The method returns String data type of value

public static String myMethod() {
		
		//return String value Viswanath Annangi
		return "Viswanath Annangi";
	}

C# Code Example:

Lets see how we can call a method returning int value and use them. As an example, lets call the method myMethod() in main()method of C# program.

In main method, we have call the myMethod() that returns value 5. So, have created a int variable to store that value and display that using Console.WriteLine method.

public class Sample {
 
	public static void Main(String[] args) {
 
		int value = myMethod();
		Console.WriteLine("Value returned from method myMethod()= " + value);
 
	}
 
	public static int myMethod() {
 
		// return int value 5
		return 2 + 3;
	}
 
}

DOUBT: Can’t we call the method directly in Console.WriteLine. why to create a local variable of int type in main() method?

Answer: Yes, we can call the method directly in Console.WriteLine method as below, if we want to just check if myMethod() is returning correct value or not.

Console.WriteLine(“Value returned from method myMethod()= ” + myMethod());

But if you want to use the value returned by myMethod()further in the main() method, then you need to store that in a variable and use it further.

OK, so for we understand what return type of method in C# and how to call them and store their value.

Let’s see one more example of returning value from a method. Below example, method myMethod() returns a String value and it is called from main() method and display the returned value.

public class CSharpMethods {
 
	public static void main(String[] args) {
 
		//call the method. the method will return
// string value
		//Store that in a variable of string type
		String name = myMethod();
		
		Console.WriteLine("My name is :" + name);
	}
	
	public static String myMethod() {
		
		String name = "Viswanath Annangi";
		return name;
	}	
	
}

Output:
My name is :Viswanath Annangi

NOTE: methods in C# must have a return type. if not returning use return type “void”

Method Parameters in C#

If you look at the syntax of method, we have learned return type. Now, we will learn about method parameters in C# i.e. methodName(list of parameters).

Syntax of method

modifier returnValueType methodName(list of parameters) {
			 // Method body;
			//Group of statements
			}

A method receives value via parameter from where the method is called. It can have one or more parameters.
In below examples, the add method takes two int type of parameter i.e. int first and int second.
print method is taking one parameter of String type.

public static int add(int first, int second){}
public static int print(String msg) {}

 

How to call methods with arguments in C#?

Let’s see a complete simple example.

In this example, we have an add method with two int type parameters i.e. int add(int first, int second), that will calculate sum using both parameters first and second and return the sum.

In main() method, we are calling add method by supplying two int values, also, known as agreements. method add, will receives these two arguments in its 2 parameters first and second and return the sum to main() method.

public class Sample {
 
	public static void Main(String[] args) {
 
		// call the method add by supplying value
		int result = add(10, 20);
		
		Console.WriteLine("Sum returned =" + result);
	}
 
	public static int add(int first, int second) {
		// use parameter to find sum
		int sum = first + second;
 
		return sum;// return the sum
	}
 
}

 

Method Parameter Vs Method Arguments in C#

In method declaration e.g. add(int first, int second), variable first and second are known as method parameter list that we write them during declaration of a method.

When we call a method by supplying values e.g. int result = add(10, 20); in above program, then these values are known as method arguments.

Related Posts