Mastering C# Functions: Top Examples & Best Practices

C# functions, also known as methods, are fundamental to object-oriented programming in C#. They encapsulate a set of instructions to perform a specific task, enhancing code reusability and maintainability. In this article, we'll explore various aspects of C# functions, including their syntax, types, and examples.

Understanding C# Functions

In C#, a function is declared using the void or a return type followed by the function name, parameters enclosed in parentheses, and the function body enclosed in curly braces. Here's a basic syntax:

```csharp returnType FunctionName(parameters) { // Function body } ```

Types of C# Functions

C# supports several types of functions, each serving a different purpose:

Constant Function Explained | Easy Horizontal Line Algebra Guide
Constant Function Explained | Easy Horizontal Line Algebra Guide

  • Void Functions: These functions do not return any value. They are declared using the void keyword.
  • Value-Returning Functions: These functions return a value of a specific data type. The return type is specified before the function name.
  • Overloaded Functions: Functions with the same name but different parameters. C# allows multiple functions with the same name but different parameters in the same scope.
  • Recursive Functions: Functions that call themselves during their execution.

Void Functions Example

```csharp public void SayHello(string name) { Console.WriteLine($"Hello, {name}!"); } ```

Value-Returning Functions Example

```csharp public int AddNumbers(int a, int b) { return a + b; } ```

Overloaded Functions Example

```csharp public int AddNumbers(int a, int b) { return a + b; } public double AddNumbers(double a, double b) { return a + b; } ```

Recursive Functions Example

```csharp public int Factorial(int number) { if (number == 0 || number == 1) return 1; else return number * Factorial(number - 1); } ```

Function Parameters

Functions can accept input parameters to perform operations. Parameters are declared inside the parentheses following the function name. Here's how you can declare parameters:

```csharp returnType FunctionName(parameterType1 parameterName1, parameterType2 parameterName2, ...) { // Function body } ```

Lambdas and Anonymous Methods

C# also supports lambda expressions and anonymous methods, which are compact ways to define functions. Lambdas are used to define delegates and anonymous methods are used to define methods without a name. Here's an example of each:

```csharp // Lambda expression Func square = x => x * x; // Anonymous method Action action = delegate { Console.WriteLine("Hello, World!"); }; ```

Extension Methods

Extension methods allow you to add new methods to existing types without creating a new derived type. They are defined using the this keyword. Here's an example:

C++ Basics
C++ Basics

```csharp public static class StringExtensions { public static bool IsNullOrWhiteSpace(this string str) { return string.IsNullOrWhiteSpace(str); } } ```

With this extension method, you can now call IsNullOrWhiteSpace on a string like this:

```csharp string str = " "; bool result = str.IsNullOrWhiteSpace(); ```

C++ STL Cheat Sheet | Containers, Algorithms & Time Complexities
C++ STL Cheat Sheet | Containers, Algorithms & Time Complexities
13 C# versions that have changed the world: C# 1.0 (2002) - Core features, such as classes, structs, interfaces, delegates, arrays and collections C# 2.0 (2005) - Generics, anonymous methods… | Kristijan Kralj | 40 comments
13 C# versions that have changed the world: C# 1.0 (2002) - Core features, such as classes, structs, interfaces, delegates, arrays and collections C# 2.0 (2005) - Generics, anonymous methods… | Kristijan Kralj | 40 comments
C++: The Comprehensive Guide
C++: The Comprehensive Guide
a piece of paper that has some writing on it with different types of notes in it
a piece of paper that has some writing on it with different types of notes in it
c# functions examples
c# functions examples
css cheat sheet
css cheat sheet
Complete C++ Roadmap for Beginners 🚀
Complete C++ Roadmap for Beginners 🚀
The 5G Core Ecosystem: 12 Network Functions (NFs) Explained
The 5G Core Ecosystem: 12 Network Functions (NFs) Explained
a poster showing the different types of computer components and their functions in this diagram, you can
a poster showing the different types of computer components and their functions in this diagram, you can
SQL HAVING CLAUSE explained for beginners
SQL HAVING CLAUSE explained for beginners
the excel sheet is displayed on an iphone screen, and it appears to be filled with information
the excel sheet is displayed on an iphone screen, and it appears to be filled with information
how to use claude opts 4 3
how to use claude opts 4 3
LeetCode 1160 Explained Simply 🌟 | Character Frequency Pattern for Beginners
LeetCode 1160 Explained Simply 🌟 | Character Frequency Pattern for Beginners
Pointers in C Programming Explained for Beginners
Pointers in C Programming Explained for Beginners
SQL CTE Explained with Examples | Common Table Expression Tutorial
SQL CTE Explained with Examples | Common Table Expression Tutorial
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
Vertex X-Coordinate Formula Explained | Easy Quadratic Parabola Guide
Vertex X-Coordinate Formula Explained | Easy Quadratic Parabola Guide
an old building with many balconies on it
an old building with many balconies on it
ABLLS-R C23 G12 Picture Cards: Object Parts Learning Resource - Etsy
ABLLS-R C23 G12 Picture Cards: Object Parts Learning Resource - Etsy
Cosine Formula Explained | Easy Trigonometry Guide for Students
Cosine Formula Explained | Easy Trigonometry Guide for Students
What is C++ ? An Introduction to programming with C++
What is C++ ? An Introduction to programming with C++
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes