C # 1 's core Foundation II--type system
C # 1 's core Foundation II--type system
Type characteristics of C #
Limitations of the C # 1 type system
Summarize
Type characteristics of C #
The type System of C # 1 is static, explicit, and secure. In most cases, C # is "strongly typed" and refers to static types.
static types and dynamic types
static types: each variable has a specific type, and the type is known at compile time.
Dynamic Type: The compiler cannot perform type checking, instead attempts to take an appropriate way to understand the given expression of the reference value.
C # from 1 to 3 are completely static languages. C # 4 introduces a dynamic type, but most of the code is still of a static type.
Explicit and implicit types
These two concepts are only meaningful in statically typed languages-because types are known at compile time.
Explicit types: the type of each variable must be explicitly indicated in the declaration.
Implicit type: allows the compiler to infer the type of a variable based on the purpose of the variable.
Type safety and type insecurity
Some languages (especially C and C + +) allow for some "improper" things, known as powerful, but in most cases "foot".
type safety: when casting a type, the compiler checks whether the conversion is feasible and does not trigger a compile-time error. If it is theoretically feasible, execution is found to be incorrect, and the CLR throws an exception.
type is unsafe: allowing type conversions to be forced on completely unrelated types is prone to serious consequences.
Limitations of the C # 1 type system
Set, strong and weak
Three collection types built in. NET 1.1:
- Array--Strongly typed--built into the language and runtime;
- System.Collections namespaces-Weak types-Cannot get the type safety at compile time;
- System.Collections.Specialized namespaces-Strongly typed collections-it is not ideal to create a new strongly typed collection for each type.
Lack of covariant return types
Co-change, "harmonious change", "very natural changes"->string to object: covariant;
Inverter--"inverse variation"--"abnormal change"->object to string: Invert.
Due to the lack of a covariant return type, it is necessary to implement an interface to implement a method that returns a specified type, and then explicitly implement an interface to invoke it--although feasible, but not elegant at all.
In fact, there is also a lack of inverse of the parameter type, for the interface solution is the same, for virtual methods, the solution is to carry out ordinary method overloading-it is really annoying.
Summarize
Type characteristics of C # 1:
- Statically typed--the compiler knows what type you are using;
- Explicit--must tell the compiler what type of variable it is;
- Safe-You cannot use one type as another unless there is a real conversion relationship;
- A collection is not strongly typed unless a large number of duplicate code is used for a particular element;
- Method overrides and interface implementations do not allow covariance/contravariance.
From for notes (Wiz)
C # 1 's core Foundation II--type system