Sobes.tech
Junior — Middle

How is a method declared and implemented in C#?

sobes.tech AI

Answer from AI

In C#, a method is declared inside a class or struct with access modifiers, a return type, a method name, and a parameter list. The implementation of the method involves writing the method body within curly braces.

Example:

public class Calculator
{
    // Method that adds two numbers and returns the result
    public int Add(int a, int b)
    {
        return a + b;
    }
}

Here, public is the access modifier, int is the return type, Add is the method name, (int a, int b) are the method parameters, and the method body returns the sum of the parameters.