Generics in.NET and C# Generics. ”Generic” Programming in C#/Java (as it was until Summer 2005) All classes inherit from Object So we can apply polymorphism.

Slides:



Advertisements
Similar presentations
Java Review Interface, Casting, Generics, Iterator.
Advertisements

1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
Type Checking.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
C# Data Structures AND Generics By Michael and Miles.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
11-Jun-15 Generics. A generic is a method that is recompiled with different types as the need arises The bad news: Instead of saying: List words = new.
Lecture 11: Polymorphism and Dynamic Binding Polymorphism Static (compile-time) binding Dynamic (run-time) binding.
CS 330 Programming Languages 10 / 18 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 24 / 2006 Instructor: Michael Eckmann.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Declaring and Checking Non-null Types in an Object-Oriented Language Authors: Manuel Fahndrich K. Rustan M. Leino OOPSLA’03 Presenter: Alexander Landau.
1 Types Object Oriented Programming Spring 2007.
1 A Short Introduction to (Object-Oriented) Type Systems Kris De Volder.
Generic Java 21/ What is generics? To be able to assign type variables to a class These variables are not bound to any specific type until the.
Types in programming languages What are types, and why do we need them? Types in programming languages1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
16 - Generics. 2 NOEA2009Java-kursus – Generics ”Generic” Programming in C#/Java (as it was until Summer 2005) All classes inherit from Object So we can.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Templates ~ their instantiation and specialization.
Java Implementation: Part 3 Software Construction Lecture 8.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Effective Java: Generics Last Updated: Spring 2009.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences University of Scranton October 18, 2015October 18, 2015October 18, 2015.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
C# EMILEE KING. HISTORY OF C# In the late 1990’s Microsoft recognized the need to be able to develop applications that can run on multiple operating system.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Types in programming languages1 What are types, and why do we need them?
CS 206 Introduction to Computer Science II 09 / 11 / 2009 Instructor: Michael Eckmann.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Interface: (e.g. IDictionary) Specification class Appl{ ---- IDictionary dic; dic= new XXX(); application class: Dictionary SortedDictionary ----
3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages.
Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
Generic(Parameterized ) types Mehdi Einali Advanced Programming in Java 1.
Generics Generics vs. heterogeneous collections Doing your own generics FEN 2014UCN Teknologi/act2learn1.
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Data Structures and Collections Principles.NET: –Two libraries: System.Collections System.Collections.Generics FEN 2014UCN Teknologi/act2learn1 Deprecated.
1.Net programmingGenericsNOEA / 2009 Generics 11. Generics in.NET and C#
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
JAVA GENERICS Lecture 16 CS2110 – Spring 2016 Photo credit: Andrew Kennedy.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
1 Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Design issues for Object-Oriented Languages
Generics, Exceptions and Undo Command
Type Systems Terms to learn about types: Related concepts: Type
Interfaces and Inheritance
Polymorphism CT1513.
Java for IOI.
Type Systems Terms to learn about types: Related concepts: Type
Subtype Substitution Principle
Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc.
Presentation transcript:

Generics in.NET and C# Generics

”Generic” Programming in C#/Java (as it was until Summer 2005) All classes inherit from Object So we can apply polymorphism and use Object as static type for elements in containers For instance: Object[ ] data this array may take any object as element This approach is well known from standard collections as ArrayList, HashMap etc.

Pros and Cons Pros heterogeneous collections... Cons many type casts not type safe type checking is done runtime when casting int and other native (value) type must be wrapped. (boxing – costs runtime overhead) Is this really an advantage?

Programming Error The program is incorrect, but we don’t detect it It is preferable, if : The program doesn’t work correctly, and we are told by some runtime error (exception) The compiler rejects the program (compile-time error)

Strategy Do as much error-checking compile-time as possible: Advantages: Easier to write correct programs More efficient program execution (better performance) Disadvantages: Restricts programmers creativity (less flexibility) Is this really a disadvantage?

The Idea: Types as Parameters C#/Java before 2005: ArrayList al = new ArrayList(); Customer c= (Customer)al.get(i);//cast Instead we want something like: ArrayList al = new ArrayList (); Customer c= al.get(i); – The compiler is able to check that only objects with static type Customer is placed in al – So the compiler knows that everything that may come out from al has static type Customer –So static type checking instead of dynamic type checking is possible –Dynamic casting can be avoided (but is not in all implementations) Type parameter

In C#: EmpSeqApplEmpSeqAppl Employee a1 = new Employee("Joe", "Programmer", 10000); Employee a = new Employee("Curt", "Senior Programmer", 20000); Employee b = new Employee("Carl", "Programmer", 10000); Employee c = new Employee("Karen", "System Programmer", 13000); Employee d = new Employee("Lisa", "Programmer", 11000); Employee e = new Employee("John", "System Engineer", 9000); string s = "HELLOOOO!"; IList emps = new List (); emps.Add(a1); emps.Add(a); emps.Add(b); emps.Add(c); emps.Add(d); emps.Add(e); //emps.Add(s);//COMPILER ERROR!!!!

.NET Generics

But everything comes at a price: no heterogeneous collections But that is not so bad... more complicated type system more complicated syntax one needs a pre-processor, giving slower compilation more mysterious error messages

Generics vs. Polymorphism Generics: all elements has the same static type (not object!) static typing (compile-time), static binding is possible For instance trying to insert a float to List l yields an compiler error Data Structures based on Polymorphism: elements may have different types, but all must be subtypes of Object Dynamic type-checking (when casting) Dynamic binding