Methods and return values
A method is a named operation that can accept input and return a result. Instead of repeating a calculation, define its behavior once and call it wherever needed. In a console program, a small helper can be written as a local function; the same parameter and return rules apply to ordinary methods.
using System;Console.WriteLine(CountItems(3, 4));static int CountItems(int boxes, int perBox){ return boxes * perBox;}The call supplies 3 and 4; the result of multiplication is returned before printing. These small nonnegative inputs avoid integer overflow.
Expected output (reasoned, not executed):
12Read the declaration
In static int CountItems(int boxes, int perBox), int before the name is the return type; the parentheses declare two integer parameters. Calling CountItems(3, 4) supplies arguments. return ends that invocation and supplies a value to the caller. The static local helper does not depend on surrounding local variables. An ordinary static method belongs to a type, as the final example shows.
Separate calculation from output
A method returning a value lets the caller store, compare or print it. A void method performs an action without producing a return value for the caller. Every reachable completion path of a value-returning method must return a compatible value or otherwise not complete normally. A short expression-bodied method uses => expression; instead of a block. Keep one clear responsibility per helper.
using System;int total = CountItems(5, 2);PrintTotal(total);static int CountItems(int boxes, int perBox) => boxes * perBox;static void PrintTotal(int total){ Console.WriteLine($"Items: {total}");}The calculation is reusable independently of the display operation. PrintTotal returns no value.
Expected output (reasoned, not executed):
Items: 10using System;Console.WriteLine(BoxMath.CountItems(0, 7));static class BoxMath{ public static int CountItems(int boxes, int perBox) => boxes * perBox;}The static class is a named container for helpers; public allows this caller to use the method. Top-level statements appear before the type declaration. Classes are explained in chapter 9.
Expected output (reasoned, not executed):
0Common mistake and correction
Do not write int total = PrintTotal(10); when PrintTotal returns void. Printing is not returning. Have the calculation return the number, and call the display method separately. Also check the input range before using multiplication with unrestricted integers.
Try it yourself
Write a helper CountSeats(int rows, int seatsPerRow) that returns the total for inputs from 0 to 100. Test (4, 6), (0, 9), and (1, 1). Print the result outside the helper.
Next: Parameters and overloads. Examples target C# 14 / net10.0. They are static study material; compiler and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Methods and return values — knowledge check
Apply the lesson concepts to the following scenarios.
What is the return type in static int CountItems(int boxes, int perBox)?
What is an argument in CountItems(3, 4)?
What does a void method provide to the caller?
Which design lets a caller compare a computed total without reading console output?
What does CountItems(0, 7) return for the multiplication helper?