I know that there were a lot of messages on it, but it still confuses me why you should go through an interface like IList and return an interface like IList instead of a specific list.
I read a lot of posts about how this makes it easier to change the implementation later, but I just don't quite understand how this works.
Tell me if I have this method
public class SomeClass { public bool IsChecked { get; set; } } public void LogAllChecked(IList<SomeClass> someClasses) { foreach (var s in someClasses) { if (s.IsChecked) {
I'm not sure how in the future using IList will help me.
How about if I'm already in a method? Should I use IList?
public void LogAllChecked(IList<SomeClass> someClasses) { //why not List<string> myStrings = new List<string>() IList<string> myStrings = new List<string>(); foreach (var s in someClasses) { if (s.IsChecked) { myStrings.Add(s.IsChecked.ToString()); } } }
What can I use to use IList now?
public IList<int> onlySomeInts(IList<int> myInts) { IList<int> store = new List<int>(); foreach (var i in myInts) { if (i % 2 == 0) { store.Add(i); } } return store; }
how about now? Is there some new int list implementation that I will need to change?
So basically I need to see some actual code examples, as if I, where to use IList, would solve some kind of problem by simply taking List into everything.
From my reading, I think that I could use IEnumberable instead of IList, because I am just sorting through things.
Edit So I played with some of my methods on how to do this. I'm still not sure about the return type (if I have to make it more specific or an interface)
public class CardFrmVm { public IList<TravelFeaturesVm> TravelFeaturesVm { get; set; } public IList<WarrantyFeaturesVm> WarrantyFeaturesVm { get; set; } public CardFrmVm() { WarrantyFeaturesVm = new List<WarrantyFeaturesVm>(); TravelFeaturesVm = new List<TravelFeaturesVm>(); } } public class WarrantyFeaturesVm : AvailableFeatureVm { } public class TravelFeaturesVm : AvailableFeatureVm { } public class AvailableFeatureVm { public Guid FeatureId { get; set; } public bool HasFeature { get; set; } public string Name { get; set; } } private IList<AvailableFeature> FillAvailableFeatures(IEnumerable<AvailableFeatureVm> avaliableFeaturesVm) { List<AvailableFeature> availableFeatures = new List<AvailableFeature>(); foreach (var f in avaliableFeaturesVm) { if (f.HasFeature) {
So, I'm returning an IList right now for the simple fact that I will add this to my domain model, which has a property like this
public virtual IList<AvailableFeature> AvailableFeatures { get; set; }
the above is IList itself, as that is what seems like a standard for use with nhibernate. Otherwise, I could return IEnumberable back, but not sure. Still can't understand what the user needs 100% (which is when concrete return takes precedence).
Edit 2
I also thought about what would happen if I wanted to do a pass by reference in my method?
private void FillAvailableFeatures(IEnumerable<AvailableFeatureVm> avaliableFeaturesVm, IList<AvailableFeature> toFill) { foreach (var f in avaliableFeaturesVm) { if (f.HasFeature) {
Am I having problems with this? So how can they not go into an array (which has a fixed size)? Would it be better perhaps for a specific list?