Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# delegate for two methods with different parameters

I am using the following methods:

public void M1(Int32 a)
{
  // acquire MyMutex
  DoSomething(a);
  // release MyMutex
}

and

public void M2(String s, String t)
{
  // acquire MyMutex
  DoSomethingElse(s, t);
  // release MyMutex
}

From what I have found so far it seems that it is not possible to use a single delegate for two methods with different signatures.

Are there any other alternatives to write something like this:

public void UsingMutex(...)
{
  // acquire MyMutex
  ...
  // release MyMutex
}

UsingMutex(M1);
UsingMutex(M2);

All I can think for the moment is to use two delegates and a boolean flag to know which delegate to call, but it is not a long term solution.

It is possible to combine generics with delegates? And if so, do you have some links for any kind of documentation?

Environment: C# 2.0

like image 894
alexandrul Avatar asked Dec 18 '08 08:12

alexandrul


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C and C++ same?

While C and C++ may sound similar, their features and usage differ. C is a procedural programming language that support objects and classes. On the other hand C++ is an enhanced version of C programming with object-oriented programming support.


3 Answers

Absolutely you can mix delegates with generics. In 2.0, Predicate<T> etc are good examples of this, but you must have the same number of args. In this scenario, perhaps an option is to use captures to include the args in the delegate?

i.e.

    public delegate void Action();
    static void Main()
    {
        DoStuff(delegate {Foo(5);});
        DoStuff(delegate {Bar("abc","def");});
    }
    static void DoStuff(Action action)
    {
        action();
    }
    static void Foo(int i)
    {
        Console.WriteLine(i);
    }
    static void Bar(string s, string t)
    {
        Console.WriteLine(s+t);
    }

Note that Action is defined for you in .NET 3.5, but you can re-declare it for 2.0 purposes ;-p

Note that the anonymous method (delegate {...}) can also be parameterised:

    static void Main()
    {
        DoStuff(delegate (string s) {Foo(5);});
        DoStuff(delegate (string s) {Bar(s,"def");});
    }
    static void DoStuff(Action<string> action)
    {
        action("abc");
    }
    static void Foo(int i)
    {
        Console.WriteLine(i);
    }
    static void Bar(string s, string t)
    {
        Console.WriteLine(s+t);
    }

Finally, C# 3.0 makes this all a lot easier and prettier with "lambdas", but that is another topic ;-p

like image 76
Marc Gravell Avatar answered Sep 22 '22 00:09

Marc Gravell


Yes, it's possible to combine generics with delegates.

public delegate void Action<T>(T x);
public delegate void Action<T,U>(T x, U y);

public void UsingMutex<T>(Action<T> x, T t) {
    // acquire mutex...
    x(t);
    // release mutex...
}
public void UsingMutex<T,U>(Action<T,U> x, T t, U u) {
    // acquire mutex...
    x(t, u);
    // release mutex...
}

But you still have to handle different number of parameters using overloads.

like image 4
mmx Avatar answered Sep 20 '22 00:09

mmx


If you look at the Func<T> and Action<T> delegates in the framework, you'll see that they define a number of similar delegates with different number of parameters. You can use generics, but that doesn't solve the number of arguments issue you're talking about.

like image 3
Brian Rasmussen Avatar answered Sep 19 '22 00:09

Brian Rasmussen