In C# how do you call one method from another method where both method are inside of the same class?

Experience Level: Junior
Tags: C#

Answer

When both methods reside inside of the same class, for calling method you just need to write its name followed by rounded brackets. If the method has any parameters, then these parameters need to be provided inside the brackets.

In the following example first the method WriteHello is called with no parameters. Then the method WriteMessage is called with two parameters. The first parameter contains the string "Hello" and the second parameter contains the string "World". The method WriteMessage accepts these two strings and writes each of them on a separate line.

Example
public class Program 
{
  public static void Main() 
  {
    WriteHello();
	WriteMessage("Hello", "World");
  }  
  
  public static void WriteHello() 
  {
    Console.WriteLine("Hello World");
  }
  
  public static void WriteMessage(string message1, message 2) 
  {
    Console.WriteLine(message1);
    Console.WriteLine(message2);
  }
}
Related C# job interview questions
C# for beginners
C# for beginners

Are you learning C#? Try our test we designed to help you progress faster.

Test yourself