Generics SoftUni Team Technical Trainers Software University
2 1.What are Generics? 2.Generic Classes, Generic Interfaces, Generic Methods 3.Type Erasure, Type Parameter Bounds 4.Wildcards Table of Contents
3 sli.do #6442 Questions
4 What are Generics?What are Generics?
5 Generics allow defining parameterized classes What are Generics?
6 Allow processing data of unknown (generic) type What are Generics? (2)
7 List Generics Example
8 List / List / List Generics Example (2)
9 Provide more readable code Benefits of Generics
10 Generics add type safety Benefits of Generics (2)
11 Generic ClassesGeneric Classes
12 Mostly used as containers Generic Classes
13 Example T is an unknown type, parameter of the class T can be used in any method in the class class GenericList class GenericList { void Add(T element) void Add(T element){…}}
14 Example (2) T is replaced with int during the instantiation T is replaced with string during the instantiation static void Main() { GenericList intList; GenericList intList; GenericList stringList; GenericList stringList;}
15 Conventions
16 Demo
Exercises in Class
18 Generic InterfacesGeneric Interfaces
19 Generic Interfaces add flexibility Generic Interfaces
20 Generic MethodsGeneric Methods
21 Can take generic input and return generic output Generic Methods
22 Generic Method Example static List CreateList (T value, int count) { List genericList = new List (); List genericList = new List (); //add given value count times in the list //add given value count times in the list return genericList return genericList} static void Main() { List strings = CreateList("hello", 5); List strings = CreateList("hello", 5); List numbers = CreateList(10, 3); List numbers = CreateList(10, 3);}
Generic Method – Example (2) static T Min (T first, T second) where T : IComparable where T : IComparable { if(first.CompareTo(second) <=0) if(first.CompareTo(second) <=0) return first; return first; else else return second; return second;} static void Main() { int min = Min (5, 7); // 5 int min = Min (5, 7); // 5 string min = Min (“Annie”, “Ann”) // Ann string min = Min (“Annie”, “Ann”) // Ann} 23
24 Demo
Exercises in Class
26 Generics are parameterized classes Template classes for processing data of unknown (generic) type Generic methods can accept generic parameter types Generic interfaces allow defining flexible contracts Summary
? ? ? ? ? ? ? ? ? OOP Advanced – Generics httpshttps://softuni.bg/courses/oop/
License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" licenseCreative Commons Attribution- NonCommercial-ShareAlike 4.0 International 28 Attribution: this work may contain portions from "Fundamentals of Computer Programming with C#" book by Svetlin Nakov & Co. under CC-BY-SA licenseFundamentals of Computer Programming with C#CC-BY-SA "OOP" course by Telerik Academy under CC-BY-NC-SA licenseOOPCC-BY-NC-SA
Free Software University Software University Foundation – softuni.orgsoftuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg softuni.bg Software Facebook facebook.com/SoftwareUniversity facebook.com/SoftwareUniversity Software YouTube youtube.com/SoftwareUniversity youtube.com/SoftwareUniversity Software University Forums – forum.softuni.bgforum.softuni.bg