In C#, a method is a code block that contains a series of statements
designed
to perform a specific task. Methods are a fundamental building
block of
C# programs, allowing you to encapsulate functionality,
improve code
readability, and promote code reuse.
using System;
class Program
{
// Main method, the entry point of the program
static void Main(string[] args)
{
// Call the SayHello method
SayHello();
//
Call the AddNumbers method and store the result in a variable
int sum = AddNumbers(5, 7);
Console.WriteLine("Sum of numbers: "
+ sum);
// Call the MultiplyNumbers method with named arguments
int product = MultiplyNumbers(num2:
3, num1: 4);
Console.WriteLine("Product of numbers: " + product);
}
// Method that prints a simple greeting
static void SayHello()
{
Console.WriteLine("Hello, World!");
}
// Method that adds two numbers and returns the result
static int AddNumbers(int num1, int num2)
{
return num1 + num2;
}
// Method that multiplies two numbers and returns the result
static int MultiplyNumbers(int num1, int num2)
{
return num1 * num2;
}
}