Generic Method Multiple (Or) Type Constraint

Generic method multiple (OR) type constraint

That is not possible. You can, however, define overloads for specific types:

public void test(string a, string arg);
public void test(string a, Exception arg);

If those are part of a generic class, they will be preferred over the generic version of the method.

Generic method with multiple constraints

It is possible to do this, you've just got the syntax slightly wrong. You need a where for each constraint rather than separating them with a comma:

public TResponse Call<TResponse, TRequest>(TRequest request)
where TRequest : MyClass
where TResponse : MyOtherClass

How do I specify multiple generic type constraints on a single method?

 public void foo<TTypeA, TTypeB>() where TTypeA : class, A 
where TTypeB : class, B

Generic method where T is type1 or type2

Although you could use a generic constraint to limit the type of each generic argument T, unfortunately there is none that would allow you to enforce at compile time whether T is type1 or type2.

Nor is there any way to enforce at compile time that your generic argument can only be of any primitive type (int, long, double, ...).

multiple generic type constraint in vb.net allows access to first type constraint members only

By using just a comma you are indicating a second generic type parameter. Multiple constraints for a single parameter must be grouped with braces:

Friend Class MainCaller(Of T As {ITest, ITest2})

The VB code you have is equivalent to this C#:

public class MainCaller<T, ITest2> where T : ITest

C# generics syntax for multiple type parameter constraints

void foo<TOne, TTwo>() 
where TOne : BaseOne
where TTwo : BaseTwo

More info here:

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters#constraining-multiple-parameters

Generic with multiple classes

First of all, your code that tries to set two type constraints on generic parameter T1 does not compile

where T1 : Supplier, new()
where T1 : Employee, new()

with the following error:

A constraint clause has already been specified for type parameter 'T1'. All of the constraints for a type parameter must be specified in a single where clause.

As MSDN article states you can use only one where constraint on each generic parameter (see http://msdn.microsoft.com/en-us/library/bb384067.aspx).

"With multiple type parameters, use one where clause for each type parameter..."

You also cannot put multiple class names into one 'where' constraint. Only one class name and several interfaces.

where T1 : Supplier, IContractor, IComparable, new()

Keep in mind that this constraint dictates that the actual type you provide as the generic parameter T1 must be a successor of the Supplier class or Supplier class itself AND it has to implement both IContractor AND IComparable interfaces.

As soon as your method accepts a MyEntity object and you do not specify what relation it has to Employee and Supplier classes, I cannot guess how this MyEntity class knows about Employee and Supplier classes and how this relation helps you.

The only thing I can suggest is either to create an interface or a base class and inherit both of your classes from it. This is the only good reason I see for creating a generic method. It could look like this:

class Program
{
static void Main(string[] args)
{
Method1<Employee>();
Method1<Supplier>();
}

private static void Method1<T1>()
where T1 : IContractor, new()
{

}
}

public class Supplier : IContractor
{
string IContractor.Name
{
get{return "Supplier-Mufflier";}
}
}

public class Employee : IContractor
{
string IContractor.Name
{
get{return "Employee-Merloyee";}
}
}

public interface IContractor
{
string Name
{
get;
}
}

If your classes Supplier and Employee do not have something important in common that is enough for creating a common interface they could implement then you should not make a generic method for processing them.

Create an overloaded method for each of such types.

Imagine you have two classes: Wife and Wine. Both have an attribute of Age and of the same type too. But do not even think of creating a common interface IAged for those classes. The essence of the classes and the meaning of the Age is so different that one should never unify them. Nevertheless some common logic might perfectly serve you. For example:

private double AgeQualify(Wife someWife)
{
return 1 / (someWife.Age * someWife.Beachness);
}

private double AgeQualify(Wine someWine)
{
return someWine.Age / someWine.Sugar;
}

Generic with multiple constraints

As I was writing the question I came up with the answer and thought I would post it instead of deleting the question.

I just need to redefine the class:

public class MyClass<T> where T : BaseClass, IInterface
{
public MyClass(T value)
{
Register(value);
}
}


Related Topics



Leave a reply



Submit