1.Net programmingGenericsNOEA / 2009 Generics 11. Generics in.NET and C#

Slides:



Advertisements
Similar presentations
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Advertisements

1-May-15 Java 1.5. Reason for changes “The new language features all have one thing in common: they take some common idiom and provide linguistic support.
5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
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.
CS 330 Programming Languages 10 / 18 / 2007 Instructor: Michael Eckmann.
Generic types for ArrayList Old Java (1.4 and older): ArrayList strings = new ArrayList(); strings.enqueue(“hello”); String word = (String) strings.get(0);
Creating Generic Classes. Introduction Java Generics were added to allow for type- safe collections and eliminate the need for burdensome, code-cluttering.
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.
1 Types Object Oriented Programming Spring 2007.
15-Jul-15 Generics. ArrayList s and arrays A ArrayList is like an array of Object s, but... Arrays use [ ] syntax; ArrayList s use object syntax An ArrayList.
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.
Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
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.
Effective Java: Generics Last Updated: Spring 2009.
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.
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.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
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 ----
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.
GENERICS AND THE JAVA COLLECTIONS FRAMEWORK Lecture 16 CS2110 – Fall 2015 Photo credit: Andrew Kennedy.
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.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
Data Structures and Collections Principles.NET: –Two libraries: System.Collections System.Collections.Generics FEN 2014UCN Teknologi/act2learn1 Deprecated.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
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
Type Systems Terms to learn about types: Related concepts: Type
Generics A Brief Review 16-Nov-18.
Polymorphism CT1513.
Java for IOI.
ArrayLists 22-Feb-19.
Type Systems Terms to learn about types: Related concepts: Type
Subtype Substitution Principle
Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc.
Presentation transcript:

1.Net programmingGenericsNOEA / 2009 Generics 11. Generics in.NET and C#

2.Net programmingGenericsNOEA / 2009 ”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.

3.Net programmingGenericsNOEA / 2009 Pro and cons Pro –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?

4.Net programmingGenericsNOEA / 2009 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)

5.Net programmingGenericsNOEA / 2009 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?

6.Net programmingGenericsNOEA / 2009 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= 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 –Dynamic casting is avoided Typeparameter

7.Net programmingGenericsNOEA / 2009 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 = "HELLO!"; 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!!!!

8.Net programmingGenericsNOEA / 2009.NET Generics

9.Net programmingGenericsNOEA / us/library/system.collections.generic.aspx

10.Net programmingGenericsNOEA / 2009

11.Net programmingGenericsNOEA / 2009 How to write your own generics: MyListProjectMyListProject using System.Collections.Generic; public class MyList : IEnumerable, ICollection T[] list; public void Add(T o) public T Get(int i)

12.Net programmingGenericsNOEA / 2009

13.Net programmingGenericsNOEA / 2009 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 (in c++ anyway)

14.Net programmingGenericsNOEA / 2009 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

15.Net programmingGenericsNOEA / 2009 Further reading In C#: – us/dnvs05/html/csharp_generics.asphttp://msdn.microsoft.com/library/default.asp?url=/library/en- us/dnvs05/html/csharp_generics.asp In Java: – In C++: – us/vclang/html/vcrefTemplates.asphttp://msdn.microsoft.com/library/default.asp?url=/library/en- us/vclang/html/vcrefTemplates.asp