Tải bản đầy đủ (.pdf) (57 trang)

Lập trình cơ sở dữ liệu C Sharp-Phần 4 pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (674.41 KB, 57 trang )

Generics
Generics
Nôi dung̣
Nôi dung̣

An toan kiêu luc biên dich (compile time type safety)̀ ̉ ́ ̣

Overloaded methods

Constraints

Generic Classes and Structs

Generic Interfaces

Generic Methods

Generic Delegates

Generic Type Conversion

Nullable Types
2
Type Safety
Type Safety
(đinh kiêu an toan)̣ ̉ ̀
(đinh kiêu an toan)̣ ̉ ̀

Cac ngôn ng .net (C#, C++, VB ) đêu la nh ng ngôn ng ́ ữ ̀ ̀ ữ ữ
đ c đinh kiêu manhượ ̣ ̉ ̣ (strong type) phai khai bao biên ro ̉ ́ ́ ̃
rang tr c khi s dung.̀ ướ ử ̣



Nh ng ư cac kiêu tâp h p (collection) lai không ́ ̉ ̣ ợ ̣ đ c ượ hô tr ̃ ợ
kiêu an tỏ àn( Type safety). Vi du kiêu ArrayList, chung cho ́ ̣ ̉ ́
phep ta l u tr tât ca cac object bên trong chung.́ ư ữ ́ ̉ ́ ́
3
Vi du vê an toan kiêú ̣ ̀ ̀ ̉
Vi du vê an toan kiêú ̣ ̀ ̀ ̉

namespace TestApp
{ class Test
{
static void Main(string[] args)
{ ArrayList list = new ArrayList();
list.Add(3);
list.Add(4);
//list.Add(5.0);
int total = 0;
foreach(int val in list)
{ total = total + val; }
Console.WriteLine( "Total is {0}", total);
}
}
}
4
Type Safety of Generics
Type Safety of Generics

Do ArrayList luôn cast moi th vê l p c s Object, nên ̣ ứ ̀ ớ ơ ở
không co cach nao đê kiêm tra kiêu th i gian biên ́ ́ ̀ ̉ ̉ ̉ ở ờ
dich. ̣


Dung Generics se loai tr viêc upcast thanh Object va ̀ ̃ ̣ ừ ̣ ̀ ̀
lam cho compiler co thê kiêm tra kiêu đ c. Nh đo co ̀ ́ ̉ ̉ ̉ ượ ờ ́ ́
thê tao collection an toan kiêu ngay th i gian biên dich̉ ̣ ̀ ̉ ờ ̣
5
5


Compile-time type safety
Compile-time type safety

Phat hiên sai kiêu d liêu th i điêm biên dich́ ̣ ̉ ữ ̣ ở ờ ̉ ̣

Vi du 1: nêu Stack đ c dung đê l u tr gia tri int ́ ̣ ́ ượ ̀ ̉ ư ữ ́ ̣
, nêu push vao 1 string thi se gây lôi (compile-́ ̀ ̀ ̃ ̃
time error).

Vi du 2: ph ng th c Sort chi co thê săp xêp cac ́ ̣ ươ ứ ̉ ́ ̉ ́ ́ ́
phân t cung kiêu d liêu. ̀ ử ̀ ̉ ữ ̣

Đê bao đam compile-time type safety, phai tao ̉ ̉ ̉ ̉ ̣
cac version kiêu khac nhau cho l p Stack cung ́ ̉ ́ ớ ̃
nh ph ng th c Sort ư ươ ứ  nhiêu ban copy cho ̀ ̉
cung 1 ma. ̀ ̃
6
Generics
Generics

Generics la 1 tinh chât m i cua C#̀ ́ ́ ớ ̉


Generic methods enable you to specify, with a single
method declaration, a set of related methods.

Generic classes enable you to specify, with a single
class declaration, a set of related classes.

Generic interfaces enable you to specify, with a single
interface declaration, a set of related interfaces.

Generics provide compile-time type safety.
7
Generic
Generic

Generic cho phep đinh kiêu an toan (type safety). ́ ̣ ̉ ̀

Cho phep tao ra môt câu truc d liêu ma không cân phai ́ ̣ ̣ ́ ́ ữ ̣ ̀ ̀ ̉
xac đinh đo la kiêu d liêu gi. ́ ̣ ́ ̀ ̉ ữ ̣ ̀

Tuy nhiên khi câu truc d liêu nay đ c s dung, trinh ́ ́ ữ ̣ ̀ ượ ử ̣ ̀
biên dich phai đam bao răng kiêu d liêu đ c s dung ̣ ̉ ̉ ̉ ̀ ̉ ữ ̣ ượ ử ̣
v i no la kiêu an toan. Generic cung t ng đ ng v i ớ ́ ̀ ̉ ̀ ̃ ươ ươ ơ
Template trong C++ tuy nhiên viêc s dung Generic ̣ ử ̣
trong .net dê dang h n nhiêu so v i Template.̃ ̀ ơ ̀ ớ
8
From Overloaded methods
From Overloaded methods

Overloaded methods th ng đ c dung th c thi cung 1 ườ ượ ̀ ự ̀
thao tac trên nhiêu loai d liêu.́ ̀ ̣ ữ ̣


Vi du: môt console application ch a 3 ph ng th c ́ ̣ ̣ ứ ươ ứ
overloaded PrintArray dung đê hiên thi cac phân t cua ̀ ̉ ̉ ̣ ́ ̀ ử ̉
mang int, mang double va mang char.̉ ̉ ̀ ̉
9
10
Overloaded methods
Overloaded methods

Trong ph ng th c PrintArray, loai phân t mang (int, ươ ứ ̣ ̀ ử ̉
double or char) xuât hiên tai 2 vi tri trong môi phu ng th c ́ ̣ ̣ ̣ ́ ̃ ơ ứ

Thay thê cac loai nay thanh 1 tên chung (generic name) ́ ́ ̣ ̀ ̀

Ca 3 ph ng th c se co chung 1 dang sau:̉ ươ ứ ̃ ́ ̣
11
Generic method
Generic method

Nêu nhiêu overloaded methods chi khac nhau ́ ̀ ̉ ́
kiêu d liêu cua cac đôi sô, nên dung ở ̉ ữ ̣ ̉ ́ ́ ́ ̀
generic method.

Môt khai bao generic method co thê đ c goi ̣ ́ ́ ̉ ượ ̣
nh ng th i điêm khac nhau v i đôi sô la ở ữ ờ ̉ ́ ớ ́ ́ ̀
nh ng loai d liêu khac nhau. ữ ̣ ữ ̣ ́

D a vao loai d liêu cua cac đôi sô đ c đ a ự ̀ ̣ ữ ̣ ̉ ́ ́ ́ ượ ư
vao generic method ma compiler điêu hanh ̀ ̀ ̀ ̀
method môt cach thich h p. ̣ ́ ́ ợ

12
Generic method declaration
Generic method declaration

Tât ca khai bao generic method phai co ́ ̉ ́ ̉ ́ type
parameter list đăt trong <> tiêp ngay sau tên method. ̣ ́
 Ex: static void PrintArray< E >( E[] inputArray )

Môi ̃ type parameter list ch a 1 hay nhiêu tham sô ứ ̀ ́
loai (̣ type parameter) cach nhau b i dâu phây.́ ở ́ ̉

Type parameter la 1 ̀ identifier thay thê cho tham ́
sô th c.́ ự

Type parameter co thê đ c dung đê khai bao loai ́ ̉ ượ ̀ ̉ ́ ̣
d liêu tra vê (return type) , loai d liêu cua tham ữ ̣ ̉ ̀ ̣ ữ ̣ ̉
sô va loai cua cac biên cuc bô. ́ ̀ ̣ ̉ ́ ́ ̣ ̣
13
Generic method’s body
Generic method’s body

Phân thân cua generic method đ c khai bao nh môt ̀ ̉ ượ ́ ư ̣
method thông th ng. ườ

Tên tham sô loai (Type parameter) đ c dung trong suôt ́ ̣ ượ ̀ ́
method phai trung loai v i tên cac loai đa đ c khai bao ̉ ̀ ̣ ớ ́ ̣ ̃ ượ ́
trong danh sach tham sô loai (type parameter).́ ́ ̣

Tham sô loai (Type parameter) co thê đ c khai bao chi 1 ́ ̣ ́ ̉ ượ ́ ̉
lân trong danh sach cac tham sô loai (type parameter list) ̀ ́ ́ ́ ̣

<>nh ng co thê xuât hiên nhiêu lân danh sach tham sô ư ́ ̉ ́ ̣ ̀ ̀ ́ ́
(parameter list) cua method đo. ̉ ́

Vi du:́ ̣
void SwapIfGreater<T>(ref T lhs, ref T rhs)
where T : System.IComparable<T>
{ ….}
14
Generic Methods
Generic Methods

Declare a generic Method:
[access modifier] <A Return Type>
GenericMethodName<A list of Generic Type
Parameter> (A list of Arguments)
{
//items
}
15
Generic Methods
Generic Methods

For example:
16
static void Swap<T>(ref T lhs, ref T rhs)
{
T temp;
temp = lhs;
lhs = rhs;
rhs = temp;

}
Generic Methods
Generic Methods

The following code example shows one way to call the
method by using int for the type argument:

We can also omit the type argument and the compiler will
infer it. The following call to Swap is equivalent to the
previous call:
17
public static void TestSwap()
{
int a = 1;
int b = 2;
Swap<int>(ref a, ref b); System.Console.WriteLine(a + " " + b);
}
Swap(ref a, ref b);
Generic Methods
Generic Methods

Within a generic class, non-generic methods can access
the class-level type parameters, as follows:

Use constraints to enable more specialized operations
on type parameters in methods.
18
class SampleClass<T>
{
void Swap(ref T lhs, ref T rhs) { }

}
Generic Methods
Generic Methods

This version of Swap<T>, now named SwapIfGreater<T>,
can only be used with type arguments that implement
IComparable<T>
19
void SwapIfGreater<T>(ref T lhs, ref T rhs)
where T : System.IComparable<T>
{
T temp;
if (lhs.CompareTo(rhs) > 0)
{
temp = lhs;
lhs = rhs;
rhs = temp;
}
}
Overloading generic method
Overloading generic method

Generic method co thê overloaded́ ̉ .

Môt class co thê cung câp 2 hay nhiêu generic methods co ̣ ́ ̉ ́ ̀ ́
cung tên nh ng tham sô khac nhau. ̀ ư ́ ́

Môt generic method co thê overloaded v i 1 method ̣ ́ ̉ ớ
generic khac hay v i cac method non-generić ớ ́
20

void DoWork() { }
void DoWork<T>() { }
void DoWork<T, U>() { }
Generic classes
Generic classes

L p generic cung câp cach th c mô ta 1 l p đôc lâp v i ớ ́ ́ ứ ̉ ớ ̣ ̣ ớ
loai d liêu (type-independent). T l p nay co thê tao cac ̣ ữ ̣ ừ ớ ̀ ́ ̉ ̣ ́
đôi t ng v i kiêu xac đinh (́ ượ ớ ̉ ́ ̣  reusability).

Khi biên dich, compiler se bao đam an toan kiêu. Đên luc ̣ ̃ ̉ ̉ ̀ ̉ ́ ́
chay thi hê thông se thay thê cac tham sô loai v i đôi sô ̣ ̀ ̣ ́ ̃ ́ ́ ́ ̣ ớ ́ ́
th c . ự
21
Generic class
Generic class

A generic class declaration is similar to a non-generic
class declaration, except that the class name is followed
by a type parameter list
class Stack< E >

Type parameter list of a generic class can have one or
more type parameters separated by commas.
22
Name Overloading in generics
Name Overloading in generics

Generic types are overloaded based upon the
number of arguments in their type argument lists.

23
public class Container {}
public class Container<T> {}
public class Container<T, R> {}
//public class Container<X, Y> {}
23
Don’t appear with
public class Container<T, R> {}
simultaneously
Name Overloading in generics
Name Overloading in generics

Each generic Container identifier
distinguishs it with another thanks to a
number of type parameters.

The name overloading rules for generic
declarations are based on the count of type
parameters rather than the names given to
their placeholders.
24
24
Distinguish declarations
Distinguish declarations

Co 2 loai khai bao: ́ ̣ ́ open type va constructed typè .

25
25
public class MyClass<T>

{
private Container<int> field1;
private Container<T> field2;
}

Declare a constructed type
Declare an open type
(or a generic type)
{
A constructed type
as well as a closed type
A constructed type
as well as an open type

×