Sitemap

Return type Delegate and Func.

3 min readOct 8, 2024

Objective: Return type Delegate and Func.

Return type delegate is the type of delegate which return data type, I will define a delegate signature and create a variable.

Note: If you are new to C# delegate please go check my article about delegate.

Press enter or click to view image in full size

Make sure the function follow the signature, I have create a return method which return int, the return variable is global variable I create to add some fake logic.

Press enter or click to view image in full size

We call it as we call normal delegate, nothing change.

Press enter or click to view image in full size

What is a Func?

This is a built-in return type delegate, which can take parameter and return type for method signature in T bracket.

To create a func delegate we use keyword func after access modifier, followed by T bracket and name of delegate.

Press enter or click to view image in full size

Inside the T bracket a first parameter is will be a return type if its only one.

Press enter or click to view image in full size

But If you want func delegate to take a parameter and return other data type.

A first parameter in T bracket will be a parameter(T) separate by comma and second parameter will be a result(Tresult) aka a return data type of method signature.

Note: We are not limited to only two parameter, we can add more than two parameter just check the tool tip for more option on how many parameter you can add.

Press enter or click to view image in full size
Press enter or click to view image in full size

This func delegate don't need extra variable, we call it using the same delegate variable.

Press enter or click to view image in full size
Those number i put in func delegate is a parameter, plus Ignore the number i add on return method I put it just to get rid of error. I assume at this level you are not beginner any more about return method.

See you into the next one.

--

--

Suleiman Abdullah
Suleiman Abdullah

Written by Suleiman Abdullah

My name is Suleiman Abdullah. I learn game development and share what I learn by writing articles. :https://ko-fi.com/suleimanabdullah

No responses yet