Download presentation
Presentation is loading. Please wait.
Published byShanon McCarthy Modified over 8 years ago
1
Generics SoftUni Team Technical Trainers Software University http://softuni.bg
2
2 1.What are Generics? 2.Generic Classes, Generic Interfaces, Generic Methods 3.Type Erasure, Type Parameter Bounds 4.Wildcards Table of Contents
3
3 sli.do #6442 Questions
4
4 What are Generics?What are Generics?
5
5 Generics allow defining parameterized classes What are Generics?
6
6 Allow processing data of unknown (generic) type What are Generics? (2)
7
7 List Generics Example
8
8 List / List / List Generics Example (2)
9
9 Provide more readable code Benefits of Generics
10
10 Generics add type safety Benefits of Generics (2)
11
11 Generic ClassesGeneric Classes
12
12 Mostly used as containers Generic Classes
13
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
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
15 Conventions
16
16 Demo
17
Exercises in Class
18
18 Generic InterfacesGeneric Interfaces
19
19 Generic Interfaces add flexibility Generic Interfaces
20
20 Generic MethodsGeneric Methods
21
21 Can take generic input and return generic output Generic Methods
22
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);}
23
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
24 Demo
25
Exercises in Class
26
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
27
? ? ? ? ? ? ? ? ? OOP Advanced – Generics httpshttps://softuni.bg/courses/oop/
28
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
29
Free Trainings @ Software University Software University Foundation – softuni.orgsoftuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg softuni.bg Software University @ Facebook facebook.com/SoftwareUniversity facebook.com/SoftwareUniversity Software University @ YouTube youtube.com/SoftwareUniversity youtube.com/SoftwareUniversity Software University Forums – forum.softuni.bgforum.softuni.bg
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.