Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write a method with delegate parameter in C# / .NET 4.0?

Tags:

c#

.net

delegates

I've been using the way that I declare delegate in class level:

protected delegate void FieldsDelegate();

//and then write a method e.g.

protected int CreateComponent(DbConnection cnctn, string tableName, Dictionary<string, object> changedFieldValues, FieldsDelegate fieldsDelegate)

However this is really cumbersome and I can't see instantly what the delegate is like. So I would like to do it like this:

protected int CreateComponent(DbConnection cnctn, string tableName, Dictionary<string, object> changedFieldValues, delegate void fieldsDelegate())

so that I don't have the separate definition.

The above is not allowed for some reason. So how can I do it?

like image 765
char m Avatar asked Apr 27 '11 09:04

char m


2 Answers

.NET now provides the Action and Func generics for this purpose.

In your case, this delegate takes no parameters and returns nothing.

// protected delegate void FieldsDelegate(); // Don't need this anymore

protected int CreateComponent(
                               DbConnection cnctn, 
                               string tableName, 
                               Dictionary<string, object> changedFieldValues, 
                               Action fieldsDelegate
                            )

If it took a string as a parameter:

// protected delegate void FieldsDelegate(string s); 

protected int CreateComponent(
                               DbConnection cnctn, 
                               string tableName, 
                               Dictionary<string, object> changedFieldValues,
                               Action<string> fieldsDelegate
                             )

If it took a string as a parameter and returned a bool:

// protected delegate bool FieldsDelegate(string s); 

protected int CreateComponent(
                               DbConnection cnctn, 
                               string tableName, 
                               Dictionary<string, object> changedFieldValues,
                               Func<string, bool> fieldsDelegate
                             )
like image 157
Andrew Shepherd Avatar answered Sep 28 '22 19:09

Andrew Shepherd


You could use the generic Action<T> and Func<T> and their variants as delegates, and the bonus is you don't even need to define a separate delegate at all.

Action<T> takes up to 16 different type parameters, so: Action<T1, T2> and on up; Each type parameter is the type parameter for the method in the same position. So, Action<int, string> would work for this method:

public void MyMethod(int number, string info)

Func<T> is the same, except it is for methods that return a value. The last type argument is the return type. Func<T> is not what you would use in your case here.

Example: Func<string, int, object> would be for a method such as:

public object MyOtherMethod(string message, int number)

Using these generic delegates makes it clear what the arguments for that delegate argument would be, which seems to be your intent.

public void MyMethod(Action<string, MyClass>, string message)

Calling that method, you know you need to pass a method in that takes a string and a MyClass.

public void MeOtherMethod(Func<int, MyOtherClass>, int iterations)

Here, you know you need to pass a method that takes an int parameter, and returns MyOtherClass

like image 36
Andrew Barber Avatar answered Sep 28 '22 19:09

Andrew Barber