7️⃣ C# Methods & Functions
Estimated reading: 3 minutes 26 views

🧠 C# Methods – Mastering the Basics of Reusable Code


🧲 Introduction – Why Learn Methods in C#?

Methods are the building blocks of any C# application. They allow you to organize logic, reuse code, and maintain cleaner structure in programs. Whether you’re building a console app, a web API, or a Unity game, understanding how to define and call methods is essential.

🎯 In this guide, you’ll learn:

  • What methods are and how they work
  • How to declare and call a method
  • Return types and void methods
  • Scope and structure of methods
  • Best practices with examples

πŸ” Core Concept – What is a Method in C#?

A method is a named block of code designed to perform a specific task. It can be invoked whenever needed and may return a value or perform an action.


πŸ”£ Method Syntax:

return_type MethodName(parameters)
{
    // Code block
    return value; // if not void
}

πŸ“˜ Example – Method Without Return (void):

void SayHello()
{
    Console.WriteLine("Hello from method!");
}

πŸ“˜ Example – Method With Return:

int Add(int a, int b)
{
    return a + b;
}

πŸ’» Code Example – Method Calling

void GreetUser(string name)
{
    Console.WriteLine($"Welcome, {name}!");
}

GreetUser("Alice");

πŸ“₯ Output:

Welcome, Alice!

🧡 Explanation:

  • GreetUser is defined with a parameter name.
  • The method is called and passed "Alice" as an argument.

🧠 Method with Return Value

double Square(double num)
{
    return num * num;
}

double result = Square(4.5);
Console.WriteLine($"Result: {result}");

πŸ“₯ Output:

Result: 20.25

πŸ“˜ Use Case: Encapsulate logic for reuse and testing.


πŸ” Methods with Multiple Parameters

void DisplayInfo(string name, int age)
{
    Console.WriteLine($"Name: {name}, Age: {age}");
}

DisplayInfo("Bob", 30);

πŸ“˜ Use Case: Pass dynamic values into a method for processing.


πŸ”„ Void vs Non-Void Methods

Featurevoid MethodMethod with Return
PurposePerforms an actionCalculates and returns a value
Return KeywordNot requiredRequired (return value;)
Use CasePrinting, logging, UI updatesMath calculations, data processing

πŸ’‘ Best Practices & Tips

πŸ’‘ Tip: Keep methods short and focused on a single task.

⚠️ Pitfall: Avoid duplicating logic; use methods to encapsulate repeatable code.

πŸ“˜ Best Practice: Use meaningful method names that clearly describe the action (CalculateTax(), PrintInvoice()).


πŸ› οΈ Real-World Use Cases

  • πŸ“‹ Input validation functions
  • πŸ”’ Mathematical operations
  • 🧾 Invoice generation
  • πŸ§ͺ Reusable utility functions (e.g., formatting, logging)
  • 🧠 Business logic layers in APIs

πŸ“Œ Summary – Recap & Next Steps

🧡 Key Takeaways:

  • Methods improve code organization, reuse, and testing.
  • C# methods can return values (int, string, etc.) or be void.
  • Parameterized methods allow dynamic input during execution.

βš™οΈ Real-world relevance: Critical for modular programming across desktop, web, and mobile applications in C#.


❓ FAQ Section

❓ What is the difference between a function and a method in C#?
βœ… In C#, the term method is used (not function), and it’s a member of a class.


❓ Can I declare a method inside another method in C#?
βœ… No. Methods must be declared at the class levelβ€”not inside other methods.


❓ How many parameters can a method accept?
βœ… As many as needed, but readability is important. Consider using objects for too many parameters.


❓ Can I overload methods in C#?
βœ… Yes! You can define multiple methods with the same name but different parameters (see: Method Overloading).


❓ Are all methods public in C#?
βœ… No. By default, methods are private in classes unless explicitly marked public, protected, etc.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

🧠 C# Methods / C# Method Basics

Or Copy Link

CONTENTS
Scroll to Top