7️⃣ C# Methods & Functions
Estimated reading: 3 minutes 288 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 :
Share

🧠 C# Methods / C# Method Basics

Or Copy Link

CONTENTS
Scroll to Top