C # data types (3)

Dynamic type dynamic types

Dynamic type is introduced later, he is actually a static type, but unlike other statically typed, the compiler will not check you in the end is what type (do not check you can not go to calla 'method'), which gives we can take advantage of the machine (but it also gives us the risk)

The example above, we have dynamicthree variables, a not initialize (null) is a string  one is int
when we try to go to the output d2 d3of Lengththe time, the compiler is not being given, because you are the type of dynamiccompiler not to check you
only interpreter to execute this line when the incorrect report, intthe type of data is not Lengththe property
so we have to know exactly what to do, what the risks are, or look into the problem strenuous.
We are a dynamic type can change its type, such as in front of me just to assign a 123, followed by an assignment I could give a "hello world!", The compiler will own type of a change over, which is dynamic convenient place
said earlier, dynamicthe danger is that the compiler will not call you to check the existence of a method or a property does not exist, can not be invoked to explain to that line only when the interpreter will error this is a hidden mine, unsafe
to be sure we minimize the use of this type, with the number of places is, COM接口and jsonor xmlwhen exchanging data with.


ExpandoObject

We said above dynamic, it is still very easy to use, including a little "amount can not be increased to the dynamic properties of dynamic"
dynamic d1;
d1.Age=42
above this is not acceptable, not dynamic would like to add on the increase, a good set of attributes are not out of nothing

To something out of nothing, in fact, there are ways defined when instantiatedExpandoObject

ExpandoObjectIn fact, using the dictionary interface IDictionary<string, object>, so it can take the key and value iteration


Anonymous Types Anonymous Types

Before we are the first well-defined class, classwhich has the properties / methods, then instantiate an object of this class to get
an anonymous type definitions do not allow us classto create an object, the amazing, but that does not mean classyou want to exit the stage of history because, after all, anonymous types are restricted
restriction return restrictions, in some cases anonymous types are so delicious

Said earlier, an anonymous type starting varkeywords. When you create an object, and classexamples of similar, but did not write classthe name of
look at this example, it is actually very simple, than classeasy to use, small and fast and efficient
but anonymous types are limited:
  1. Only attribute, no way. After all, is not a true class, there is no way and Field,
  2. Once the object is instantiated, you can not add attributes
  3. The attributes are readonly, once an object is created, the properties of the object will no longer powerless to change the

But the anonymous function is very useful:
  1. There is a very complex object, you want to simplify it (for example, you want to send the object to the browser)
  2. objects contain sensitive information that you do not want to let the user know

Exe first obtain information here, and then use an anonymous type to get its file name and file size

Guess you like

Origin www.cnblogs.com/springbrotherhpu/p/11564891.html