1

I am working on windows form and using Thread safe concept to set the value of Main thread controls,here I have two delegates one is used with no argument and other is used to pass the argument but I found impossible to do this because it says

Form already contain definition for StringArgReturningVoidDelegate

Delegate declaration

    delegate string StringArgReturningVoidDelegate();

    delegate void StringArgReturningVoidDelegate(string text);
TAHA SULTAN TEMURI
  • 4,031
  • 2
  • 40
  • 66
  • 1
    AFAIR using `delegate` keyword is a (c# ?) compiler feature, when used it will in fact create a normal C# type: `class StringArgReturningVoidDelegate`, then this class is used as type by events and actual variables holding references to methods. This is why you can't have same name. Quickfix: use another name. – Sinatr May 15 '17 at 09:58
  • How can I rename a delegate when I am using Class as delegate name? – TAHA SULTAN TEMURI May 15 '17 at 10:01
  • 1
    You don't have to use same as containing type name. In fact, they don't have to belong to class, you can [define delegate in the namespace](http://stackoverflow.com/q/2343449/1997232) directly. [Naming](http://stackoverflow.com/q/2346065/1997232) delegates is actually a subject. Currently there are `Action<>` and `Func<>` generics to define methods pointers and `EventHandler<>` for events. I am not even sure if there is a need to continue using `delegate` keyword.. In other words, don't define delegate types, use `Func` for first and `Action` for second. – Sinatr May 15 '17 at 10:05

2 Answers2

3

The delegates must me named uniquely within their enclosing type/namespace. I put it to you that the first one should possibly be NoArgsReturningStringDelegate, but: just using Func<string> and Action<string> rather than declaring your own delegate types would be easier...

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

No.You cannot overload a delegate.

Some pointers as to why : C# - How can I "overload" a delegate?

Some workarounds for this, you can find in this answer : https://stackoverflow.com/a/41574140/4054186

Community
  • 1
  • 1
Abdul Rehman Sayed
  • 6,532
  • 7
  • 45
  • 74