Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240...
-
Upload
denis-fletcher -
Category
Documents
-
view
218 -
download
1
description
Transcript of Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240...
![Page 1: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/1.jpg)
Lecture 10: Generics & Basic Desktop Programming
Svetla BoytchevaAUBG, Spring 2014
1
COS 240 Object-Oriented Languages
![Page 2: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/2.jpg)
Course Materials – Source Codes http://www.wrox.com/WileyCDA/WroxTitle/productCd-1118314417.html
2
![Page 3: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/3.jpg)
Reminder Next Class – Midterm Exam
3
![Page 4: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/4.jpg)
Midterm Exam Structure Practical task - 20 % (computer)
• 1 task• Basic desktop programming• Chapters 15, 16
Test– 80% (written on paper, computer usage is not allowed) • 10 tasks• Chapters 8-12 (approx. 2 tasks per chapter)
4
![Page 5: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/5.jpg)
More reading:
Karli Watson et all, Beginning Visual C# 2012 Programming, Wrox, Jonh Wiley & Sons, 2013• CHAPTER 12: Generics.• CHAPTER 15: Basic Desktop Programming
5
![Page 6: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/6.jpg)
WHAT YOU WILL LEARN What generics are How to use some of the generic classes provided by
the .NET Framework How to define your own generics How variance works with generics Basic Desktop Programming
6
![Page 7: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/7.jpg)
GENERICS Generics enable you to create flexible types that
process objects of one or more specific types. These types are determined when you instantiate or
otherwise use the generic.
7
![Page 8: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/8.jpg)
Nullable Types One of the ways in which value types (which
include most of the basic types such as int and double as well as all structs) differ from reference types (string and any class) is that they must contain a value.
They can exist in an unassigned state, just after they are declared and before a value is assigned, but you can’t make use of the value type in that state in any way. Conversely, reference types can be null.
8
![Page 9: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/9.jpg)
9
![Page 10: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/10.jpg)
Nullable Types Note that nullable types are so useful that they have
resulted in a modification of C# syntax. Rather than use the syntax shown previously to
declare a nullable type variable, you can instead use the following:
int? nullableInt; int? is simply a shorthand for System.Nullable<int>
but is much more readable.
10
![Page 11: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/11.jpg)
Operators and Nullable Types
11
![Page 12: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/12.jpg)
Resolving Bool? Comparisons with null Value
12
![Page 13: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/13.jpg)
The ?? Operator (null coalescing operator) It is a binary operator that enables you to supply an alternative
value to use for expressions that might evaluate to null. The operator evaluates to its first operand if the first operand is
not null, or to its second operator if the first operand is null. Functionally, the following two expressions are equivalent:
op1 ?? op2op1 == null ? op2 : op1
In this code, op1 can be any nullable expression, including a reference type and, importantly, a nullable type.
Example:int? op1 = null;int result = op1 * 2 ?? 5;
13
![Page 14: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/14.jpg)
The System.Collections.Generic Namespace
14
![Page 15: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/15.jpg)
List<T>
15
![Page 16: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/16.jpg)
List<T> Creating a collection of type T objects
requires the following code:List<T> myCollection = new List<T>();
List<T> also has an Item property, enabling array-like access:T itemAtIndex2 = myCollectionOfT[2];
16
![Page 17: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/17.jpg)
Sorting and Searching Generic Lists
17
![Page 18: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/18.jpg)
Functions - Delegates A delegate is a type that enables you to store references to functions. Although this sounds quite involved, the mechanism is surprisingly simple. The most important purpose of delegates is their usage in events and event handling Delegates are declared much like functions, but with no function body and using the
delegate keyword. The delegate declaration specifies a return type and parameter list. After defining a delegate, you can declare a variable with the type of that delegate. You can then initialize the variable as a reference to any function that has the same
return type and parameter list as that delegate. Once you have done this, you can call that function by using the delegate variable as if it were a function.
When you have a variable that refers to a function, you can also perform other operations that would be otherwise impossible. For example, you can pass a delegate variable to a function as a parameter, and then that function can use the delegate to call whatever function it refers to, without knowing which function will be called until runtime.
18
![Page 19: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/19.jpg)
19
![Page 20: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/20.jpg)
Example
20
![Page 21: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/21.jpg)
21
![Page 22: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/22.jpg)
Dictionary<K, V>
22
![Page 23: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/23.jpg)
Dictionary<K, V>
23
![Page 24: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/24.jpg)
DEFINING GENERIC TYPES You can define the following:
• Generic classes• Generic interfaces• Generic methods• Generic delegates
You’ll also look at the following more advanced techniques for dealing with the issues that come up when defining generic types:• The default keyword• Constraining types• Inheriting from generic classes
24
![Page 25: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/25.jpg)
Defining Generic Classes
25
![Page 26: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/26.jpg)
26
T1 is ADT (class)T1 is basic data type
![Page 27: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/27.jpg)
The default Keyword
27
![Page 28: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/28.jpg)
Constraining Types The types you have used with generic classes until
now are known as unbounded types because no restrictions are placed on what they can be.
By constraining types, it is possible to restrict the types that can be used to instantiate a generic class.
For example, it’s possible to restrict a type to one that inherits from a certain type.
28
![Page 29: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/29.jpg)
29
![Page 30: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/30.jpg)
30
![Page 31: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/31.jpg)
31
![Page 32: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/32.jpg)
Example
32
![Page 33: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/33.jpg)
Inheriting from Generic Classes
33
![Page 34: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/34.jpg)
Generic Operators
34
![Page 35: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/35.jpg)
Generic Structs
35
![Page 36: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/36.jpg)
36
![Page 37: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/37.jpg)
Defining Generic Interfaces
37
![Page 38: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/38.jpg)
Defining Generic Methods A generic method is one in which the return and/or parameter types
are determined by a generic type parameter or parameters:public T GetDefault<T>(){ return default(T);}
This trivial example uses the default keyword you looked at earlier in the chapter to return a default value for a type T. This method is called as follows:
int myDefaultInt = GetDefault<int>(); The type parameter T is provided at the time the method is called.
38
![Page 39: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/39.jpg)
Defining Generic Delegates
39
![Page 40: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/40.jpg)
VARIANCE Variance is the collective term for covariance and
contravariance, two concepts that were introduced in .NET 4. In fact, they have been around longer than that (they were
available in .NET 2.0), but until .NET 4 it was very difficult to implement them, as this required custom compilation procedures.
40
![Page 41: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/41.jpg)
41
![Page 42: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/42.jpg)
Covariance
42
![Page 43: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/43.jpg)
Contravariance
43
![Page 44: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/44.jpg)
Basic Desktop Programming Label Button TextBox, MaskedTextBox, RichTextBox NumericUpDown RadioButton CheckBox GroupBox MessageBox MonthCalendar, DateTimePicker
44
![Page 45: Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring 2014 1 COS 240 Object-Oriented Languages.](https://reader035.fdocuments.us/reader035/viewer/2022070605/5a4d1af57f8b9ab0599811c0/html5/thumbnails/45.jpg)
45
Questions?Questions?