Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module 5: Common Type System

Similar presentations


Presentation on theme: "Module 5: Common Type System"— Presentation transcript:

1 Module 5: Common Type System

2 Overview An Introduction to the Common Type System
Elements of the Common Type System Object-Oriented Characteristics

3 An Introduction to the Common Type System
Common Type System Architecture Value Types vs. Reference Types

4 Common Type System Architecture
CLR Type System Value Types (System.ValueType) Fields Methods Properties Reference Types (System.Object) Built-in Types Primitive Types Delegates Built-in Types User-defined Value and Reference Types Interface Types Contract specifying method and property signature

5 Value Types vs. Reference Types
Value Types Are Primitive or User-Defined Structures Allocated on stack Assigned as copies Default behavior is pass by value Reference Types Are Objects That Are: Allocated on heap using the new keyword Assigned as references Passed by reference

6 Elements of the Common Type System
Primitive Types Objects Constructors Properties Custom Types Enumerations Interfaces

7 Primitive Types Simple Small Types Common in Most Languages
Naming Differences C# Support Conversions

8 Objects Every Class Inherits from System.Object
Objects Specify Data and Behavior Fields Define the Data Methods Define the Behavior class Accumulator { public float Result; public void Add(float amount) Result += amount; }

9 Constructors Constructors Are Used to Initialize Classes
Constructors Can Have Zero or More Parameters class Rectangle { private int x1,y1,x2,y2; public Rectangle(int x1, int y1, int x2,int y2) this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; }

10 Properties Properties Are Similar to Fields
Properties Use get and set Accessor Methods for Managing Data Values public float Start { get return start; } set if (start >= 0) start = value;

11 Custom Types Inherit from System.ValueType
Are Defined with the struct Keyword in C# Can Have Methods, Properties, and Fields struct Employee { public string Name; public ushort Age; public DateTime HireDate; public float Tenure() TimeSpan ts = DateTime.Now – HireDate; return ts.Days/(float)365; }

12 Enumerations .NET Framework Enumerations Inherit from System.Enum
Use the enum keyword Bit Flags enum SeatPreference : ushort { Window, //Assigned value 0 Center, //Assigned value 1 Aisle //Assigned value 2 }

13 Interfaces An Interface Is a Contractual Description of Methods and Properties An Interface Has No Implementation Use Casting in Client Code to Use an Interface interface ICDPlayer { void Play(short playTrackNum); void Pause(); void Skip(short numTracks); short CurrentTrack get; set; }

14 Object-Oriented Characteristics
Abstraction Encapsulation Inheritance Polymorphism

15 Abstraction Abstraction Works from the Specific to the General
Grouping Elements Makes It Easier to Work with Complex Data Types Abstraction Is Supported Through Classes

16 Encapsulation Encapsulation Is the Process of Hiding Internal Details of a Class Encapsulation Keywords public protected internal private Type-Level Accessibility Nested Classes

17 Inheritance Inheritance Is the Reuse of Class Members in Other Classes
The Common Type System Only Supports Single Inheritance for Classes Member Hiding Redefine the same method in the derived class Use the new keyword Abstract Members Sealed Classes

18 Polymorphism Polymorphism Allows a Reference Variable to Call the Correct Method Virtual Methods Enable Polymorphism in the Common Type System Use the virtual keyword in the base class Use the override keyword in the derived class Sealed Methods

19 Lab 5: Building Simple Types

20 Review An Introduction to the Common Type System
Elements of the Common Type System Object-Oriented Characteristics


Download ppt "Module 5: Common Type System"

Similar presentations


Ads by Google