Having a collection in class
This question already has an answer here:
My suggestion is just define a generic List inside of your class and write additional Add
and Remove
methods like this and implement IEnumerable:
public class MyClass : IEnumerable
{
private List<string> myList;
public MyClass()
{
myList = new List<string>();
}
public void Add(string item)
{
if (item != null) myList.Add(item);
}
public void Remove(string item)
{
if (myList.IndexOf(item) > 0) myList.Remove(item);
}
public IEnumerable<string> MyList { get { return myList; } }
public IEnumerator GetEnumerator()
{
return myList.GetEnumerator();
}
}
This is the best way if you don't want to implement your own collection.You don't need to implement an interface to Add
and Remove
methods.The additional methods like this fits your needs I guess.
Well this is really an opinion based question which aren't specifically what SO is used for. However all implementations have different pro's and con's as you have listed, really the one you implement is the option that best fits the application use.
In my personal opinion I would go with option one and add the methods you need to interface with your inner collection. This is quite simple to implement and simple to use.
Something like.
public class AClass : IEnumerable<string>
{
public AClass()
{
this.values = new List<string>();
}
private readonly List<string> values;
public void Add(string inputString)
{
this.values.Add(inputString);
}
public void Remove(string inputString)
{
this.values.Remove(inputString);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<string> GetEnumerator()
{
return values.GetEnumerator();
}
}
Now this has simple add remove features. Also note I used a readonly
list as your list doesnt need to be rest after the constructor has been called.
Update AS Per comment.
Yes you do have convert from a List to the AClass
other class (you would have to anyway). I have added an extra constructor and an implicit operator to cast from a List or an IEnumerable in the constructor.
public class AClass : IEnumerable<string>
{
public static implicit operator AClass(List<string> collection)
{
return new AClass(collection);
}
public static implicit operator List<string>(AClass aClass)
{
return aClass.values;
}
public AClass()
{
this.values = new List<string>();
}
public AClass(IEnumerable<string> collection)
{
this.values = collection.ToList();
}
private readonly List<string> values;
public void Add(string inputString)
{
this.values.Add(inputString);
}
public void Remove(string inputString)
{
this.values.Remove(inputString);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<string> GetEnumerator()
{
return values.GetEnumerator();
}
}
This could then be used as the following ways.:
List<string> listA = new List<string>();
AClass classA = new AClass(); //parameterless constructor
AClass classA1 = new AClass(listA); //simple constructor call with collection
AClass classA2 = (AClass)listA; //direct cast
List<string> listB = (List<string>)listA; //cast back to a list
Just an idea, again still a personal opinion.
这只是Selman建议的一小部分改编,所以他的回答符合我的期望:
public class MyClass : IEnumerable<string>
{
private List<string> myList;
public MyClass()
{
myList = new List<string>();
}
public ICollection<string> MyList { get { return myList; } }
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<T> GetEnumerator(){
return myList.GetEnumerator();
}
}
链接地址: http://www.djcxy.com/p/53920.html
上一篇: 列表<T>或IList <T>
下一篇: 在课堂上收集