开发者

How to make a Generic Collection with Type Constraint that implement two interfaces?

开发者 https://www.devze.com 2023-02-07 22:51 出处:网络
excuse me I havent dealt much with generic in c# according to this question ,how is it possible to make a generc collection

excuse me I havent dealt much with generic in c#

according to this question ,how is it possible to make a generc collection that implement two interfaces i was looking for a direct way like this:of course it makes error and totally is wrong.

interface IEmployee {void DisplayInfo();}

interface ISalary {void CalculateSalary();}


class Nurse : IEmployee, ISalary
{
 //some Implementation
}


class Doctor : IEmployee, ISalary
{
 //some Implementation
}

class EntryPoint
{
 static void Main(string[] args)
  { 
  System.Collections.Generic .List<T>  employees where T:开发者_JAVA百科 ISalary,IEmployee
   =new System.Collections.Generic .List<T>();
  }

 Nurse oNurse = new Nurse();
 Doctor oDoctor = new Doctor();

 employees.Add(oNurse);
 employees.Add(oDoctor);
}

after some Reading i found that maybe i must define a generic class like this at first:

public class HospitalEmployee<T> where T : IEmployee, ISalary

{

}

and unfortunately it dosnt work ,Now I am confused and dont know what must to do exactly,please help,thank u


You can do it like this:

interface IEmployee { void DisplayInfo(); }
interface ISalaried { void CalculateSalary(); }
interface ISalariedEmployee : IEmployee, ISalaried {}
class Doctor : ISalariedEmployee { whatever }
class Nurse : ISalariedEmployee { whatever }
...
var list = new List<ISalariedEmployee>() { new Nurse(), new Doctor() };

Does that help?

Essentially the feature you really want does not exist. There is a way to say "this generic type parameter must be constructed with a type argument that implements these two interfaces" but there is, oddly enough, not a way to say "this local variable must be initialized with a reference to an object that implements these two interfaces". It is simply a shortcoming of the C# type system that you can represent that in type parameters but not in locals. What you want is:

var list = new List<IEmployee + ISalary>();

And now you can only put things into the list that implement both interfaces. But there is no such feature in C#, unfortunately. Sorry!


It is not clear what are you trying to do: create your own generic container or use List<T> to store different objects.

But as far as I understood you need something like this:

List<IEmployee> employees = new List<IEmployee>();
Nurse oNurse = new Nurse();
Doctor oDoctor = new Doctor();

employees.Add(oNurse);
employees.Add(oDoctor);

UPDATE

Just create an interface which inherits all interfaces want to use like:

interface IEmployeeWithSalery: IEmployee, ISalery {}
List<IEmployeeWithSalery> employees = new List<IEmployeeWithSalery>()


This sounds a lot like my question Storing an object that implements multiple interfaces and derives from a certain base (.net) which I asked a few weeks ago. I offer a possible workaround there which may be more work than defining and using a few "combined" interface types, but has the advantage that one can define an object to work with any particular combination of interfaces which are suitably defined without having to define a new "combined" interface type for that combination.

0

精彩评论

暂无评论...
验证码 换一张
取 消