You are on page 1of 47

1

.NET and C# Basics

part 4

Generic Templates
Collections

www.luxoft-training.com
2
What we will learn
 Generic templates
 Collections
 Embedded query language

www.luxoft-training.com
3
Generic templates
 Why?
 Definition
 Generic type parameters
 Generic templates and inheritance
 Restrictions
 Practice

www.luxoft-training.com
4
Why?
 One implementation is used with different types

No need to define 2 functions

www.luxoft-training.com
5
Definition

Generic templates introduce the concept of generic type


parameters in .NET

 By using these templates you can create classes and methods


with types whose specification is suspended until declaring and
creating instances in the client code.

www.luxoft-training.com
6
Syntax
 Syntax:
 Generic type:
public class List<T> { … }

 Generic method:
public bool IsEqual<T>(T val1, T val2) { … }

 Usage:
List<String> listOfStrings = new List<String>();

Console.WriteLine(IsEqual<String>(“First”, “Second”);
www.luxoft-training.com
7
Example. MyStack Collection
Example. Create your own collection:
Instead of a concrete type - parameter T,

Specify a concrete type


for which a class is created

www.luxoft-training.com
8
One universal definition

In this case, you create 3 separate type-safe


objects using the same definition of class

www.luxoft-training.com
9
Rules of naming type parameters

Assign descriptive names to generic type parameters

www.luxoft-training.com
10
Generic types and inheritance
A generic type can be derived from non-generic types
A descendant has the same type as the base type
A base type parameters is different from a descendant type
parameter
A non-generic type can be inherited from a generic type

www.luxoft-training.com
11
Generic types and inheritance
A generic type can be derived from other non-generic types

www.luxoft-training.com
12
Generic types and inheritance
A descendant has the same type as the base type

www.luxoft-training.com
13
Generic types and inheritance
A base type parameters is different from a descendant type
parameter

www.luxoft-training.com
14
Generic types and inheritance
A non-generic type can be inherited from a generic type

www.luxoft-training.com
15
Verification and constraints
 The compiler analyzes the code of generic type to ensure its
compatibility with any types.
=> Only methods of the class object are accessible

 This constraint allows to shorten the list of types that can be


passed in a generic argument.
Example:

www.luxoft-training.com
16
Other constraints
 You can constrain multiple parameters simultaneously.

- The U parameter type must be a class

- The T parameter type must have the base


class “Base” or its derivative
- It must have a public constructor
without parameters

www.luxoft-training.com
17
Collections
 Definition
 Types of collections
 Benefits of using collections
 Data structures used in collections
 Algorithmic complexity of operations
 Benchmark test
 Practice

www.luxoft-training.com
18
Collections
 Unlike arrays, a group of objects in a collection can dynamically
increase and decrease according to the application needs
 With some collections you can assign a key to any object that
needs to be included in the collection in order to quickly get the
object by the key.
 Namespaces
 System.Collections
 System.Collections.Generic
www.luxoft-training.com
19
Frequently used collections
Dictionary<TKey,TValue> - a key-value pair. Key is a unique value.
List<T> - a list of objects. It’s a dynamically expandable array. Provides a set of
methods for searching and sorting.
HashSet<T> - a set of unique values (elements)
LinkedList<T> - a linked list (each item is linked with the preceding and
subsequent ones)

www.luxoft-training.com
20
Collections
It is advisable to always use generic collections as you get certain
advantages in terms of:
 Security of types
 Performance
 Available methods

www.luxoft-training.com
21
Data structures
Most collections provide a standard set of methods:
 Searching
 Inserting an element in the beginning and end of the collection
 Deleting any arbitrary element
 Sorting, etc.

www.luxoft-training.com
22
Stack
Provides a collection of
objects that is served
according to the principle ‘d’ ‘d’
LIFO (Last In First Out).

‘f’ ‘f’
‘b’ ‘b’
‘c’ ‘c’
‘a’ ‘a’
www.luxoft-training.com
23

Demonstrating the work


with Stack

DEMO

www.luxoft-training.com
24
Queue
Provides a collection of objects based on the principle First In First
Out (FIFO). Example of such collection - Queue<T>

‘f’

‘a’ ‘f’ ‘d’ ‘c’


‘e’

www.luxoft-training.com
25

Demonstrating the work


with Queue

DEMO

www.luxoft-training.com
26
Linked list
Provides a collection of objects where each object stores links to the
previous and next object in the collection.
Therefore the elements can be located in the memory
inconsequently. ‘f’

‘a’

‘d’

www.luxoft-training.com
27

Demonstrating the
Linked List

DEMO

www.luxoft-training.com
28
Binary tree
65
A data structure where
each element can contain 2
elements linked by certain rules 13 81
and can itself be such a linked
element for another element.
Example of collection: 5 67 82
SortedSet<T>

70
www.luxoft-training.com
29

Demonstrating Sorted Set

DEMO

www.luxoft-training.com
30
Hash Table
Represents a collection of “key-value” pairs which are ordered by the key
hash code: Example of collection: HashSet<T>
Dictionary <Tkey, TValue>

00 123-40-50
Paul Smith 01 523-30-50
02 123-40-50
Mary Wong 03 333-41-40

55 623-40-50
www.luxoft-training.com
31

Demonstrating Dictionary

DEMO

www.luxoft-training.com
32
Practice
Use the code of our zoo example.
Each animal has a name.
There are open-air cages in the zoo ; they are the same for all kinds of
animals with which the manager works.
- Every open-air cage has a certain capacity
- You can add any animal to or remove it from any open-air cage
- You can display the names of all the animals that are in the open-
air cages (base class constraint)

www.luxoft-training.com
33
LINQ
 Definition
 Query structure
 Delayed execution
 Types of returned data from a query

www.luxoft-training.com
34
LINQ
Language-Integrated Query (LINQ) is a simpe and easy query
language.

Irrespective of a source type (database, XML file, Inumerable


collection), it provides the same approach to data retrieval.

www.luxoft-training.com
35
Query structure
LINQ consists of 3 main parts:

Determining the data source

Forming a query

Executing the query

www.luxoft-training.com
36
Query structure
Determining the data source

Forming a query

Executing the query

www.luxoft-training.com
37
LINQ

www.luxoft-training.com
38
LINQ
 The data source should implement the interface IEnumerable
<T> or IQueriable<T>

 The query shows what is returned and how the returned data
should be sorted, grouped, and formed.

www.luxoft-training.com
39
Deferred execution
(from the previous example)
The actual execution of the query is deferred until the query variable is
iterated over in a foreach!

To trigger immediate execution, you can, for example, call the method
ToList() or ToArray().

www.luxoft-training.com
40
Types of returned data from a query
 The data source contains a sequence of strings, and the query
output also represents a sequence of strings.

www.luxoft-training.com
41
Types of returned data from a query
 The query receives a sequence of objects Customer as input data,
and selects only the property Name as output.

www.luxoft-training.com
42
Types of returned data from a query
 The query receives a sequence of objects Customer as input data,
and selects only the property Name as output.

www.luxoft-training.com
43
Types of returned data from a query
 The query receives a sequence of objects Customer as input data,
and selects only the property Name as output.

www.luxoft-training.com
44

Example of using LINQ

DEMO

www.luxoft-training.com
45
Practice
The structure Country contains the name, population (million)
From the list of countries:
 Select countries with population over 50 million
 Select an array containing the names of all countries in the list, in
alphabetical order

Germany - 80
Brazil - 150
Spain - 60
Indonesia - 160
Slovenia - 6

www.luxoft-training.com
46

Summary
 Generalizations
 Collections
 Data structures
 Embedded query language

www.luxoft-training.com
47

Questions

www.luxoft-training.com

You might also like