I have a class, which is just a wrapper over a list, i.e.,
public class Wrapper
{
public List<int> TList
{get;set;}
public Wrapper()
{
TList=new List<int>();
}
}
I want to make Wrapper inherits from IEnumerable so that I can use the following syntax:
Wrapper wrapper = new Wrapper()
{
2,4,3,6
};
Any idea how to which interface to implement IEnumerable<T>, or IEnumerable, and how to define the method body?
If you implement
ICollection<int>you get the desired functionality.Correction: you actually only need to implement
IEnumerableorIEnumerable<T>and have a publicAddmethod in your class:(I also took the liberty of making the
TListsetter private; it is usually recommended that collection type properties are read-only so that the collection as such can not be substituted by any code outside the type.)