Complete C# Tutorial

Methods in C#: Simple Guide with Easy Example

🎉 Introduction:

Hey there, coding champ! Ever felt like you’re writing the same code over and over again? 😅 Don’t worry—you’re not alone! Imagine you’re at a coffee shop. Every time someone orders coffee, you manually grind beans, boil water, and pour the coffee. Sounds exhausting, right? What if you had a button to make coffee instantly? ☕

That’s what Methods in C# do! They’re like that magical coffee button—reusable chunks of code that save you time and effort. Intrigued? Let’s dive in! 🚀

📚 What are Methods in C#?

In simple words, a method is a block of code that performs a specific task. Instead of writing the same code repeatedly, you create a method and call it whenever needed. Pretty cool, right?

🔑 Syntax:

				
					<access_modifier> <return_type> <method_name>(parameters)
{
    // Code to execute
}
				
			

Example:

				
					public void SayHello()
{
    Console.WriteLine("Hello, friend! 👋");
}
				
			

👉 Here, public is the access modifier, void means it returns nothing, and SayHello is the method name.

1️⃣ Simple Method Example:

🎯 Scenario:

Think of a calculator that adds two numbers. Instead of writing the addition code every time, create a method!

				
					using System;

class Calculator
{
    public int AddNumbers(int a, int b)
    {
        return a + b;
    }

    static void Main()
    {
        Calculator calc = new Calculator();
        int sum = calc.AddNumbers(5, 7);  
        Console.WriteLine("Sum: " + sum);  // Output: Sum: 12
    }
}
				
			

💡 Explanation:

  • AddNumbers takes two integers and returns their sum.
  • We called the method inside Main without rewriting the addition logic.

Output:

				
					Sum: 12
				
			

Feels like magic, doesn’t it? 😍

2️⃣ Method with Return Value Example:

🚗 Real-World Scenario:

Imagine you’re booking a cab. The app calculates the fare based on distance. Let’s code that!

				
					using System;

class CabService
{
    public double CalculateFare(double distance)
    {
        double ratePerKm = 10.0;
        return distance * ratePerKm;
    }

    static void Main()
    {
        CabService cab = new CabService();
        double fare = cab.CalculateFare(8.5);  
        Console.WriteLine("Fare: ₹" + fare);  // Output: Fare: ₹85
    }
}
				
			

💡 Explanation:

  • CalculateFare multiplies distance with rate per km.
  • You just enter the distance—boom! Fare calculated. 🎉

Output:

				
					Fare: ₹85
				
			

3️⃣ Method with Parameters Example:

🚗 Real-World Scenario:

Ordering pizza? 🥳 You select size and toppings. Let’s build that using methods!

				
					using System;

class PizzaOrder
{
    public void PlaceOrder(string size, string topping)
    {
        Console.WriteLine($"Order placed: {size} pizza with {topping} 🍕");
    }

    static void Main()
    {
        PizzaOrder order = new PizzaOrder();
        order.PlaceOrder("Large", "Pepperoni");  
        order.PlaceOrder("Medium", "Mushrooms");  
    }
}
				
			

💡 Explanation:

  • The PlaceOrder method takes two parameters—size and topping.
  • No need to repeat code for each order. Just call the method with new inputs!

Output:

				
					Order placed: Large pizza with Pepperoni 🍕  
Order placed: Medium pizza with Mushrooms 🍕  
				
			

4️⃣ Method Overloading Example:

🚗 Real-World Scenario:

A calculator that can handle both integers and doubles. Handy, right?

				
					using System;

class MathOperations
{
    public int Multiply(int a, int b) => a * b;
    public double Multiply(double a, double b) => a * b;

    static void Main()
    {
        MathOperations math = new MathOperations();
        Console.WriteLine("Int Multiply: " + math.Multiply(3, 4));  // 12
        Console.WriteLine("Double Multiply: " + math.Multiply(2.5, 4.2));  // 10.5
    }
}
				
			

💡 Explanation:

  • Same method name Multiply, different parameters—this is method overloading.
  • It makes code clean and easy to read!

Output:

				
					Int Multiply: 12  
Double Multiply: 10.5  				
			

🎯 Conclusion:

Yay! 🎉 You’ve just unlocked the power of Methods in C#! They make your code clean, reusable, and super-efficient—just like your favorite coffee machine ☕. Remember, methods are your coding BFFs. Feeling pumped? I bet you are! 🚀

⏭️ Next what?

Guess what? Next up is Best Practices for Writing Clean Classes and Methods. You’ll learn how to make your code sparkle ✨ and avoid common pitfalls. It’s gonna be fun and helpful! Don’t miss it! 😉

Leave a Comment

Share this Doc

Methods in C#

Or copy link