Explain it simply
Methods, in plain language
Method is a reusable block of code. As a program grows, cramming everything into Main turns it into spaghetti fast. Breaking things into methods keeps your code easier to read, test, and change.
Problem → Why → Solution
The problem — why this concept matters
Combining input, UI, business logic, and storage in one method makes testing and reuse difficult.
A common mistake
Assuming one successful happy path proves the code is correct, maintainable, and production-ready.
Core concept — understand how it works
A method contract includes its name, parameters, return type, preconditions, side effects, and failure behavior. Prefer single responsibility, clear parameters, return values, and pure logic; use `ref`, `out`, and optional parameters only when justified.
- typed input
- C# expression and method
- CLR value or object state
Explain the core concept and runtime behavior of Methods
Core syntax or pattern
class Program
{
static void SayHello(string name)
{
Console.WriteLine($"Hello, {name}!");
}
static int Add(int x, int y)
{
return x + y;
}
static void Main(string[] args)
{
SayHello("Aung");
int total = Add(5, 3);
Console.WriteLine($"Total: {total}");
}
}A method contract includes its name, parameters, return type, preconditions, side effects, and failure behavior. Prefer single responsibility, clear parameters, return values, and pure logic; use `ref`, `out`, and optional parameters only when justified.
Read the code step by step
- typed input
class ProgramMethod is a reusable block of code. As a program grows, cramming everything into Main turns it into spaghetti fast. Breaking things into methods keeps your code easier to read, test, and change.
- C# expression and method
return x + y;A method contract includes its name, parameters, return type, preconditions, side effects, and failure behavior. Prefer single responsibility, clear parameters, return values, and pure logic; use `ref`, `out`, and optional parameters only when justified.
- CLR value or object state
}Write a method that validates price and quantity and returns a total, then test normal, zero, negative, and near-overflow inputs.
- typed input
- C# expression and method
- CLR value or object state
Success
Explain the core concept and runtime behavior of Methods
Failure
Combining input, UI, business logic, and storage in one method makes testing and reuse difficult.
Method is a reusable block of code. As a program grows, cramming everything into Main turns it into spaghetti fast. Breaking things into methods keeps your code easier to read, test, and change.
class Program
{
static void SayHello(string name)
{
Console.WriteLine($"Hello, {name}!");
}
static int Add(int x, int y)
{
return x + y;
}
static void Main(string[] args)
{
SayHello("Aung");
int total = Add(5, 3);
Console.WriteLine($"Total: {total}");
}
}What this code does
SayHellomethod takes a name as a parameter and prints out a greeting.Addmethod takes two numbers andreturns their sum.voidmeans the method doesn't return a value.intreturn type has to hand back anintvalue.
Hello, Aung! Total: 8Real-world usage
How to apply it in a real project
Write a method that validates price and quantity and returns a total, then test normal, zero, negative, and near-overflow inputs.
Mini challenge
Build it yourself
Write a method that validates price and quantity and returns a total, then test normal, zero, negative, and near-overflow inputs.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
class Program
{
static void SayHello(string name)
{
Console.WriteLine($"Hello, {name}!");
}
static int Add(int x, int y)
{
return x + y;
}
static void Main(string[] args)
{
SayHello("Aung");
int total = Add(5, 3);
Console.WriteLine($"Total: {total}");
}
}Expected: Explain the core concept and runtime behavior of Methods
Show hint
Write a method that validates price and quantity and returns a total, then test normal, zero, negative, and near-overflow inputs.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- typed input → C# expression and method → CLR value or object state
- A method contract includes its name, parameters, return type, preconditions, side effects, and failure behavior. Prefer single responsibility, clear parameters, return values, and pure logic; use `ref`, `out`, and optional parameters only when justified.
- Combining input, UI, business logic, and storage in one method makes testing and reuse difficult.
- Write a method that validates price and quantity and returns a total, then test normal, zero, negative, and near-overflow inputs.
C# documentation — Microsoft Learn