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>
.
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?
A dynamic list is rule-based and automatically updates when a prospect's data changes. You can use either list as a recipient list or suppression list for list emails and engagement programs. The benefit of a dynamic list is that it's created based on criteria and updates as prospects meet or fail to meet the criteria.
var DyObjectsList = new List<dynamic>; dynamic DyObj = new ExpandoObject(); if (condition1) { DyObj. Required = true; DyObj. Message = "Message 1"; DyObjectsList . Add(DyObj); } if (condition2) { DyObj.
List in C# is a collection of strongly typed objects. These objects can be easily accessed using their respective index. Index calling gives the flexibility to sort, search, and modify lists if required. In simple, List in C# is the generic version of the ArrayList.
In C# 4.0, a new type is introduced that is known as a dynamic type. It is used to avoid the compile-time type checking. The compiler does not check the type of the dynamic type variable at compile time, instead of this, the compiler gets the type at the run time.
There are 3 "general" types (although not all are real types) in C#: object, var and dynamic.
Object
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.
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();
Var
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();
Dynamic
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();
note that dynamic can cause exceptions very easily if used carelessly:
public void f(dynamic x) { 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.
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.
The difference is that if you use object and you try to access some member of your object it will be a compile time error (because object doesn't have this member). In order to work you need to know what the type is and cast it.
With dynamic you can access any member - no compile time error. If the member doesn't exist at runtime it would be a runtime error. This is the way to go if you know that your heretogeneous objects all have the same member for example.
However if this is the case there is another more clear solution: You can define an interface, with this member and then make all your heretogeneous objects implement it and your list can be List<IYourInterface>
.
Keep in mind that dynamic's performance might be slightly worse, because of the, well, dynamic type resolution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With