Methods (Functions) & Method Parameters in C#
Learn how to create reusable methods, pass parameters, return values, and write clean C# code with practical examples.
Table of Contents
- What is a Method?
- Why Use Methods?
- Method Syntax
- Method Parameters
- Return Values
- Optional Parameters
- Named Arguments
- ref, out and params
- Best Practices
- Summary
What is a Method?
A method (also called a function) is a reusable block of code that performs a specific task. Instead of writing the same code multiple times, you can write it once inside a method and call it whenever needed.
Why Use Methods?
- Reduce duplicate code.
- Improve code readability.
- Make applications easier to maintain.
- Break large problems into smaller tasks.
Method Syntax
accessModifier returnType MethodName(parameters)
{
// Code
}
Creating and Calling a Method
using System;
class Program
{
static void Welcome()
{
Console.WriteLine("Welcome to C# Tutorials");
}
static void Main()
{
Welcome();
}
}
Method Parameters
Parameters allow you to pass data into a method.
static void Greet(string name)
{
Console.WriteLine("Hello " + name);
}
Greet("John");
Greet("David");
Methods with Return Values
static int Add(int a, int b)
{
return a + b;
}
int result = Add(10,20);
Console.WriteLine(result);
return keyword whenever your method needs to send a value back to the caller.
Optional Parameters
static void Welcome(string name = "Guest")
{
Console.WriteLine("Welcome " + name);
}
Welcome();
Welcome("John");
Named Arguments
static void Student(string name,int age)
{
Console.WriteLine(name + " " + age);
}
Student(age:25,name:"John");
Using ref Parameter
static void Increase(ref int number)
{
number++;
}
Using out Parameter
static void GetValues(out int x)
{
x = 100;
}
Using params
static int Sum(params int[] numbers)
{
int total = 0;
foreach(int n in numbers)
total += n;
return total;
}
Console.WriteLine(Sum(10,20,30));
Complete Example
using System;
class Program
{
static int Multiply(int a,int b)
{
return a*b;
}
static void Main()
{
int result = Multiply(5,6);
Console.WriteLine(result);
}
}
Best Practices
- Keep methods small and focused.
- Use meaningful method names.
- Avoid methods that perform too many tasks.
- Return values instead of using global variables.
- Use parameters to make methods reusable.
Summary
| Concept | Description |
|---|---|
| Method | Reusable block of code. |
| Parameter | Input passed into a method. |
| Return | Returns a value from a method. |
| void | No value is returned. |
| params | Accepts multiple arguments. |
| ref | Passes value by reference. |
| out | Returns values through parameters. |
Interview Questions
- What is a method in C#?
- What is the difference between parameters and arguments?
- What is the difference between void and return?
- When should you use ref and out?
- What is the purpose of params?
Practice Exercise
- Create a method to print your name.
- Create a method that adds two numbers.
- Create a method that returns the largest of two numbers.
- Create a method using optional parameters.
- Create a method using params.
FAQ
Can a method return multiple values?
Yes. You can use out, tuples, or custom objects.
What is the difference between a parameter and an argument?
A parameter is defined in the method, while an argument is the value passed when calling the method.
Next Tutorial: Method Overloading in C#
No comments:
Post a Comment