清单<对象> VS名单<动态> [英] List<Object> vs List<dynamic>

查看:95
本文介绍了清单<对象> VS名单<动态>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建对象的异类列表(自定义类)。我首先想到的是要建立一个列表与LT; ISomeMarkerInterface> 但我很快了解到,这是不是我想要的。我的下一个想法是列表<动态> ,这似乎并没有成为一个坏主意。不过,我在做一些研究,并在有关装箱和拆箱这文章,并在这个例子来,他们正在做的基本上是我想用什么列表<对象>

I need to create a heterogeneous List of objects (custom classes). My first thought was to create a List<ISomeMarkerInterface> but I quickly learned that this is not what I want. My next thought was List<dynamic> and this didn't seem to be a bad idea. However, I was doing some research and came across this article about boxing and unboxing and in the example, they're doing basically what I want using List<Object>.

除了一个事实,即动态将在运行时评估和对象在编译的时候,是什么之间的差异列表&LT;动态&GT; 列表&LT;对象&gt; ?是不是他们本质上是一回事吗?

Aside from the fact that dynamic will be evaluated at runtime and Object at compile-time, what is the difference between List<dynamic> and List<Object>? Aren't they essentially the same thing?

推荐答案

有3一般类型(虽然不是所有的都是真正的类型),在C#中:对象,VAR和动态。

There are 3 "general" types (although not all are real types) in C#: object, var and dynamic.

对象

这是实际的类型,像任何其他类型的,有一个特殊的规则:如果一个类型不继承,它的对象的继承。由此看来,它遵循的所有的类型继承的对象的直接或间接。

An actual type, like any other type, with one special rule: if a type doesn't inherit, it inherits from object. From this, it follows that all types inherit from object, directly or indirectly.

重点:对象是一个类型即可。一个对象可以是类型的对象的,而类型都有它的方法,如toString()方法。由于一切从继承的对象的,一切都可以上溯造型成的对象的。当你分配对象给的对象的参考,你在做上溯造型只是当你指定喜欢的的大象的对象类型到的动物的参考,其中大象的继承自的动物

Emphasis: object is a type. An object can be of type object, and the type has its methods, like ToString(). Since everything inherits from object, everything can be upcast into object. When you assign an object to an object reference, you are doing upcasting just like when you assign an Elephant type object to an Animal reference where Elephant inherits from Animal.

SomeType x = new SomeType();
object obj = x;
obj.DoSomething();


  • OBJ 的被视为是在编译时类型的对象的,而且会是类型的对象的在运行时(这是合乎逻辑的,因为这是一个实际的类型 - OBJ的被声明为的对象的所以只能是此类型)

  • obj.DoSomething()的会导致编译时错误,因为的对象的没有这种方法,不管是否SOMETYPE有它。

    • obj is treated as being of type object at compile time, and will be of type object at runtime (which is logical, since it is an actual type - obj is declared as object so can only be of that type)
    • obj.DoSomething() will cause a compile-time error, as object does not have this method, regardless of whether SomeType has it.
    • 瓦尔

      这是不是一个实际的类型,它仅仅是简写编译器,找出基于分配的右侧,我的类型。

      This is not an actual type, it is merely shorthand for "compiler, figure out the type for me based on the right side of the assignment".

      SomeType x = new SomeType();
      var obj = x;
      obj.DoSomething();
      


      • OBJ 的被视为是在编译时类型的 SOMETYPE 的,并且会在运行时类型的 SOMETYPE 的,就像如果你有写SOMETYPE而不是变种。

      • 如果的 SOMETYPE 的有一个方法的 DoSomething的()的,这code将工作

      • 如果的 SOMETYPE 的不具有法,code会导致编译时错误

        • obj is treated as being of type SomeType at compile time, and will be of type SomeType at runtime, just as if you had written "SomeType" instead of "var".
        • if SomeType has a method DoSomething(), this code will work
        • if SomeType doesn't have the method, the code will cause a compile-time error
        • 动态

          这是告诉编译器禁用编译时类型的变量检查类型。一个对象被视为具有类型的动态的在编译时和运行时。

          This is a type that tells the compiler to disable compile-time type checking on the variable. An object is treated as having the type dynamic at compile-time and run-time.

          SomeType x = new SomeType();
          dynamic obj = x;
          obj.DoSomething();
          


          • obj是类型的动态的在编译和运行时

          • 如果的 SOMETYPE 的有一个方法的 DoSomething的()的,这code将工作

          • 如果的 SOMETYPE 的不具有法,code编译,但在运行时抛出一个异常

          • 注意,动态的若不慎使用可以很容易导致异常:

            • obj is of type dynamic at compile and run time
            • if SomeType has a method DoSomething(), this code will work
            • if SomeType doesn't have the method, the code will compile, but throw an exception at run-time
            • note that dynamic can cause exceptions very easily if used carelessly:

              public void f(dynamic x)
              { 
                  x.DoSomething();
              }
              


            • 这将抛出一个异常,如果的 X 的是,没有一个类型的的DoSomething 的方法,但它仍然将有可能把它和传递任何对象作为参数没有编译时错误,导致错误,只显示自身在运行时,可能只有在特定情况下 - 一个潜在的错误。所以,如果你在任何一类的公有接口中使用动态,你应该总是使用手动反射式检查在运行时,仔细地处理异常,或不做摆在首位。

              This will throw an exception if x is of a type that doesn't have the DoSomething method, but it will still be possible to call it and pass any object as the parameter without a compile-time error, causing an error that only shows itself at run-time, and possibly only in specific circumstances - a potential bug. So if you use dynamic in any kind of public interface of a class, you should always manually type-check at runtime using reflection, carefully deal with exceptions, or not do it in the first place.

              请注意:该对象被称为永远不会改变它的类型,当然。虽然的 OBJ 的可能的对象的,在 X 的,它指的仍然是的 SOMETYPE

              Note: the object being referred to never changes its type, of course. While obj may be object, the x that it refers to is still SomeType.

              这篇关于清单&LT;对象&gt; VS名单&LT;动态&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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