0
0
C Sharp (C#)programming~3 mins

Why Return values and void methods in C Sharp (C#)? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your methods could tell you exactly what they did, instead of leaving you guessing?

The Scenario

Imagine you are baking cookies and writing down each step on paper. After baking, you want to know how many cookies you made, but you only wrote the steps, not the result.

The Problem

Without a way to get results back, you must guess or check manually. This wastes time and can cause mistakes, especially if you repeat the process many times.

The Solution

Return values let methods give back results, like telling you exactly how many cookies you baked. Void methods do tasks without returning anything, like just baking without counting.

Before vs After
Before
void BakeCookies() { /* bake but no count returned */ }
int count = 0; // manual tracking outside
After
int BakeCookies() { /* bake and return count */ return 12; }
What It Enables

It lets your program communicate results clearly, making code easier to use and understand.

Real Life Example

A calculator method returns the sum of two numbers so you can use it elsewhere, while a void method might just print a message without returning anything.

Key Takeaways

Return values send results back from methods.

Void methods perform actions without returning data.

Using both properly makes programs clearer and more powerful.