What if you could write a piece of code once and use it everywhere without copying it again?
Why Method declaration and calling in C Sharp (C#)? - Purpose & Use Cases
Imagine you want to calculate the area of different rectangles in your program. Without methods, you would have to write the same calculation code again and again for each rectangle.
Writing the same code repeatedly is slow and boring. It's easy to make mistakes or forget to update all places if you want to change the calculation. This wastes time and causes bugs.
By declaring a method, you write the calculation once and give it a name. Then you just call that method whenever you need the area. This keeps your code clean, easy to read, and simple to fix.
int area1 = width1 * height1;
int area2 = width2 * height2;
// repeated code for each rectangleint CalculateArea(int width, int height) {
return width * height;
}
int area1 = CalculateArea(width1, height1);
int area2 = CalculateArea(width2, height2);Methods let you reuse code easily and organize your program like building blocks, making complex tasks simple.
Think of a coffee machine: you press a button (call a method) to make coffee instead of manually mixing ingredients every time.
Methods save time by avoiding repeated code.
They make programs easier to read and fix.
Calling methods is like using tools you create to solve problems efficiently.
Practice
Greet that returns nothing and takes no parameters in C#?Solution
Step 1: Understand method declaration syntax
A method declaration starts with an access modifier (like public), then the return type (void means no return), then the method name, and parentheses for parameters.Step 2: Check each option's syntax
public void Greet() { } correctly uses 'public void Greet() { }'. Options A, B, and D have syntax errors like wrong brackets or missing parentheses.Final Answer:
public void Greet() { } -> Option DQuick Check:
Method declaration = public void Greet() { } [OK]
- Using square brackets [] instead of parentheses ()
- Omitting parentheses after method name
- Placing return type after method name
CalculateSum that takes two integers as parameters?Solution
Step 1: Recall method call syntax
To call a method, use its name followed by parentheses containing arguments separated by commas.Step 2: Evaluate each option
CalculateSum(5, 10); uses parentheses correctly. Options B, C, and D use incorrect brackets or omit parentheses, causing syntax errors.Final Answer:
CalculateSum(5, 10); -> Option CQuick Check:
Method call = name + parentheses + arguments [OK]
- Using square brackets instead of parentheses
- Omitting parentheses when calling methods
- Using curly braces for method calls
public class Program {
public static int Double(int x) {
return x * 2;
}
public static void Main() {
int result = Double(4);
Console.WriteLine(result);
}
}Solution
Step 1: Understand the method Double
The method takes an integer x and returns x multiplied by 2.Step 2: Trace the method call in Main
Double(4) returns 4 * 2 = 8, which is stored in result and printed.Final Answer:
8 -> Option AQuick Check:
Double(4) = 8 [OK]
- Confusing return value with input
- Expecting 16 by squaring instead of doubling
- Thinking method does not return a value
public int AddNumbers(int a, int b) {
int sum = a + b;
}Solution
Step 1: Check method return type and body
The method declares it returns an int but does not return any value.Step 2: Fix by adding a return statement
Addingreturn sum;returns the calculated value, matching the return type.Final Answer:
Add a return statement:return sum;-> Option AQuick Check:
Return value needed for non-void methods [OK]
- Forgetting to return a value in non-void methods
- Changing return type incorrectly
- Removing parameters unnecessarily
IsEven that returns true if a number is even, otherwise false. Which of the following is the correct method declaration and call to check if 7 is even?Solution
Step 1: Check method return type and logic
Method should return bool and use modulo (%) to check evenness. public bool IsEven(int num) { return num % 2 == 0; } ... bool result = IsEven(7); correctly returns true if remainder is 0.Step 2: Verify method call and variable type
public bool IsEven(int num) { return num % 2 == 0; } ... bool result = IsEven(7); calls IsEven(7) and stores result in a bool variable, matching the method return type.Final Answer:
public bool IsEven(int num) { return num % 2 == 0; } ... bool result = IsEven(7); -> Option BQuick Check:
Use modulo and bool return for even check [OK]
- Using division instead of modulo for even check
- Returning int instead of bool
- Declaring method void but trying to return a value
Ready to prove it?
Pass the test and this pattern moves unknown
