C# > Core C# > Methods and Parameters > Method Declaration and Calling

Simple Method Declaration and Calling in C#

This example demonstrates how to declare a simple method in C# that takes no parameters and returns a value. It also shows how to call or invoke that method from within the `Main` method. This is the fundamental building block for code modularity and reusability.

Basic Method Declaration

The `AddTwoNumbers` method is declared using the `public static int` keywords. `public` means the method can be accessed from anywhere. `static` means the method belongs to the class itself, not an instance of the class. `int` specifies that the method returns an integer value. The `Main` method calls `AddTwoNumbers` and stores the returned value in the `result` variable. Finally, it prints the result to the console.

using System;

public class Example
{
    public static int AddTwoNumbers()
    {
        int a = 5;
        int b = 10;
        int sum = a + b;
        return sum;
    }

    public static void Main(string[] args)
    {
        int result = AddTwoNumbers();
        Console.WriteLine("The sum is: " + result);
    }
}

Concepts Behind the Snippet

Methods are essential for breaking down complex programs into smaller, manageable units. They promote code reusability and improve code readability. The return type of a method must match the type declared in the method signature. The scope of a method (e.g., `public`, `private`) determines its accessibility.

Real-Life Use Case

Imagine you're building a calculator application. You could have separate methods for addition, subtraction, multiplication, and division. Each method would perform its specific operation, making the main application logic cleaner and easier to understand.

Best Practices

Keep methods focused on a single task. Use descriptive names for methods and variables. Avoid methods that are too long; break them down into smaller, more manageable units. Always validate input parameters where applicable.

Interview Tip

Be prepared to explain the difference between `static` and instance methods. Also, understand the concept of method overloading and how it contributes to polymorphism.

When to Use Them

Use methods whenever you have a block of code that performs a specific task and may need to be executed multiple times. Methods also help to improve code organization and readability, even if the code is only executed once.

Memory Footprint

Methods themselves do not consume significant memory. However, variables declared within a method consume memory while the method is executing. Once the method completes, the memory allocated to those variables is released.

Alternatives

For very simple operations, you might consider using lambda expressions or anonymous methods. However, for more complex logic, methods are generally preferred for clarity and maintainability.

Pros

  • Code reusability
  • Improved code organization
  • Increased readability
  • Reduced code duplication
  • Easier debugging

Cons

  • Slight overhead due to method call (negligible in most cases)
  • Potential for increased complexity if methods are not well-designed

FAQ

  • What does the 'static' keyword mean in a method declaration?

    The `static` keyword means that the method belongs to the class itself and not to a specific instance of the class. You can call a static method directly using the class name, without creating an object of the class.
  • What happens if I don't return a value from a method that's supposed to return one?

    If you declare a method with a return type (e.g., `int`, `string`, `bool`), the method must return a value of that type. If you don't, the compiler will throw an error. If your method doesn't need to return a value, use the `void` keyword as the return type.
  • Can I have multiple methods with the same name?

    Yes, this is called method overloading. You can have multiple methods with the same name as long as they have different parameter lists (different number of parameters, different types of parameters, or different order of parameters).