Generics in C# / Anders Børjesson

Slides:



Advertisements
Similar presentations
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Advertisements

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.
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.
Creating Generic Classes. Introduction Java Generics were added to allow for type- safe collections and eliminate the need for burdensome, code-cluttering.
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.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
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.
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.
C#.Net Development Version 1.0. Overview Nullable Datatype Description ? HasValue Lifted Conversions null coalescing operator ?? Partial Classes Copyright.
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.
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.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
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.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
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.
Generics in C# 1. Generics List vs. non-generic ArrayList Generic List Namespace System.Collections.Generic List list = new List (); List.add(”Anders”);
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.
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.
Operator Overloading.
TK1924 Program Design & Problem Solving Session 2011/2012
Lecture 10 Collections Richard Gesick.
Introduction to LINQ and Generic Collections
Interfaces and Generics
Haskell Chapter 2.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Critical sections, locking, monitors, etc.
A tree set Our SearchTree class is essentially a set.
Syntax, semantics, and pragmatics
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
Generics, Lambdas, Reflections
Continuing Chapter 11 Inheritance and Polymorphism
Advanced Programming in Java
Generics.
Advanced Programming in Java
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Collection types/Anders Børjesson
Building Java Programs
Building Java Programs
Polymorphism.
Name: Rubaisha Rajpoot
A tree set Our SearchTree class is essentially a set.
Lecture 2: Implementing ArrayIntList reading:
Object Oriented Programming in java
Generics.
Interfaces and Generics
Java Inheritance.
Tonga Institute of Higher Education
Generics, Lambdas, Reflections
Go to pollev.com/cse143.
Chapter 19 Generics Jung Soo (Sue) Lim Cal State LA.
CIS 199 Final Review.
Class.
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# / Anders Børjesson

Generics List<T> vs. non-generic ArrayList Generic List<T> Namespace System.Collections.Generic Non-generic ArrayList Namespace System.Collections List<String> list = new List<String>(); List.add(”Anders”); List.add(22); Rejected by the compiler String str = List.get(0); Example: GenericsUsing 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# / Anders Børjesson

Generics in C# / Anders Børjesson 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) http://msdn.microsoft.com/en-us/library/512aeb7t.aspx Generics in C# / Anders Børjesson

Writing a generic class Class SomeName<T> { // 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 Generics in C# / Anders Børjesson

Generics in C# / Anders Børjesson 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 http://msdn.microsoft.com/en-us/library/83fhsxwc.aspx Syntax default(T) T find(int id) { …. return default(T); } Generics in C# / Anders Børjesson

Generics in C# / Anders Børjesson 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<T> where T : constraint { … } Class SomeName<T> : ISomeInterface where T : constraint { … } Different types of constraints http://msdn.microsoft.com/en-us/library/d5x73970.aspx Generics in C# / Anders Børjesson

Generics in C# / Anders Børjesson Generic methods Methods, as well as classes + interfaces, can be generic. Generic methods are usually static Example String.Join(…) method Public static string Join<T>(string separator, IEnumerable<T> values) Example Enumerable.Where(…) method http://msdn.microsoft.com/en-us/library/bb534803(v=vs.110).aspx Generics in C# / Anders Børjesson

References and further readings MSDN Generics (C# Programming Guide) http://msdn.microsoft.com/en-us/library/512aeb7t.aspx John Sharp: Microsoft Visual C# 2012 Step by Step Chapter 17 Introducing Generics, page 389-418 Bart de Smet: C# 5.0 Unleashed, Sams 2013 Chapter 15 Generic Types and Methods, page 701-754 Generics in C# / Anders Børjesson