Back
Close

C# LINQ Background Topics

player_one
14K views
Previous: Welcome Next: Delegate Variables

Delegates - Delegate declarations

Why learn about delegates?

When learning LINQ, it is important to have a good understanding of delegates in C#. Many of the more powerful capabilities of LINQ make use of delegates.

What is a delegate?

A delegate is simply a reference to a method. Delegates can be stored and passed around in a variable, and hence they must have a type definition:

private delegate int FuncTwoInts(int one, int two);

The line above defines the type FuncTwoInts. The FuncTwoInts type is a reference to a method that takes two int parameters and returns a single int result.

Exercise

Declare a new type SayHello as a delegate that takes a single string parameter and returns a string result.

Delegates Exercise
namespace Delegates1
{
// Uncomment the SayHello delegate declaration
//
// Following the example in the lesson, declare the
// type SayHello as a delegate that takes a single
// string parameter and returns a string result.
// public ... SayHello ...
public static class DelegatesExercise1
{
public static void CallSayHelloDelegate(SayHello func)
{
Exercise.PrintAnswer(func("World"));
Exercise.PrintAnswer(func("my baby"));
Exercise.PrintAnswer(func("my honey"));
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants