Программирование

Содержание

Слайд 2

19.1. Стек для чисел

class MyFloatStack // Stack for floats
{
int StackPointer =

19.1. Стек для чисел class MyFloatStack // Stack for floats { int
0;
float [] StackArray; // Array of float
↑ float
float ↓
public void Push( float x ) // Input type: float
{... }
float

public float Pop() // Return type: float
{...}
...
}

Слайд 3

19-2. Generic types are templates for types

19-2. Generic types are templates for types

Слайд 4

19-3. Generics and user-defined types

19-3. Generics and user-defined types

Слайд 5

19-4. Continuing with the Stack Example

class MyStack
{
int StackPointer = 0;
T

19-4. Continuing with the Stack Example class MyStack { int StackPointer =
[] StackArray;
public void Push(T x ) {...}
public T Pop() {...}
...
}

Слайд 6

19-5. Creating instances from a generic type

19-5. Creating instances from a generic type

Слайд 7

19-6. Declaring a Generic Class

Type parameters

class SomeClass < T1, T2

19-6. Declaring a Generic Class Type parameters ↓ class SomeClass { Normally,
>
{ Normally, types would be used in these positions.
↓ ↓
public T1 SomeVar = new T1();
public T2 OtherVar = new T2();
} ↑ ↑
Normally, types would be used in these positions.

Слайд 8

19-7. Creating a Constructed Type

19-7. Creating a Constructed Type

Слайд 9

19-8. Type parameters versus type arguments

19-8. Type parameters versus type arguments

Слайд 10

19-9. Creating Variables and Instances

MyNonGenClass myNGC = new MyNonGenClass ();
Constructed class Constructed

19-9. Creating Variables and Instances MyNonGenClass myNGC = new MyNonGenClass (); Constructed
class
↓ ↓
SomeClass mySc1 = new SomeClass();
var mySc2 = new SomeClass();

Слайд 12

19-11. Two constructed classes created from a generic class

19-11. Two constructed classes created from a generic class

Слайд 13

19-12. Constraints on Type Parameters

class Simple
{
static public bool LessThan(T i1, T

19-12. Constraints on Type Parameters class Simple { static public bool LessThan(T
i2)
{
return i1 < i2; // Error
}
...
}

Слайд 14

19-13. Where Clauses

Type parameter Constraint list
↓ ↓
where TypeParam : constraint, constraint, ...

19-13. Where Clauses Type parameter Constraint list ↓ ↓ where TypeParam :

Colon

nbounded With constraints
↓ ↓ No separators
class MyClass < T1, T2, T3 > ↓
where T2: Customer // Constraint for T2
where T3: Icomparable // Constraint for T3
{ ↑
... No separators
}

Слайд 15

19-14. Constraint Types and Order

19-14. Constraint Types and Order

Слайд 16

19-15. If a type parameter has multiple constraints, they must be in

19-15. If a type parameter has multiple constraints, they must be in this order.
this order.

Слайд 17

19-16. Правила задания ограничений

class SortedList
where S: IComparable { ... }
class LinkedList
where M

19-16. Правила задания ограничений class SortedList where S: IComparable { ... }
: IComparable
where N : ICloneable { ... }
class MyDictionary
where KeyType : IEnumerable,
new() { ... }

Слайд 18

19-17. Generic Structs

struct PieceOfData // Generic struct
{
public PieceOfData(T value) { _Data =

19-17. Generic Structs struct PieceOfData // Generic struct { public PieceOfData(T value)
value; }
private T _Data;
public T Data
{
get { return _Data; }
set { _Data = value; }
}
}

Слайд 19

19-18. Generic Structs

class Program {
static void Main() Constructed type
{ ↓
var intData =

19-18. Generic Structs class Program { static void Main() Constructed type {
new PieceOfData(10);
var stringData = new PieceOfData("Hi there.");

Constructed type
Console.WriteLine("intData = {0}", intData.Data);
Console.WriteLine("stringData = {0}", stringData.Data);
}
}

Слайд 20

19-19. Generic Interfaces

Type parameter

interface IMyIfc // Generic interface
{ T ReturnIt(T inValue); }
Type

19-19. Generic Interfaces Type parameter ↓ interface IMyIfc // Generic interface {
parameter Generic interface
↓ ↓
class Simple : IMyIfc // Generic class
{
public S ReturnIt(S inValue) // Implement interface
{ return inValue; }
}

Слайд 21

19-20. Generic Interfaces

class Program{
static void Main(){
var trivInt = new Simple();
var trivString

19-20. Generic Interfaces class Program{ static void Main(){ var trivInt = new
= new Simple();
Console.WriteLine("{0}", trivInt.ReturnIt(5));
Console.WriteLine("{0}", trivString.ReturnIt("Hi there."));
}
}

This code produces the following output:
5
Hi there.

Слайд 22

19-21. Using Generic Interfaces

interface IMyIfc // Generic interface
{ T ReturnIt(T inValue); }
Two different

19-21. Using Generic Interfaces interface IMyIfc // Generic interface { T ReturnIt(T
interfaces from the same generic interface
↓ ↓
class Simple : IMyIfc, IMyIfc // Non-generic class
{
public int ReturnIt(int inValue) / / Implement int interface
{ return inValue; }
public string ReturnIt(string inValue) // Implement string interface
{ return inValue; }
}

Слайд 23

19-22. Using Generic Interfaces

class Program {
static void Main() {
Simple trivInt = new Simple();
Simple

19-22. Using Generic Interfaces class Program { static void Main() { Simple
trivString = new Simple();
Console.WriteLine("{0}", trivInt.ReturnIt(5));
Console.WriteLine("{0}", trivString.ReturnIt("Hi there."));
}
}

This code produces the following output:
5
Hi there.

Слайд 24

19-23. Duplicate interface

interface IMyIfc { T ReturnIt(T inValue); }
class Simple : IMyIfc, IMyIfc { //

19-23. Duplicate interface interface IMyIfc { T ReturnIt(T inValue); } class Simple
Error!
public int ReturnIt(int inValue) // Implement first interface.
{ return inValue; }
public S ReturnIt(S inValue) // Implement second interface,
{ return inValue; } // but if it's int, it would be
// the same as the one above.
}

Слайд 25

19-24. Generic Delegates

 
Type parameters

delegate R MyDelegate( T value );

19-24. Generic Delegates Type parameters ↓ delegate R MyDelegate ( T value

Return type Delegate formal parameter

Слайд 26

19-25. Generic delegate and matched delegate methods

delegate void MyDelegate(T value); // Generic

19-25. Generic delegate and matched delegate methods delegate void MyDelegate (T value);
delegate
class Simple {
static public void PrintString(string s)
{ Console.WriteLine(s); }
static public void PrintUpperString(string s)
{ Console.WriteLine("{0}", s.ToUpper()); }
}

Слайд 27

19-26. Generic Delegate

class Program {
static void Main( ) {
var myDel = // Create

19-26. Generic Delegate class Program { static void Main( ) { var
inst of delegate
new MyDelegate(Simple.PrintString);
myDel += Simple.PrintUpperString; // Add a method.
myDel("Hi There."); // Call delegate
}
}

This code produces the following output:
Hi There.
HI THERE.

Слайд 28

19-27. Generic Delegate

public delegate TR Func(T1 p1, T2 p2);

19-27. Generic Delegate public delegate TR Func (T1 p1, T2 p2); class

class Simple {
static public string PrintString(int p1, int p2)
{ int total = p1 + p2; return total.ToString(); }
}
class Program {
static void Main() {
var myDel = new Func(Simple.PrintString);
Console.WriteLine("Total: {0}", myDel(15, 13));
}
}

Слайд 29

19-28. Generic Methods

19-28. Generic Methods

Слайд 30

19-29. Declaring a Generic Method

Type parameter list Constraint clauses
↓ ↓
public

19-29. Declaring a Generic Method Type parameter list Constraint clauses ↓ ↓
void PrintData ( S p ) where S: Person
{ ↑
…. Method parameter list
}

Слайд 31

19-30. Invoking a Generic Method

Объявление обобщенного метода:
void MyMethod() {
T1 someVar;
T2 otherVar;

}
Type arguments

19-30. Invoking a Generic Method Объявление обобщенного метода: void MyMethod () {


MyMethod();
MyMethod();

Слайд 32

19-31. A generic method with two instantiations

19-31. A generic method with two instantiations

Слайд 33

19-32. Inferring Types

public void MyMethod (T myVal) { ... }

19-32. Inferring Types public void MyMethod (T myVal) { ... } ↑
↑ ↑
Both are of type T
int MyInt = 5;
MyMethod (MyInt);
↑ ↑
Both are ints
MyMethod(MyInt);

Слайд 34

19-33. Example of a Generic Method

class Simple { // Non-generic class
static public

19-33. Example of a Generic Method class Simple { // Non-generic class
void ReverseAndPrint(T[] arr) // Generic method
{
Array.Reverse(arr);
foreach (T item in arr) // Use type argument T.
Console.Write("{0}, ", item.ToString());
Console.WriteLine("");
}
}

Слайд 35

19-34. Example of a Generic Method

class Program {
static void Main() {
var intArray =

19-34. Example of a Generic Method class Program { static void Main()
new int[] { 3, 5, 7, 9, 11 };
var stringArray = new string[] { "first", "second", "third" };
var doubleArray = new double[] { 3.567, 7.891, 2.345 };
Simple.ReverseAndPrint(intArray); // Invoke method
Simple.ReverseAndPrint(intArray); // Infer type and invoke
Simple.ReverseAndPrint(stringArray);
Simple.ReverseAndPrint(stringArray);
Simple.ReverseAndPrint(doubleArray);
Simple.ReverseAndPrint(doubleArray);
}
}

Слайд 36

19-35. Extension Methods with Generic Classes

static class ExtendHolder {
public static void Print(this

19-35. Extension Methods with Generic Classes static class ExtendHolder { public static
Holder h) {
T[] vals = h.GetValues();
Console.WriteLine("{0},\t{1},\t{2}", vals[0], vals[1], vals[2]); }
}
class Holder {
T[] Vals = new T[3];
public Holder(T v0, T v1, T v2)
{ Vals[0] = v0; Vals[1] = v1; Vals[2] = v2; }
public T[] GetValues() { return Vals; }
}

Слайд 37

19-36. Example

class Program {
static void Main(string[] args) {
var intHolder = new Holder(3, 5,

19-36. Example class Program { static void Main(string[] args) { var intHolder
7);
var stringHolder = new Holder("a1", "b2", "c3");
intHolder.Print();
stringHolder.Print();
}
}
This code produces the following output:
3, 5, 7
a1, b2, c3
Имя файла: Программирование-.pptx
Количество просмотров: 102
Количество скачиваний: 0