Professional Documents
Culture Documents
Chuong1 Ontapcoso
Chuong1 Ontapcoso
CHNG 1:
N TP CC LP C S
A D M I N T U T H O C L A PT R I N H. C O M
NI DUNG
2
t in
3/26/2010
1.1 System.object
Phng thc
Mc ch
Truy xut
string ToString()
public virtua l
int GetHashCode()
public virtua l
public virtua l
public static
bool
ReferenceEquals(obje ct
objA, object objB )
public static
Type GetType( )
public
object MemberwiseClone()
void Finalize()
To mt bn sao t i tng
protected
protected virtual
Mc ch
Phng thc
Hm hy ( Destructor)
Compare
CompareOrdinal
Format
IndexOf
1.2 X Vltrchui
xut h in (System.string)
u tin ca 1 chui con hoc k t trong
chui
Phng thc
IndexOfAny
string ToString()
LastIndexOf
V tr Truy
xut hin
1 hoc
xut u tin ca bt kMc
ch1 tp k t
trong chui
Tr
v
1
chui
i din cho i
public virtua l
tng
Ging indexof , nh ng tm ln
in cui
Choxut
phph nhng
icng
tng cung
int GetHashCode()
specifier
type
bool Equals(object obj)
public virtua l
1 i
tngkhc
currency
1.23
-12,345.00
oolc Equals(object
objA,{0:c}
public static
So snh bng 2 i tng
object objB)
decim al
{0:d}
System.Format Exception
-12345
boold
So snh cc
tham chiu i tng
(whol e number)
public static
ReferenceEquals(obje ct
xem chng c ch n cng i
exponentobjB
/ scienti
1.234500e+0 00
objAe , object
) fi c {0:e}
tng -1.234500e+0 04
f
fixed point
Type GetType( )
g
general
{0: f}
{0:g}
Tr v 1-12345.00
i tng dn xut t
System.Type m a ra chi tit
-12345
kiu d liu
1.23
public
1.2345
n
number
{0:n}
1.23
-12,345.00
object MemberwiseClone()
protected
To mt bn
sao t i tng
r
round trippabl e
{0:r}
1.23
System.Format Exception
void Finalize()
protected virtual
Hm hy ( Destructor)
x
hexadecimal
{0:x4}
System.Format Exception
ffffc fc7
Phng thc
K t
Compare
\w
Mc ch
ngha
So snh
ni
cadu
2 chui
K t word (gm
ch ci
vdung
ch s,
gch di _ ) tngng [a-zAZ_0-9]
CompareOrdinal
3/26/2010
Mc ch
Phng thc
LastIndexOfAny
PadLeft
PadRigth
Replace
Split
Substring
ToLower
ToUpper
Trim
V d:
Compare
3/26/2010
V d: Format
Phng thc
Mc ch
Truy xut
string ToString()
public virtua l
int GetHashCode()
public virtua l
public virtua l
public static
bool
ReferenceEquals(obje ct
objA , object objB )
public static
Type GetType( )
public
object MemberwiseClone()
void Finalize()
To mt bn sao t i tng
protected
protected virtual
Hm hy ( Destructor)
Mc ch
Phng thc
Compare
CompareOrdinal
1.2 X l
chui (System.string)
nh dng mt chui cha 1 gi t r khc v ch nh
Format
cch mi g i tr
8 nn c nh dng.
LastIndexOf
string chuoi0 1
exponent / scienti fi c
{0:e}
1.234500e+000
-1.234 500e+0 04
fixed point
{0: f}
1.23
-12345.00
general
{0:g}
1.2345
-12345
number
{0:n}
1.23
-12,34 5.00
round trippabl e
{0:r}
1.23
System.Format Exception
hexadecimal
{0:x4}
System.Format Exception
ffffc fc7
3/26/2010
Console.WriteLine(str.PadLeft(15, pad));
Console.WriteLine(str.PadLeft(2, pad));
Console.WriteLine(str.PadRight(15, pad));
Console.WriteLine(str.PadRight(2, pad));
Ngn ng biu thc chnh quy l ngn ng c thit k c bit cho vic
x l chui. C 2 c tnh :
tp m escape cho vic xc nh kiu ca cc k t . V d vic dng k t *
trnh by chui con bt k trong biu thc DOS , biu thc chnh quy dng nhiu
chui nh th trnh by cc mc nh l 'bt k 1 k t' ,'1 t ngt ','1 k t tu
chn',...
1 h thng cho vic nhm nhng phn chui con, v tr v kt qu trong sut thao
tc tm.
1
Dng biu thc chnh quy c th thao tc cp cao v phc tp trn chui:
nh tt c cc t lp li trong chui , chuyn "The computer books books"
"The computer books"
Chuyn tt c cc t theo title case, nh l chuyn "this is a Title" "This Is A
Title".
Chuyn nhng t di hn 3 k t thnh title case , v d chuyn "thi s is a Title
"This is a Title
Xc
Bo m cc cu c vit hoa
Phn cch nhng phn t ca URL
3/26/2010
K t
^
ngha
Bt u ca chui nhp
V d
^B
X$
Bt k k t no ngoi tr k t
xung dng(\n)
K t trc c th c lp li 0
hoc nhiu ln
K t trc c th c lp li 1
hoc nhiu ln
K t trc c th c lp li 0
hoc 1 ln
Bt k k t khong trng
*
+
?
\s
.ation
ra*t
ra+t
ra?t
\sa
Mt s th hin
B, nh ng ch nu k t
u tin trong chui
X, nhng ch nu k t
cui cng trong chui
isation, izat ion
rt, rat, raat, raaat, and so
on
rat, raat , raaat and so on,
(but not rt)
rt and rat only
[space]a, \ta, \na (\t and
\n c ngha ging nh
trong C#)
3/26/2010
Phng thc
Mc ch
Truy xut
string ToString()
public virtua l
int GetHashCode()
public virtua l
public virtua l
public static
bool
ReferenceEquals(obje ct
objA , object objB )
public static
Type GetType( )
public
object MemberwiseClone()
void Finalize()
To mt bn sao t i tng
protected
protected virtual
Hm hy ( Destructor)
Mc ch
Phng thc
Compare
CompareOrdinal
Format
14
K t
IndexOf
|
ngha
V tr xut h in u
tin ca 1 chui con hoc k t trong
chui
K t ngn cch so trngtngng vi php or (lu ci ny nu
mun kt hp
iu kin)
Vnhiu
tr xut
hin u tin ca bt k 1 hoc 1 tp k t
Khp vi 1 k
t
nm
trong nhm l a hay b hay c.
trong chui
So trng vi 1 k t nm trong phm vi a-z, dng du - lm du ngn
cch.
LastIndexOf
Ging indexof , nh ng tm ln xut h in cui cng
S khng so trng vi 1 k t nm trong nhm, v d khng so trngvi a
[^abc]
hay b hay c.
output
output
Xc nh 1 group
(biu thc con) xem
nh n l mt yu t n
l trong
()
specifier
type
format
(double 1.2345)
(int -1 2345)
pattern .
V d: ((a(b))c)
s khp vi
b, ab, abc.
c
currency
{0:c}
1.23
-12,3 45.00
n l con s, Khp ng vi n k t ng trc n .
{n}
decim al V d A{2}: khp ng vi 2 ch A.
d
{0:d}
System.Format Exception
-12345
Khp ng vi n k t tr ln ng trc n ,
{n, } (whol e number)
A{2,} khp vi AA, AAA ...
e
exponent / scienti fi c {0:e}
1.234500e+000
-1.234 500e+0 04
Khp ng vi t m->n k t ng trc n,
{m,n }
A{2,4}
khp
vi
AA,AAA,AAAA.
f
fixed point
{0: f}
1.23
-12345.00
IndexOfAny
[abc]
[a-z]
general
{0:g}
1.2345
-12345
number
{0:n}
1.23
-12,34 5.00
round trippabl e
{0:r}
1.23
System.Format Exception
hexadecimal
{0:x4}
System.Format Exception
K t
ngha
ffffc fc7
3/26/2010
V d:
nh ngha biu thc chnh quy so khp tin t (USD).
// Define a regular exp ression for cur rency values.
Rege x r x = new Regex (@"^-?\d +(\.\d{2})?$?.USD");
// Define some test str ings.
string [] test s = { "-42", "1 9.99" , "0.001", "100 USD" };
// Chec k each tes t stri ng against the regular expression.
foreach (string test in tests)
{
if (rx.IsMatch(test) )
Console.WriteLine("{0} is a currency value.", test);
else
Console.WriteLine("{0} i s not a currency value.", test) ;
}
3/26/2010
VD: http://www.wrox.com
3/26/2010
System.Collection.Arraylist
Phng thc
string ToString()
public virtua l
int GetHashCode()
public virtua l
public virtua l
public static
bool
ReferenceEquals(obje ct
objA , object objB )
public static
Type GetType( )
public
object MemberwiseClone()
void Finalize()
To mt bn sao t i tng
protected
protected virtual
Hm hy ( Destructor)
Mc ch
Phng thc
Compare
CompareOrdinal
Mc ch
Truy xut
1.4 Nhm
cc i tng
Ging compare nhng khng quan tm n ngn ng
20
string ToString()
int GetHashCode()
l
cp bt
ringknhng
hm
chots
V public
tr xutvirtua
hin u
tin ca
1 hoc
1 bm
tp k
dngtp
hp.
trong chui
IndexOf
IndexOfAny
bool Equals(object obj)
LastIndexOf
gPhng
general
thc
n
number
Compare
public static
output
output
So snh cc tham chiu i tng
format
(double
1.2345)
(int
-1 2345)
public static
xem chng c ch n cng i
{0:c}
{0:d}
object
e
MemberwiseClone()
exponent / scienti fi c {0:e}
fixed point
voidf Finalize()
public virtua l
tng
-12,3 45.00
Tr v 1 i tng dn xut t
public
System.Type
m a ra chi tit
System.Format Exception
-12345
kiu d liu
1.23
protected
1.234500e+000
{0:protected
f}
1.23
virtual
{0:g}
{0:n}
So snh
1.2345
-1.234
To mt bn
sao500e+004
t i tng
Hm hy (-12345.00
Destructor)
Mc ch
ni1.23
dung ca 2 chui
-12345
-12,34 5.00
10
3/26/2010
Mc ch
Truy xut
string ToString()
public virtua l
int GetHashCode()
public virtua l
public virtua l
public static
bool
ReferenceEquals(obje ct
objA , object objB )
public static
Type GetType( )
public
object MemberwiseClone()
void Finalize()
protected
protected virtual
Mc ch
Phng thc
Compare
CompareOrdinal
Format
Phng thc
IndexOf
nh dng mt
22 chui cha 1 gi t r khc v ch nh
cch mi g i tr nn c nh dng.
System.Collection.Arraylist
V tr xut h in u tin ca 1 chui con hoc k t trong
Truy xut
Mc ch
chui
string ToString()
public virtua l
tngbt k 1 hoc 1 tp k t
V tr xut hin u tin ca
Cho php nhng i tng cung
trong chui
IndexOfAny
public virtua l
ool
Equals(object
specifier
type objA,
object objB)
boolc
currency
ReferenceEquals(obje ct
decim alobjB )
objAd, object
(whol e number)
Typee GetType(
) / scienti fi c
exponent
output
format
public static
(double 1.2345)
So snh
-12,3
45.00
So snh cc
tham
chiu i tng
xem chng c ch n cng i
tng
System.Format Exception
-12345
Tr v 1 i tng dn xut t
public
m
a ra chi tit
1.234500e+000 System.Type
-1.234
500e+004
kiu d liu
{0:c}
1.23
public static
{0:d}
{0:e}
f
fixed point
{0: f}
1.23
object MemberwiseClone()
protected
g
general
{0:g}
1.2345
void Finalize()
protected virtual
n
number
{0:n}
1.23
Phng thc
round trippabl e
Compare
output
bng 2 i
(inttng
-1 2345)
{0:r}
1.23
-12345.00
To mt bn sao t i tng
-12345
Hm hy ( Destructor)
Mc ch
-12,34 5.00
System.Format Exception
11
3/26/2010
V d: ArrayList
23
V d: ArrayList
24
list.Add("fish");
Console.WriteLine("Index of \"fish\":"+list.IndexOf("fish"));
list.Remove("fish");//Remove "fish" the fist out of list
foreach (string s in list)
Console.WriteLine(s);//Display: amphibian,bird,plant,fish
list.Clear();
Console.WriteLine("count of list:" + list.Count);//Display:0
Console.WriteLine("Index of \"fish\":"+list.IndexOf("fish"));
list.Remove("fish");//Remove "fish" the fist out of list
foreach (string s in list)
Console.WriteLine(s);//Display: amphibian,bird,plant,fish
string [] arra y = list.ToArray(typeof (string)) as string[];
//Loop ove r array .
foreach (string valu e in array)
Console.WriteLine(value);
//Display: amphibian,bird,plant,fish
list.Clear();
Console.WriteLine("count of list:" + list.Count);//Display:0
12
3/26/2010
1.4.2. Collection
1 i tng l 1 collection nu n c th cung cp 1 tham chiu n
mt i tng c lin quan, c bit n nh l enumarator, m c
th duyt qua tng mc trong collection. c bit, 1 collection phi
thi hnh 1 interface:
System.Collections.Ienumerable
interface IEnumera ble
{
IEnumerator GetEnumerator() ;
}
1.4.2. Collection
13
3/26/2010
1.4.2. Collection
V d: vi string
string[] MessageSet = new string[3];
MessageSet[0] = "Hello";
MessageSet[1] = "Good bye";
IEnumerator enumerator = MessageSet.GetEnumerator();
string nextMessage;
enumerator.MoveNext();
while ((nextMessage = enumerator.Current as string) !=
null)
{
Console.WriteLine(nextMessage);
// toNextMessage
enumerator.MoveNext();
}
14
3/26/2010
/ / I nn e r cl a s s i m p le m e nt s I E nu m e ra t o r in t e rf a c e:
p ub l i c c l as s To k e nE n u me r a to r : I En u m er a t or
{
p ri v a te i n t p o s it i o n = - 1 ;
p ri v a te T o ke n s t;
p ub l i c T o k en E n um e r at o r (T o k en s t)
{
t hi s . t = t ;
}
/ / D e c la r e th e M ov e N ex t m et h o d re q u ir e d by I En u m e ra t o r:
p ub l i c b o o l M o v eN e x t ()
{
i f ( p os i t io n < t . e le m e nt s . Le n g th - 1 )
{
p os i t io n + +;
r et u r n t r u e;
}
e ls e
{
r et u r n f a l se ;
}
}
/ / D e c la r e th e R es e t me t h od r e qu i r ed b y IE n u me r a t or :
p ub l i c v o i d R e s et ( )
{
p os i t io n = - 1 ;
}
/ / D e c la r e th e C ur r e nt p r op e r ty r e q ui r e d b y IE n u m er a t or :
p ub l i c o b j ec t C ur r e n t
{
g et
{
r et u r n t . e le m e nt s [ po s i ti o n ];
}
}
}
29
30
15
3/26/2010
31
16
3/26/2010
p ub l i c c l as s Ve c t or
{
f lo a t a ;
f lo a t b ;
f lo a t c;
p ub l i c V e c to r ( )
{
a = 0;
b = 0;
c = 0;
}
p ub l i c V e c to r ( in t a , in t b, i n t c )
{
t hi s . a = a ;
t hi s . b = b ;
t hi s . c = a ;
}
p ub l i c o v e rr i d e s t r i ng T o St r i ng ( )
{
r et u r n " ( " +a + " ," + b +" , " +c + " )" ;
}
}
pub li c c las s V ec to rs : I En um er abl e
{
pub li c V ec to r[ ] el em en ts ;
pub li c i nt nu m;
pub li c V ec to rs ()
{
ele me nt s = ne w Vec to r[ 3] ;
ele me nt s[ 0] = n ew Ve ct or (1 ,2 ,3) ;
ele me nt s[ 1] = n ew Ve ct or (2 ,2 ,2) ;
ele me nt s[ 2] = n ew Ve ct or (3 ,3 ,3) ;
num = 3;
}
pub li c I En um er at or G etE nu me ra to r()
{
ret ur n n ew Ve ct or En um era to r( th is );
}
}
p ub l i c c l as s Ve c t or E n um e r at o r : IE n u me r a to r
{
p ri v a te i n t p o s it i o n = - 1 ;
p ri v a te V e ct o r s v ;
p ub l i c V e c to r E nu m e ra t o r( V e ct o r s v )
{
t hi s . v = v ;
}
p ub l i c b o o l M o v eN e x t ()
{
i f ( p os i t io n < v . n um - 1 )
{
p os i t io n + +;
r et u r n t r u e;
}
e ls e r e t ur n f al s e ;
}
p ub l i c v o i d R e s et ( )
{
p os i t io n = - 1 ;
}
p ub l i c o b j ec t C ur r e n t
{
g et { re t u rn v . el e m en t s [p o s it i o n] ; }
}
s ta t i c v o id M ai n ( )
{
V ec t o rs v = ne w Ve c t or s ( );
I En u m er a t or e = v. G e tE n u me r a to r ( );
V ec t o r s ;
w hi l e (e . M ov e N ex t ( )) {
s = e .C u r re n t as V e c to r ;
C on s o le . W ri t e Li n e (s ) ;
}
}
}
33
17
3/26/2010
class Employee
{
string Id;
string Name;
int Year;
public Employee(string id, string name, int year)
{
this.Id = id;
this.Name = name;
this.Year = year;
}
public override string ToString()
{
return Id + "\t" + Name + "\t" + Year;
}
}
18