1 / 18

The Type System

The Type System. .NET Type System. The type system is the part of the CLR that defines all the types that programmers can use, and allows developers to define new types (interfaces, methods etc.). A type is a definition, from which value can be instantiated.

marygwilson
Télécharger la présentation

The Type System

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Type System The Type System

  2. .NET Type System • The type system is the part of the CLR that defines all the types that programmers can use, and allows developers to define new types (interfaces, methods etc.). • A type is a definition, from which value can be instantiated. • Types have members (fields or methods). • Supports many of the types and operations found in modern programming languages The Type System

  3. Evolution of Type Systems • No type system – treats memory as sequence of bytes • Built-in abstractions of common types – characters, integers etc. • User-defined types • Dynamic type checking The Type System

  4. Design Challenge • Type systems provided by many programming languages are incompatible. • E.g. C, C++, SmallTalk, Java • Developing a single type system for multiple languages poses a difficult design challenge. • .NET’s approach to CLR design: accommodates most of the common types and operations supported in modern object-oriented programming languages The Type System

  5. Source File C++, C#, VB .NET Language-Specific .NET Compiler C++, C#, VB .NET Types/Metadata can be imported by Intermediate Language (IL) Code and Metadata Executable File can be referenced by Execution System Runtime CLR – Programming Language Interaction The Type System

  6. CLR Type System Value Types Reference Types Built-in Value Types Object Types Interface Types Pointer Types User-Defined Value Types User-Defined Reference Types CLR Type System The Type System

  7. The Type System

  8. CLR Type System • Value Type • Consists of a sequence of bits in memory • Simple or primitive types e.g. a 32-bit integer (build-in) • User-defined e.g. structures • Compared by equality (data value) • Inherits from System.ValueType or System.Enum • cannot be inherited by other types (i.e. sealed) • Allocated on stack • Reference Type • Combines the address of a value and the value’s sequence of bits • Can be compared by identity (same object?) or equality (same data?) • e.g. classes, interface, array, string • Can be sealed or not • Allocated on heap, only with a reference on the stack The Type System

  9. Fig 3-2 The Type System

  10. CLR Built-in Value Types The Type System

  11. User-Defined Value Types • Enumerations • A way to name a group of values of some integer type • Used to define types whose values have meaningful names rather than just numbers • Structures, can contain • A collection of elements of various types • Methods (both static and instance) • Fields (both static and instance) • Properties (both static and instance) • Logical field of a type, system generated accessors (get_ & set_) • Events (both static and instance) • Used to expose asynchronous changes in an observed event The Type System

  12. Enumeration Module Module1 Enum CardSuit clubs = 0 diamonds = 1 hearts = 2 spades = 3 End Enum Sub Main() Dim cs As CardSuit = CardSuit.hearts System.Console.Out.WriteLine("The value cs is {0}", cs) End Sub End Module The Type System

  13. Structure Module Module1 Structure PixelCoord Public x As Single Public y As Single End Structure Sub Main() Dim p As PixelCoord p.x = 200 p.y = 100 System.Console.Out.WriteLine("The value p.x is {0}", p.x) System.Console.Out.WriteLine("The value p.y is {0}", p.y) End Sub End Module The Type System

  14. Reference Types • Combines the address of a value and the value’s sequence of bits • Accessed through a strongly typed reference • Allocated on the garbage collected heap • Three main categories • Object Types • Interface Types • Pointer Types The Type System

  15. Object Types • Object • All object types inherit, either directly or indirectly, from the CLR type System.Object class • Key methods: Equals(), Finalize(), GetHashCode(), GetType(), MemberwiseClone(), ReferenceEquals(), ToString() • String • Sealed, immutable, allows greater optimization • Key methods: Compare(), Concatenate(), Conversion() etc. • Key properties: Length The Type System

  16. Interface Types • A partial specification of a type, binds implementers to provide implementations of the members contained in the interface. • An interface type may define • Methods (static or instance) • Fields (static) • Properties (static or instance) • Events (static or instance) • All instance methods in an interface are public, abstract, and virtual The Type System

  17. Pointer Types • Provide a means of specifying the location of either code or a value • Three pointer types • Unmanaged function pointers refer to code • Unmanaged pointers refer to values • Managed Pointers are known to the garbage collector and are updated if they refer to an item that is moved on the garbage collected heap The Type System

  18. Some Types Issues • Assignment Compatibility (Listing 2.10) • A reference type T, can refer to an object • With an exact type of T, or • That is a subtype of T, or • That supports the interface T • Nested Types: types inside of other types (inner class) • Visibility: refers to whether a type is available outside its assembly (export or not) • Accessibility: members of a type can have different accessibility levels • public: available to all types • assembly: available to all types within this assembly • family: available in the type’s definition and all its subtypes • Private: available only in the type’s definition The Type System

More Related