With the release of C# 12, developers have even more powerful tools at their disposal to streamline their code the delegate default parameters. One of the significant enhancements is the support for default parameters in delegates.
In this blog post, we’ll explore the built-in delegates Action and Func, how to use them, and the new feature of default parameters in C# 12.
Understanding Delegates in C#
Delegates in C# are a type that represents references to methods with a particular parameter list and return type. They are used to pass methods as arguments to other methods.
Action and Func Delegates:
- Action: Represents a method that does not return a value. It can take 0 to 16 input parameters.
- Func: Represents a method that returns a value. It can take 0 to 16 input parameters and always returns one output parameter.
Example:
Action print = () => Console.WriteLine("Hello, World!");
Func<int, int> square = x => x * x;
print(); // Outputs: Hello, World!
Console.WriteLine(square(5)); // Outputs: 25
Lambda Expressions with Delegates
Both Action and Func delegates can be declared using lambda expressions. Here’s how you can declare and use them:
Lambda Expressions:
Action<int> printSquare = x => Console.WriteLine(x * x);
Func<int, int> addFive = x => x + 5;
printSquare(5); // Outputs: 25
Console.WriteLine(addFive(5)); // Outputs: 10
Delegate Default Parameters in C# 12
With C# 12, you can now use default parameters in delegates, similar to how you use them in methods. This feature can be incredibly useful for simplifying method calls and providing default behavior.
Using Default Parameters in Lambdas:
Func<int, int> addFive = (x = 4) => x + 5;
Console.WriteLine(addFive()); // Outputs: 9
Console.WriteLine(addFive(3)); // Outputs: 8
Practical Application: Declaring and Using Delegate Default Parameters
Let’s look at a practical example where we declare delegates with default parameters and use them in a class.
Class with Delegates:
public class Test
{
private delegate void PrintDelegate();
private delegate void SquareDelegate(int x = 5);
public void Print()
{
PrintDelegate printDel = () => Console.WriteLine("Test");
printDel();
}
public void Square(int x)
{
SquareDelegate squareDel = (y) => Console.WriteLine(y * y);
squareDel(x);
}
}
// Usage
var test = new Test();
test.Print(); // Outputs: Test
test.Square(5); // Outputs: 25
Using Default Values:
public class Test
{
private delegate void SquareDelegate(int x = 5);
public void Square()
{
SquareDelegate squareDel = (y) => Console.WriteLine(y * y);
squareDel();
}
}
// Usage
var test = new Test();
test.Square(); // Outputs: 25
Conclusion
The addition of default parameters to delegates in C# 12 is a powerful feature that can help simplify your code and make it more readable. By understanding and utilizing Action and Func delegates, along with lambda expressions and default parameters, you can write cleaner and more efficient C# code.
If you found this guide helpful, please head over to my YouTube channel, give it a thumbs up, and subscribe to our channel for more insights and tutorials. Thank you for reading!
Related Videos from my YouTube Channel: