为什么要使用的IList或列表? [英] why use IList or List?

查看:142
本文介绍了为什么要使用的IList或列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道一直存在很多职位上这一点,但它仍然混淆了我,你为什么要在这样的IList的接口传递和返回象IList的接口,而不是后面的具体名单。

I know there has been alot of posts on this but it still confuses me why should you pass in an interface like IList and return an interface like IList back instead of the concrete list.

我看了帖子说了很多如何可以更容易地改变实现以后,但我只是不充分看到是如何工作的。

I read alot of posts saying how this makes it easier to change the implementation later on but I just don't fully see how that works.

说,如果我有这样的方法

Say 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)
            {
                // log 
            }

        }
    }

我用的IList将帮助我我不知道如何在未来。

I am not sure how in the future by using IList will help me out.

怎么样,如果我已经在方法?如果我仍然在使用的IList?

How about if I am already in the method? Should I still be using 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());
            }

        }
    }

我能得到什么,现在使用的IList?

What do I get for using 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 implementation of a list of int's that I will need to change out?

所以基本上我需要看到一些实际的code例子如何,如果我在这里用的IList会解决一些问题上只是把名单到的一切。

So basically I need to see some actual code examples of how if I where using IList would have solved some problem over just taking List into everything.

这是我的阅读我想我可以用IEnumberable,而不是IList的,因为我刚刚经历的东西循环。

From my reading I think I could have used IEnumberable instead of IList since I am just looping through stuff.

修改
于是,我被玩弄我的一些方法,如何做到这一点。我还是不知道的返回类型(如果我要使它更具体或接口)

Edit So I been playing around with some of my methods on how to do this. I am still not sure about the return type(if I should make it more concrete 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)
                {
                                                    // nhibernate call to Load<>()
                    AvailableFeature availableFeature = featureService.LoadAvaliableFeatureById(f.FeatureId);
                    availableFeatures.Add(availableFeature);
                }
            }

            return availableFeatures;
        }

所以,我现在回到IList的一个简单的事实,我会再加入到我的域模型有什么像这样的属性。

So I am right now returning IList for the simple fact that I will then add this to my domain model what has a property like this

public virtual IList<AvailableFeature> AvailableFeatures { get; set; }

上面是一个IList本身,因为这是似乎是与NHibernate使用标准。否则,我可能已经返回IEnumberable回来,但不敢肯定。仍然无法找出用户将100%的需求(这就是返回一个具体的在有优势)。

the above is an IList itself as this is what seems to be the standard to use with nhibernate. Otherwise I might have returned IEnumberable back but not sure. Still can't figure out what the user would 100% need(that's where return a concrete has an advantage over).

编辑2

我也在想,如果我想做按引用传递在我的方法会发生什么?

I was also thinking what happens if I want to do pass by reference in my method?

private void FillAvailableFeatures(IEnumerable<AvailableFeatureVm> avaliableFeaturesVm, IList<AvailableFeature> toFill)
            {

                foreach (var f in avaliableFeaturesVm)
                {
                    if (f.HasFeature)
                    {
                                                        // nhibernate call to Load<>()
                        AvailableFeature availableFeature = featureService.LoadAvaliableFeatureById(f.FeatureId);
                        toFill.Add(availableFeature);
                    }
                }
            }

我会碰到这个问题?因为能将它们不以阵列(即具有固定大小)通过?它会为一个具体的列表会更好,也许?

would I run into problems with this? Since could they not pass in an array(that has a fixed size)? Would it be better maybe for a concrete List?

推荐答案

在这里有三个问题:我应该使用什么类型的形式参数?我应该使用局部变量?和我应该用什么的返回类型?

There are three questions here: what type should I use for a formal parameter? What should I use for a local variable? and what should I use for a return type?

这里的原则就是不要问比你更需要的IEnumerable&LT; T&GT; 传达我需要从开始到结束得到这个序列的元素。 的IList&LT; T&GT; 传达我需要获取和设置此序列以任意顺序的元素。 列表&LT; T&GT; 传达我需要获取和设置此序列的元素以任意顺序,我只接受列表。我不接受阵列

The principle here is do not ask for more than you need. IEnumerable<T> communicates "I need to get the elements of this sequence from beginning to end". IList<T> communicates "I need to get and set the elements of this sequence in arbitrary order". List<T> communicates "I need to get and set the elements of this sequence in arbitrary order and I only accept lists; I do not accept arrays."

通过要求超过你的需要,你(1)使主叫方做不必要的工作,以满足您不必要的要求;(2)进行通信的谎言给读者。问只为你要使用的东西。如果这样,来电者都有一个序列,他们不需要调用了ToList它来满足您的需求。

By asking for more than you need, you (1) make the caller do unnecessary work to satisfy your unnecessary demands, and (2) communicate falsehoods to the reader. Ask only for what you're going to use. That way if the caller has a sequence, they don't need to call ToList on it to satisfy your demand.

任何你想要使用。这是你的方法。你是唯一一个谁可以看到该方法的内部实现细节。

Use whatever you want. It's your method. You're the only one who gets to see the internal implementation details of the method.

同样的原理,逆转。 提供您的来电者要求的最低限度如果主叫方只需要枚举序列的能力,只是给他们一个的IEnumerable&LT; T&GT;

Same principle as before, reversed. Offer the bare minimum that your caller requires. If the caller only requires the ability to enumerate the sequence, only give them an IEnumerable<T>.

这篇关于为什么要使用的IList或列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆