Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Type System1. 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.

Similar presentations


Presentation on theme: "The Type System1. 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."— Presentation transcript:

1 The Type System1

2 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

3 The Type System3 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

4 The Type System4 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

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

6 The Type System6 CLR Type System 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

7 The Type System7

8 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

9 The Type System9 Fig 3-2

10 The Type System10 CLR Built-in Value Types CIL NameBase Framework Name (Framework Class Library) DescriptionCLS Support boolSystem.BooleanBoolean, true or falseY charSystem.CharUnicode characterY int8System.SByteSigned 8-bit integerN int16System.Int16Signed 16-bit integerY int32System.Int32Signed 32-bit integerY int64System.Int64Signed 64-bit integerY unsigned int8System.ByteUnsigned 8-bit integerY unsigned int16System.UInt16Unsigned 16-bit integerN unsigned int32System.UInt32Unsigned 32-bit integerN unsigned int64System.UInt64Unsigned 64-bit integerN float32System.SingleIEEE 32-bit floating-point numberY float64System.DoubleIEEE 64-bit floating-point numberY native intSystem.IntPtrSigned native integer, equivalent to the machine word size (32 bits on a 32-bit machine, 64 bits on a 64-bit machine) Y native unsigned int System.UIntPtrUnsigned native integerN

11 The Type System11 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

12 The Type System12 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

13 The Type System13 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

14 The Type System14 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

15 The Type System15 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

16 The Type System16 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

17 The Type System17 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

18 The Type System18 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


Download ppt "The Type System1. 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."

Similar presentations


Ads by Google