I have class with method remove, it needs to delete Customer from CustomerList.
public class ObservableList<T>
{
List <T> CustomerList = new List<T>();
public void Remove(ObservableList<Customer> list) // not correct
{
//something for delete from list;
}
}
indexer in this class:
public T this[int indexer]
{
get { return CustomerList[indexer]; }
set { CustomerList.Add(value); }
}
The string for testing this method looks like this:
ObservableList<Customer> list = new ObservableList<Customer>();
LoadFromBin(list, "source1.bin");
list.Remove(list[2]);
The question is, how to properly format the input data for this method(Remove)? And is the indexer executed correctly in my code?
CodePudding user response:
Based on the attempted usage Remove
should accept T
, not a collection:
public class ObservableList<T>
{
List <T> CustomerList = new List<T>();
public void Remove(T toRemove) => CustomerList.Remove(toRemove);
}
But again, based on usage it seems you should consider implementing RemoveAt
:
public class ObservableList<T>
{
List <T> CustomerList = new List<T>();
public void Remove(T toRemove) => CustomerList.Remove(toRemove);
// usage: list.RemoveAt(2);
public void RemoveAt(int index) => CustomerList.RemoveAt(index);
}
As for the indexer - it is covered in the docs, your set
method should set value for index, not add element:
public T this[int indexer]
{
get { return CustomerList[indexer]; }
set { CustomerList[indexer] = value; }
}
But in general, unless it is some exercise you are doing - consider advice from @Jeroen Mostert in the comments - see if existing collections won't serve your needs better than trying to write your own.