Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I can`t declare a Delegate at function level?

Tags:

c#

delegates

Sorry if this question is too simple or easy.

I just started studying Delegates using C#. When I tried to declare one inside a function I got design time errors, but when I declare the same Delegate at class level, it works fine. Why?

If matters this is the code: delegate void Test();

like image 334
RHaguiuda Avatar asked Feb 26 '23 20:02

RHaguiuda


1 Answers

AFAIK, declaring a delegate like that (in C#, which I assume is the language you are using) is declaring a new type. Just as you cannot declare classes, structs, or interfaces in a method, you cannot declare this type.

Edit: If you're just learning delegates, and the language is indeed c#, consider using the Func templated delegate! It will save you from having declarations everywhere.

http://msdn.microsoft.com/en-us/library/bb549151.aspx

like image 67
Stefan Valianu Avatar answered Mar 07 '23 12:03

Stefan Valianu