Module 5: Common Type System
Overview An Introduction to the Common Type System Elements of the Common Type System Object-Oriented Characteristics
An Introduction to the Common Type System Common Type System Architecture Value Types vs. Reference Types
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
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
Elements of the Common Type System Primitive Types Objects Constructors Properties Custom Types Enumerations Interfaces
Primitive Types Simple Small Types Common in Most Languages Naming Differences C# Support Conversions
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; }
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; }
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;
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; }
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 }
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; }
Object-Oriented Characteristics Abstraction Encapsulation Inheritance Polymorphism
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
Encapsulation Encapsulation Is the Process of Hiding Internal Details of a Class Encapsulation Keywords public protected internal private Type-Level Accessibility Nested Classes
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
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
Lab 5: Building Simple Types
Review An Introduction to the Common Type System Elements of the Common Type System Object-Oriented Characteristics