Creating and Calling Method in C#

(Last Updated On: January 29, 2019)

Learn how to create and define method in C# language and how to call methods in C# program. Also, learn defining multiple methods and why to use methods in C# with example.

Syntax of method in C#

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

In below example,

public” is modifier

Void” is return type

Orange () – Orange is method name, () can be empty or can have list of parameters

public static void orange() {
 
		Console.WriteLine("This is Orange!!!");
	}

We will read modifier and return types and about “public static void” in details later.

For now, lets focus on method with empty parameter and void return type, void means, method returns nothing.

NOTE: Method in C# always has bracket () after the method name with / without parameters and must have return type.

 

Example of Creating and Defining a Method in C#

This is how we create and define a method in a C# program. Notice the bracket () after the method named orange or after main method.

In below example, main() and orange() are methods. They are not returning anything. So, preceded with “void” return type.

Focus on method and its body. It does not have semi colon; after method name e.g. orange (); but orange () {}

public class Sample {
 
	public static void Main(String[] args) {
 
		
	}
 
	// Defining a method
	public static void orange() {
 
		Console.WriteLine("This is Orange!!!");
	}
 
}

How to call a method in C#?

As an example, in main() method, we are calling orange() method using syntax orange();

public class CSharpMethods {
 
	public static void Main(String[] args) {
 
		// This is how we call a method
		orange();
 
	}
 
	// Defining a method
	public static void orange() {
 
		Console.WriteLine("This is Orange!!!");
	}
 
}

IN BRIEF,

Method has modifiers like private and public etc. to restrict access of methods from outside of the class.

Return type shows what value a method is returning. If it does not return anything, then we write “void” keyword before method. If it returns int value then return type will be “int”, if it returns String value then, return type will be “String”.

Note that we will discuss “public static void” , meaning modifiers and return type in detail later. However, you will learn return type and parameters of method in C# language as the next post after understanding methods in C#.

 

WARNING:

We CANNOT define a method inside another method. But, we can call a method from any method.

For example, Below, apple() method definition is wrong as it has been defined inside the orange() method.

public class Sample {
 
	public static void Main(String[] args) {
 
	}
 
	// Defining a method
	public static void orange() {
 
		Console.WriteLine("This is Orange!!!");
		
		public static void apple() {
 
			Console.WriteLine("This is Orange!!!");
		}
	}
 
}

Correct way to do this is :

public class Sample {
 
	public static void Main(String[] args) {
 
	}
 
	// Defining a method
	public static void orange() {
 
		Console.WriteLine("This is Orange!!!");
		
		
	}
	
	public static void apple() {
 
		Console.WriteLine("This is Orange!!!");
	}
	
 
}

 

Defining multiple methods in C#

We can create and define multiple methods. In below example, two methods orange () and apple () has been defined and called in main () method.

Notice closely, how methods have been defined and called in main() method

public class CSharpMethods {
 
	public static void Main(String[] args) {
 
		orange();
		apple();
		
	}
	
	//defining methods
	public static void orange() {
 
		Console.WriteLine("This is Orange!!!");
	}
 
	
	public static void apple() {
 
		Console.WriteLine("This is apple!!!");
	}
}

NOTE: You can call a method from any other method.

 

Why to use Methods in C#?

We use method for readability and maintainability of an application.

#Bad Code

public class CSharpMethod2 {
 
	public static void Main(String[] args) {
		Console.WriteLine("Garlic");
		Console.WriteLine("Curd");
		Console.WriteLine("Paneer");
		Console.WriteLine("Orange");
		Console.WriteLine("Banana");
		Console.WriteLine("Butter");
		Console.WriteLine("Milk");
		Console.WriteLine("Lassi");
		Console.WriteLine("ButterMilk");
		Console.WriteLine("Mango");
		Console.WriteLine("Apple");
		Console.WriteLine("Potato");
		Console.WriteLine("Onion");
		Console.WriteLine("Tomato");
		
		
 
	}
}

#Good Code

public class CSharpMethod2 {
 
	public static void Main(String[] args) {
		fruitShop();
		vegShop();
		dairyShop();
 
	}
 
	public static void fruitShop() {
		Console.WriteLine("\nFRUITS");
		Console.WriteLine("Mango");
		Console.WriteLine("Apple");
		Console.WriteLine("Orange");
		Console.WriteLine("Banana");
 
	}
 
	public static void vegShop() {
		Console.WriteLine("\nVEGETABLES ");
		Console.WriteLine("Potato");
		Console.WriteLine("Onion");
		Console.WriteLine("Tomato");
		Console.WriteLine("Garlic");
 
	}
 
	public static void dairyShop() {
		Console.WriteLine("\nDAIRY");
		Console.WriteLine("Curd");
		Console.WriteLine("Paneer");
		Console.WriteLine("Butter");
		Console.WriteLine("Milk");
		Console.WriteLine("Lassi");
		Console.WriteLine("ButterMilk");
		
 
	}
}