Curriculum
Methods and Functions in C# are reusable blocks of code designed to perform specific tasks within an application. Methods and Functions in C# help developers write organized, maintainable, scalable, and efficient code. Every .NET developer uses Methods and Functions in C# extensively while developing Console Applications, ASP.NET Core Applications, MVC Projects, Web APIs, Desktop Applications, and Enterprise Software Solutions.
Understanding Methods and Functions in C# is essential because modern software applications contain thousands of lines of code, and reusable methods help reduce duplication while improving maintainability.
Methods and Functions in C# are named blocks of code that perform a specific operation when called.
Instead of writing the same code repeatedly, developers can create a method once and reuse it throughout the application.
Example:
void DisplayMessage()
{
Console.WriteLine("Welcome to C# Programming");
}
Method Call:
DisplayMessage();
Output:
Welcome to C# Programming
This approach improves code reusability and readability.
Methods and Functions in C# help developers:
Large enterprise applications heavily depend on methods to manage complexity.
A typical C# method consists of:
Example:
public int Add(int num1, int num2)
{
return num1 + num2;
}
Components:
Access Modifiers control method accessibility.
Common Access Modifiers:
| Access Modifier | Description |
|---|---|
| public | Accessible everywhere |
| private | Accessible within the same class |
| protected | Accessible in derived classes |
| internal | Accessible within the same project |
Example:
public void Display()
{
}
The Return Type specifies what a method returns.
Example:
int GetAge()
{
return 25;
}
Output:
25
If a method does not return anything, use:
void
Example:
void ShowMessage()
{
Console.WriteLine("Hello");
}
Methods can execute tasks without receiving input values.
Example:
void Welcome()
{
Console.WriteLine("Welcome Student");
}
Method Call:
Welcome();
Output:
Welcome Student
These methods are useful for displaying messages and performing fixed operations.
Parameters allow methods to accept input values.
Example:
void DisplayName(string name)
{
Console.WriteLine(name);
}
Method Call:
DisplayName("Rahul");
Output:
Rahul
Parameters make methods flexible and reusable.
Example:
void StudentInfo(string name, int age)
{
Console.WriteLine(name);
Console.WriteLine(age);
}
Method Call:
StudentInfo("Rahul", 20);
Output:
Rahul
20
Multiple parameters allow methods to process more information.
Methods can return processed results.
Example:
int Add(int num1, int num2)
{
return num1 + num2;
}
Method Call:
int result = Add(10, 20);
Console.WriteLine(result);
Output:
30
Return values are widely used in business logic.
Variables defined in a method declaration.
Example:
void Display(string name)
Here:
name
is a parameter.
Actual values passed during a method call.
Example:
Display("Rahul");
Here:
"Rahul"
is an argument.
Understanding this distinction is important for professional development.
Method Overloading allows multiple methods to have the same name but different parameter lists.
Example:
int Add(int a, int b)
{
return a + b;
}
int Add(int a, int b, int c)
{
return a + b + c;
}
Method Calls:
Add(10, 20);
Add(10, 20, 30);
Output:
30
60
Method Overloading improves flexibility and code readability.
A Recursive Method calls itself until a termination condition is reached.
Example:
int Factorial(int number)
{
if(number == 1)
return 1;
return number * Factorial(number - 1);
}
Method Call:
Console.WriteLine(Factorial(5));
Output:
120
Recursion is commonly used in algorithms and data structures.
Static Methods belong to the class rather than objects.
Example:
class Program
{
static void Display()
{
Console.WriteLine("Hello");
}
}
Method Call:
Program.Display();
Output:
Hello
Static Methods are widely used in utility classes.
Variables declared inside a method are called local variables.
Example:
void Display()
{
int age = 20;
}
The variable is accessible only within that method.
Methods handle:
Deposit();
Withdraw();
Transfer();
Methods handle:
AddToCart();
PlaceOrder();
ProcessPayment();
Methods handle:
AddStudent();
UpdateStudent();
DeleteStudent();
Methods handle:
RegisterPatient();
GenerateBill();
BookAppointment();
Methods and Functions in C# are essential for modular software development.
Avoid methods containing hundreds of lines of code.
Incorrect:
DoWork();
Better:
CalculateSalary();
Reuse methods rather than repeating code.
Always utilize returned results when needed.
Methods and Functions in C# are heavily used in:
A strong understanding of Methods and Functions in C# helps developers build maintainable and scalable software solutions.
Methods and Functions in C# are reusable blocks of code that perform specific tasks.
Methods improve code organization, maintainability, and reusability.
Parameters are defined in method declarations, while arguments are actual values passed during method calls.
Method Overloading allows multiple methods with the same name but different parameter lists.
A Recursive Method calls itself repeatedly until a stopping condition is met.
They are used throughout .NET applications for business logic, data processing, APIs, and software architecture.
WhatsApp us