Generics in C# 1. Generics List vs. non-generic ArrayList Generic List Namespace System.Collections.Generic List list = new List (); List.add(”Anders”);

Slides:



Advertisements
Similar presentations
1 Generic Collections Chapter Objectives You will be able to Use generic collection classes available in the.NET framework.
Advertisements

CHAPTER 12 GENERICS Introduction to java 1. Assignment 5 Solution See Eclipse.
1 ADT and Data Structure Example Generics / Parameterized Classes Using a Set Implementing a Set with an Array Example: SetADT interface Example: ArraySet.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 17 – Generic Programming.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 18 – Generic Classes.
Generics. DCS – SWC 2 Generics In many situations, we want a certain functionality to work for a variety of types Typical example: we want to be able.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Threads in C# Threads in C#.
C#: Data Types Based on slides by Joe Hummel. 2 UCN Technology: Computer Science Content: “.NET is designed around the CTS, or Common Type System.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L13 (Chapter 21) Generics.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Creating Generic Classes. Introduction Java Generics were added to allow for type- safe collections and eliminate the need for burdensome, code-cluttering.
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
3. Data Types. 2 Microsoft Objectives “.NET is designed around the CTS, or Common Type System. The CTS is what allows assemblies, written in different.
1 Chapter 21 Generics. 2 Objectives F To know the benefits of generics (§21.1). F To use generic classes and interfaces (§21.2). F To declare generic.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
Chapter 7 Methods: A Deeper Look Visual C# 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved Generics.
Collection types Collection types.
Java Generics.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
References types and Value types With a very brief introduction to struct and enum Reference types and Value types1.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Interfaces 1. Interfaces are (parts of) contracts Interfaces are contracts between implementers and consumers Consumers: Programmers using a class implementing.
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.
Hoang Anh Viet Hà Nội University of Technology Chapter 1. Introduction to C# Programming.
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
CS 61B Data Structures and Programming Methodology July 3, 2008 David Sun.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
C# 2.0 and Future Directions Anders Hejlsberg Technical Fellow Microsoft Corporation.
Understanding Data Types and Collections Lesson 2.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Advanced C# Types Tom Roeder CS fa. From last time out parameters difference is that the callee is required to assign it before returning not the.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
Introduction to C# 2.0 An Advanced Look Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
ITF11006.NET Generics. Generics - Sample Recent Generics - Characteristics Performance Type Safety Binary Code Reuse Code Bloat Naming Guidelines.
COMP 121 Week 8: Generic Collections. Objectives To understand type variables and how they are used in generic programming To be able to implement and.
Chapter 4 Generic Vector Class. Agenda A systemic problem with Vector of Object – Several approaches at a solution – Generic structures Converting classes.
Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke.
Java Generics. It is nice if we could write a single sort method that could sort array of any type of elements: – Integer array, – String array, Solution:
Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
LINQ Language Integrated Query LINQ1. LINQ: Why and what? Problem Many data sources: Relational databases, XML, in-memory data structures, objects, etc.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
1 Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Understanding Data Types and Collections Lesson 2.
TK1924 Program Design & Problem Solving Session 2011/2012
Haskell Chapter 2.
Critical sections, locking, monitors, etc.
A tree set Our SearchTree class is essentially a set.
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Delegates/ Anders Børjesson
Lecturer: Mukhtar Mohamed Ali “Hakaale”
A tree set Our SearchTree class is essentially a set.
Object Oriented Programming in java
Generics in C# / Anders Børjesson
Generics.
Go to pollev.com/cse143.
slides created by Alyssa Harding
Chapter 11 Inheritance and Encapsulation and Polymorphism
Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc.
Interfaces, Enumerations, Boxing, and Unboxing
Presentation transcript:

Generics in C# 1

Generics List vs. non-generic ArrayList Generic List Namespace System.Collections.Generic List list = new List (); List.add(”Anders”); List.add(22); Rejected by the compiler String str = List.get(0); Example: GenericsUsing Non-generic ArrayList Namespace System.Collections ArrayList list = new ArrayList(); List.add(”Anders”); List.add(22); Objects of any type can be added to the list Object obj = list.get(0); String str = (String)obj; Type cast necessary Generics in C#2

Generics, what is it? ”Generics introduce to the.NET Framework the concept of type parameters, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code. For example, by using a generic type parameter T you can write a single class that other client code can use without incurring the cost or risk of runtime casts” Generics (C# Programming Guide) Generics in C#3

Writing a generic class Class SomeName { // T can be used inside the class as // parameter type // return type // property type } Naming convention: The type parameter is usually called ‘T’ (or TSomething) sounds like Type Example: GenericsUsing Generics in C#4

Default values Sometimes you need to get a default value for a generic type parameter Example: T find(int id) If there is no element with the specified id we want to return the default value Default values Null: References types, like String, etc. O: int, etc. False: bool Default value table Syntax default(T) T find(int id) { …. return default(T); } Generics in C#5

Generic constraints In a generic class, all you can do to a variable of the type parameter, are the operations defined in the class Object That is not much! ToString(), Equals(), etc. If you want more you must constrain the type parameter Generics constraints guarantees that the type has certain operations Example: GenericConstraints Syntax Class SomeName where T : constraint { … } Class SomeName : ISomeInterface where T : constraint { … } Different types of constraints Generics in C#6

Generic methods Methods, as well as classes + interfaces, can be generic. Generic methods are usually static Example String.Join(…) method Public static string Join (string separator, IEnumerable values) Example Enumerable.Where(…) method Generics in C#7

References and further readings MSDN Generics (C# Programming Guide) John Sharp: Microsoft Visual C# 2012 Step by Step Chapter 17 Introducing Generics, page Bart de Smet: C# 5.0 Unleashed, Sams 2013 Chapter 15 Generic Types and Methods, page Generics in C#8