C# Return Values

Methods can return values to the caller using the return statement. The return type of the method must match the type of value it returns.

Key Topics

1. Defining Return Type

Example: Method with Return Type

static int Multiply(int a, int b)
{
    return a * b;
}

2. Using the Return Statement

Example: Returning a Value

int result = Multiply(4, 5);  // result is 20

3. Storing Return Values

Example: Using the Returned Value

Console.WriteLine("Result: " + result);  // Outputs: Result: 20

Key Takeaways

  • The return type specifies what type of value a method returns.
  • The return statement exits the method and returns a value.
  • Returned values can be stored in variables for later use.
  • Returning values from methods enhances their usefulness and flexibility.