In C #, can you set Or to the where clause?

if i have this code:

public interface IJobHelper
{
    List<T> FilterwithinOrg<T>(IEnumerable<T> entities) where T : IFilterable;
}

is there anything that supports the execution of something like this:

public interface IJobHelper
{
    List<T> FilterwithinOrg<T>(IEnumerable<T> entities) where T : IFilterable or ISemiFilterable
}

therefore, it will accept anything that supports one of the two interfaces. I am basically trying to create overload.

+5
source share
5 answers

The language does not support "oring" together interfaces / classes in a sentence where.

You will need to specify them separately with different method names so that the signatures are different.

public interface IJobHelper
{
    List<T> FilterwithinOrg<T>(IEnumerable<T> entities) 
        where T : IFilterable
    List<T> SemiFilterwithinOrg<T>(IEnumerable<T> entities) 
        where T : ISemiFilterable
}

. , , , , , , IBaseFilterable.

public interface IBaseFilterable { }
public interface IFilterable : IBaseFilterable { }
public interface ISemiFilterable : IBaseFilterable { }

public interface IJobHelper
{
    List<T> FilterwithinOrg<T>(IEnumerable<T> entities)
        where T : IBaseFilterable
}

, , , , .

+2

, AND, OR.

( T IFilterable ISemiFilterable)

public interface IJobHelper
{
    List<T> FilterwithinOrg<T>(IEnumerable<T> entities) where T : IFilterable, ISemiFilterable
}
+3

, .

+2

, , , .

public interface IFilterable { }

public interface IFullyFilterable : IFilterable { }

public interface ISemiFilterable : IFilterable { }

... where T : IFilterable { }
+2

, ...

 interface baseInterface {}
 interface IFilterable: baseInterface {}
 interface ISemiFilterable: baseInterface {}

,

 List<T> FilterwithinOrg<T>(IEnumerable<T> entities) where T : baseInterface 

The only drawback is that the compiler will not allow you to use methods from any of the derived interfaces without casting ...

0
source

All Articles