Logic and its application to Collections.Generic and inheritance

后端 未结 6 1861
猫巷女王i
猫巷女王i 2021-01-06 20:49

Everything inherits from object. It\'s the basis of inheritance. Everything can be implicitly cast up the inheritance tree, ie.

object me = new Person();
         


        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-06 21:27

    The linq workaround is a good one. Another workaround, since you are using type object, is to pass the list as IEnumerable (not the generic version).

    Edit: C# 4 (currently beta) supports a covariant type parameter in IEnumerable. While you won't be able to assign directly to a List, you can pass your list to a method expecting an IEnumerable.

    提交回复
    热议问题