CSE459_CSharp_03_Generics

CSE459_CSharp_03_Generics - Programming in C# Generics CSE...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Programming in C# Generics CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Motivation l See the Type Unification and the use of the ArrayList set of slides. l In summary, four main goals: 1. Increase type safety (statically) 2. Eliminate type casts 3. Eliminate box’ing and unbox’ing 4. C++ has templates l Syntax is similar to C++
Background image of page 2
Generic Syntax l Write public class Stack<T> { … } l T is the type variable l Stack<int> myStack = new Stack<int>(); l Can have several type parameters l Dictionary<TKey, TValue> l Compiler will now enforce type safety l myStack.Push(4.3) // Compiler error Design Note It is customary to use T for a generic single type. For multiple types or in cases where the type is clear a more specific name should be used. This is pre-fixed by a capital T.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Terminology l Why the name Generic? l We separate the behavior from the type allowing more generic behavior descriptions. l Also called Parametric Polymorphism l We supply a type parameter and the same code or behavior applies to this type.
Background image of page 4
Generic Parameterization l Generics can be used with: l Types l Struct l Interface l Class l Delegate l Methods
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Using Generics - Types l Can be used to easily create non- generic derived types: public class IntStack : Stack< int > { }
Background image of page 6
Using Generics - Types l Can be used in internal fields, properties and methods of a class: public struct Customer<T> { private static List<T> customerList; private T customerInfo; public T CustomerInfo { get ; set ; } public int CompareCustomers( T customerInfo ); } A better type name here would be TCustomerInfo
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
l Using the type is like using any other non-generic type. l
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2012 for the course CSE 459.24 taught by Professor Crawfis during the Winter '11 term at Ohio State.

Page1 / 30

CSE459_CSharp_03_Generics - Programming in C# Generics CSE...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online