Chris Halcrow
Chris Halcrow

Reputation: 31950

What does these generic parameter constraints mean?

I'm using a method with this as the 1st parameter:

Action<IFieldsetter<Contact>>

How do I read this? Does is mean that this must be an Action object where the Action constructor is constrained to accept only something that implements IFieldsetter? And it looks like IFieldsetter itself is being constrained, but I don't understand this part at all.

Upvotes: 0

Views: 108

Answers (4)

Andrew
Andrew

Reputation: 7880

It's not an actual constraint, but just the type it needs. So, that method's first parameter is an Action (i.e. delegate), which has only one parameter, and that parameter is a IFieldsetter<Contact>, whatever that means. My guess is that IFieldsetter exposes a setter and in this case it must handle the type Contact, but you should be the one who knows for real what they are and do! Post the code of this interface and this class if you need further help.

For example, if it was Action<IEnumerable<String>>, it would mean that the delegate receives a list of strings as a parameter. Here's some code:

// Your method with the *special* parameter.
private void ChrisMethod(Action<IEnumerable<String>> method)
{
    string[] exampleList = { "First", "Second", "Third" };
    method(exampleList);
}

// The method that can be used as parameter.    
private void ParameterMethod(IEnumerable<String> list)
{
    foreach(string str in list)
        Console.WriteLine(str);
}

public void Main()
{
    ChrisMethod(ParameterMethod);
}

A constraint on a type parameter is a different thing. You can learn more about that here.

Upvotes: 4

Ramireddy Ambati
Ramireddy Ambati

Reputation: 199

Action<IFieldsetter<Contact>> means Action delegate accepts a parameter of type that implements a generic interface IFieldsetter. suppose a class is implemented with IFieldsetter intercae with Contact as the generic parameter as shown below.

public class Test: IFieldsetter<Conatct>
{

}

Now instance of this test class can be passed as parameter to the Action deletegate.

Upvotes: 0

Sweeper
Sweeper

Reputation: 271625

This is nested generic type parameter. From the outermost layer, you can see that this is an Action<T> delegate. And the delegate needs an argument of type T. In this case, T is replaced by IFieldsetter<Contact>. i.e. Action<IFieldSetter<Contact>> needs an argument of type IFieldSetter<Contact>. Now the IFieldSetter<T> interface sets a field of type T. And in this case, T is replaced by Contact so IFieldSetter<Contact> sets a field of type Contact.

Let's sum up: Action<IFieldsetter<Contact>> represents an Action delegate that needs an argument of type IFieldSetter which can set a field of type Contact. Now do you understand?

Upvotes: 1

Brian Holley
Brian Holley

Reputation: 41

A C# System.Action (MSDN) is a delegate object where Action<T> is the equivalent of a delegate function matching void FunctionName(T). So you can set it to a function and later on call that function.

The section generics block <Contact> applies to the IFieldsetter, so you have an Action that takes an argument of IFieldsetter<Contact>. Without knowing anything about the IFieldsetter I can't tell you what it's going to do with the Contact generic argument there.

In order to use this, you'll can have something resembling the following:

void Main()
{
    FunctionThatDoesStuff(SetField);
}
void FunctionThatDoesStuff(Action<IFieldsetter<Contact>> action)
{
    var setter = new IFieldsetter<Contact>();
    action(setter);
}
void SetField(IFieldsetter<Contact> setter)
{
}

Upvotes: 1

Related Questions