How to define and call methods in c#?

Methods are blocks of code that perform a specific task.

Example:


  public void Greet(string name)
  {
      Console.WriteLine("Hello, " + name + "!");
  }

  Greet("Alice");
  

Solution:

You can also use return types and parameters to make methods more dynamic:


  public string GetGreeting(string name)
  {
      return "Hello, " + name + "!";
  }

  string greeting = GetGreeting("Bob");
  Console.WriteLine(greeting);
  

Beginner's Guide to C#