C # 1 's core Foundation II--type system

Source: Internet
Author: User

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.

    1. 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.

    2. 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.

    3. 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
    1. 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.
    2. 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:

    1. Statically typed--the compiler knows what type you are using;
    2. Explicit--must tell the compiler what type of variable it is;
    3. Safe-You cannot use one type as another unless there is a real conversion relationship;
    4. A collection is not strongly typed unless a large number of duplicate code is used for a particular element;
    5. Method overrides and interface implementations do not allow covariance/contravariance.


From for notes (Wiz)

C # 1 's core Foundation II--type system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.