Download - Chuong 2 Basic C#
-
8/3/2019 Chuong 2 Basic C#
1/111
Chng 2 C# Language Fundamentals
-
8/3/2019 Chuong 2 Basic C#
2/111
N i dung C ch vi t 1 console application
Console I/ 0 v lnh nh p xu t Khai b o bi n:
Strongly type Implicited type local variable
C c ki u d li u Value type Reference type
2
-
8/3/2019 Chuong 2 Basic C#
3/111
N i dung (tt) Chuy n i ki u Boxing and unboxing Tham s ref, out, param
L nh lp for, while, do while, foreach L nh ph n nh nh switch, l nh nhy C c ki u d li u ph c:
Enumeration Struct Array
3
-
8/3/2019 Chuong 2 Basic C#
4/111
Visual studio 2008 environment
L mt mi tr ng l p tr nh nhi u cng c(tool-rich programming environment) ch a m i
nh nng c n thi t t o c c d n C# t nh n l n
4
-
8/3/2019 Chuong 2 Basic C#
5/111
Basic C# syntax
C# code is made up of a series of statements,each of which is terminated with a semicolon.
C# is a block-structured language {} C# code is that it is case sensitive.
5
-
8/3/2019 Chuong 2 Basic C#
6/111
Console Application
Console Application : l ng d ng m input voutput u n m trong console window.
Console window: c n g i l c a s d u nh cl nh (MS-DOS command prompt)
6
-
8/3/2019 Chuong 2 Basic C#
7/111
ng d ng console C# u tin
7
// Ch ng trnh C# u tin
using System;using System.Collections.Generic;using System.Text;
namespace HelloWorld {
class Program {
static void Main( string [] args)
{ Console .Write( "Hello World!" );Console .ReadLine();
}}
}
-
8/3/2019 Chuong 2 Basic C#
8/111
Cu trc chng tr nh C# Ph n ch thch (option)
Ph n khai b o dng namespace (option)
Ph n nh ngha namespace v l p
8
// Ch ng trnh C# u tin
using System;using System.Collections.Generic;using System.Text;
namespace HelloWorld {
class Program {static void Main( string [] args){ {Console .Write( "Hello World!" );Console .ReadLine();
}}
}
-
8/3/2019 Chuong 2 Basic C#
9/111
C u l nh (Statement) C c c u l nh c vi t trong th n ca phnth c (method) Th c hi n m t cng vi c n o
K t thc bi du chm phy (; )
9
namespace HelloWorld {
class Program {
static void Main( string [] args){
Console .Write( "Hello World!" );Console .ReadLine();
}}
}
Cc cu lnh
Phng thc Main
-
8/3/2019 Chuong 2 Basic C#
10/111
Khong tr ng
Bao gm K t tr ng, k t xung d ng, k t tab D ng trng
S d ng hp l chng tr nh d c
10
namespace HelloWorld { class Program
{
static void Main( string [] args){Console .Write( "Hello World!" );
Console .ReadLine();}}}
namespace HelloWorld {
class Program {
static void Main( string [] args){
Console .Write( "Hello World!" );Console .ReadLine();
}}
}
-
8/3/2019 Chuong 2 Basic C#
11/111
Ch thch
Ch thch (comment) c dng gii thchv chng tr nh v c c c u l nh
Gip cho chng tr nh d hi u hn c b qua khi bin dch Khng nh hng t i k t qu th c thi ca
chng tr nh C th ph t sinh ra documentation ca chng
trnh qua ch thch XML
11
-
8/3/2019 Chuong 2 Basic C#
12/111
Hai c ch t o ch thch c bn
G ph n ch thch sau cp k t// G ph n ch thch gi a cp k t/* v */
12
/* Ch ng trnh C# u tin In ra cu cho "Hello World" */
using System;
namespace HelloWorld {
class Program {
static void Main(string[] args){
Console.Write("Hello World!"); // Xut ra cu cho Console.ReadLine(); // Ch nhn Enter
}}
}
-
8/3/2019 Chuong 2 Basic C#
13/111
XML Comment
Cho php ph t sinh ra su li u d ng XML Thch hp cho vi c vi t su li u ca d n l
Ch thch XML b t u v i triple slash (///)v c c tag ca XML Ch thch XML dng cho
User defined types Class, delegate, enum and struct Member of user defined types
13
-
8/3/2019 Chuong 2 Basic C#
14/111
V d : ng d ng CONSOLE n gin
14
-
8/3/2019 Chuong 2 Basic C#
15/111
Using Namespace
gii quy t v n xung t tn (name-crashing) nn t o 1 container c t tn
namespace Hai class c th trng tn nhau nhng khng bi s
d ng nh m l n n u thu c 2 namespace kh c nhau
15
Tn y TextHello.Greeting
-
8/3/2019 Chuong 2 Basic C#
16/111
Using Namespace
C c class trong .NET framework c ng c s px p v o c c namespace kh c nhau.
V d : l p Console thu c namespace System tham chi u n 1 l p n o , c th dng
tn y :Namespace.ClassName
Hoc tr nh phi vi t y tham chi u nnamespace, c th dng using directive uchng trnh
16
-
8/3/2019 Chuong 2 Basic C#
17/111
Using Namespace
Bn namespace thng d ng hay c dngtrong c c chng trnh s c Visual Studiot ng a vo u m i chng trnh m i.
17
-
8/3/2019 Chuong 2 Basic C#
18/111
File program.cs
M t chng tr nh n gin nh t bao gm: 1file program.cs, trong file c 1 namespace mcnh v 1 class.
Class: phi c ti thi u1 hm Main
static void Main( string[] args)
{}
18
-
8/3/2019 Chuong 2 Basic C#
19/111
19
-
8/3/2019 Chuong 2 Basic C#
20/111
Tnh nng IntelliSense ca IDE
T ng hi n th danh s ch c c th nh ph nca class khi l nh c g v o c a s so ntho.
C# s hi n th th nh ph n (member) ph hpv i c c k t ang c g v o, sau hi nth tool tip ch a m t nh nng ca th nhph n.
20
-
8/3/2019 Chuong 2 Basic C#
21/111
Console I/O
c k t vn bn t c a s console Console.Read() gi tr tr v l int
Console.ReadLine() gi tr tr v l string
xut chu i k t dng Console.Write() / Console.WriteLine()
21
-
8/3/2019 Chuong 2 Basic C#
22/111
Printing on the Console
nh d ng vn bn xu t ra:Console.WriteLine( "{0}\n{1}",
"Welcome to", "C# Programming!" );
22
i s cha chu i nh dang
i s 0 i s 1
-
8/3/2019 Chuong 2 Basic C#
23/111
23
-
8/3/2019 Chuong 2 Basic C#
24/111
K t nh d ng chu i
24
-
8/3/2019 Chuong 2 Basic C#
25/111
25
-
8/3/2019 Chuong 2 Basic C#
26/111
Console I/O
Console.WriteLine()
26
\ n: k t xung dng
-
8/3/2019 Chuong 2 Basic C#
27/111
Console I/O
27
/ F5
Xut chui c chui
Ch c 1 dng, mc ch l dng mn hnh
-
8/3/2019 Chuong 2 Basic C#
28/111
C# Is a strongly Typed Language
C# l mt ngn ng c kim sot chtch v mt kiu d liu
M i bi n (variable) v i tng (objectinstance) trong h thng phi c x c nhki u t ng minh (well-defined type)
Cho php compiler d d ng ki m tra c nh hp l c c php to n trn c c bi n v i
tng.
28
-
8/3/2019 Chuong 2 Basic C#
29/111
Khai b o bi n
Khai b o bi n theo 2 d ng: ; =; Hay dng k t hp c 2 d ng
V d : int xSize, ySize; int age = 25; int xSize, ySize = 5;
29
-
8/3/2019 Chuong 2 Basic C#
30/111
Variable name
Hai quy t c: K t u phi l ch ci (letter) hoc d u _ hoc
@. C c k t p theo c th l ch ci, _ hay s.
Ph n bi t CH HOA v ch th ng Phi kh c v i t kha (dng @ kh c ph c)
30
-
8/3/2019 Chuong 2 Basic C#
31/111
G n gi tr bi n
Assign operator: = Ex
myInteger = 17;myString = "\"myInteger\" is";
31
-
8/3/2019 Chuong 2 Basic C#
32/111
Ph m vi bi n
Ph m vi (scope) c x c nh bi cp du{ v } C th ch a ph m vi nh hn
V tr khai b o bi n Trong th n phng th c: bi n c c b Trong th n l p(trong class): thu c tnh
Bi n trong C# ch c t c d ng trong ph m vim n c khai b o
32
-
8/3/2019 Chuong 2 Basic C#
33/111
Keyword
abstract const extern in operator sbyte throw viras continue false int out sealed true voidbase decimal finally interface override set try vobool default fixed internal params short typeof wbreak delegate float is partial sizeof uint whbyte do for lock private stackalloc ulong yiecase double foreach long protected static uncheckedcatch else get namespace public string unsafe
char enum goto new readonly struct ushortchecked event if null ref switch usingclass explicit implicit object return this value
Cc t kha trong C# 2005
33
-
8/3/2019 Chuong 2 Basic C#
34/111
Data types
There are two kinds of data types in C#. Value Types (implicit data types, structs and
enumeration)
Reference Types (objects, delegates)
34
-
8/3/2019 Chuong 2 Basic C#
35/111
Values types
Bi n lo i tham tr (value types) ch a 1 gi trc th ca lo i d li u n o .
V d : bi n snguyn (int) ch a gi tr7
35
-
8/3/2019 Chuong 2 Basic C#
36/111
Reference types
Bi n lo i tham chi u ch a a ch b nh ch ad li u m bi n tham chi u n.
36
-
8/3/2019 Chuong 2 Basic C#
37/111
Reference type
37
String s1= "Hello";
"Hello"
"Bye"s2
s3
s1
string s2 = "Bye";string s3;s3 = s1;
-
8/3/2019 Chuong 2 Basic C#
38/111
38
-
8/3/2019 Chuong 2 Basic C#
39/111
Implicit data types
M i ki u d li u ng m nh u c nh xth nh m t ki u tng ng trong CommonType System (CTS) v CLS (Common LanguageSpecification).
39
-
8/3/2019 Chuong 2 Basic C#
40/111
C c ki u d li u ng m nh trong C#
40
-
8/3/2019 Chuong 2 Basic C#
41/111
C c ki u d li u ng m nh trong C#
41
-
8/3/2019 Chuong 2 Basic C#
42/111
B nh my nh c t ch c nh th no?
H iu h nh chia b nh th nh 2 chunk kh cnhau, m i chunk c qun ltheo 1 c chkh c nhau.
Stack Heap
42
-
8/3/2019 Chuong 2 Basic C#
43/111
Value types v reference types
T t c lo i tham tr (value) u c t o trnstack.
T t c lo i tham bi n (reference) u c t otrn heap Nhng con tr tham chi u (reference pointer) th
n m trn stack.
43
-
8/3/2019 Chuong 2 Basic C#
44/111
44
-
8/3/2019 Chuong 2 Basic C#
45/111
45
-
8/3/2019 Chuong 2 Basic C#
46/111
Stack
A stack is a data structure that store items ina first in first out (FIFO) fashion. It is an area of memory supported by the processor and its
size is determined at the compile time.
46
-
8/3/2019 Chuong 2 Basic C#
47/111
Heap
A heap consists of memory available to theprogram at run time. Reference types areallocated using memory available from theheap dynamically (during the execution of program). The garbage collector searches fornon-referenced data in heap during theexecution of program and returns that space
to Operating System.
47
-
8/3/2019 Chuong 2 Basic C#
48/111
Value type vs. Reference type
Characteristic Value type Reference type
Variable hold Value ReferenceAllocated Stack Heap
Default Zero Null
Parameter Copy value Copy reference
48
-
8/3/2019 Chuong 2 Basic C#
49/111
Constant
M t h ng l m t bi n nhng tr khng thay const int a = 100 ; // gi tr ko th thay i
H ng b t bu c phi c g n gi tr lc khaibo
Tr ca h ng c th c tnh to n v o lc bi
dch H ng bao gi c ng static
49
-
8/3/2019 Chuong 2 Basic C#
50/111
Constant
u i m Chng tr nh d c, kh c ph c nh ng con s
magic number trong code. Chng tr nh d s a hn. Tr nh l i d d ng hn, tr nh bin dch s b o l i
n u g n l i gi tr cho h ng
50
-
8/3/2019 Chuong 2 Basic C#
51/111
Minh h a s d ng h ng
51
nh ngha hng
Constant
-
8/3/2019 Chuong 2 Basic C#
52/111
readonly
52
const : phi c gn gi tr khi khai boreadonly : ko cn khi to trc , khi gn gi tr th sau ko thayi c
Cha c khi gn
Ko c thay i
-
8/3/2019 Chuong 2 Basic C#
53/111
Implicitly Typed Local Variables
In C#, every variable declared in the codemust have an explicit type associated with it.But sometimes, when writing code for strongly
typed languages, the amount of typingneeded to declare such variables can betedious
53
Bi lo i di
-
8/3/2019 Chuong 2 Basic C#
54/111
Bi n lo i suy dinType inference
var l t kha m i trong C# 3.0 C ch khai b o:
var =;
Bi n c lo i ng m nh l lo i cavalue.
V d : var myVar = 5;myVar l bi n lo i int
54
-
8/3/2019 Chuong 2 Basic C#
55/111
using System;using System.Collections.Generic;
public class EntryPoint{static void Main(){
var myList = new List();myList.Add( 1 );myList.Add( 2 );myList.Add( 3 );foreach( var i in myList ){
Console.WriteLine( i );
}}
}
An implicitly typedvariable declaration
must include an
initialzer
var newValue; // emits error CS0818
var a = 2, b = 1;var x, y = 4;
Not permited
55
Chuy i ki d li
-
8/3/2019 Chuong 2 Basic C#
56/111
Chuy n i ki u d li uType Conversion (cast)
Nhi u lc c n chuy n i d li u cho c cinstances sang 1 lo i d li u kh c
C 2 d ng chuy n i Implicit cast Explicit cast
56
-
8/3/2019 Chuong 2 Basic C#
57/111
Implicit cast
Tun theo promotion rules. ch c php chuy n ki u n o m khng l m m t
d li u th m i c php
Do C# t th c hi n Khng c n l p tr nh vin can thi p
57
-
8/3/2019 Chuong 2 Basic C#
58/111
Implicit type cast
Xy ra khi p t ki u nh qua ki u l n
p t l p d n xut qua l p c s
58
int i = 59;double x = i;
string s = "Hello";object o = s;
-
8/3/2019 Chuong 2 Basic C#
59/111
Implicit type-cast
From Tosbyte short, int, long, float, double, decimal
byte short, ushort, int, uint, long, ulong, float, double, decimal
short int, long, float, double, decimal
ushort int, uint, long, ulong, float, double, decimal
int long, float, double, decimal
uint long, ulong, float, double, decimal
long, ulong float, double, decimal
float double
char ushort, int, uint, long, ulong, float, double, decimal
59
-
8/3/2019 Chuong 2 Basic C#
60/111
Explicit type-cast
Do l p tr nh vin ch nh Xy ra khi
p t ki u l n qua ki u nh : c th mt gi tr
p t l p c s qua l p d n xut
60
double x = 74.86;int i = ( int ) ) x; // i = 74
string s = "Hello" ;object o = s;string s2 = ( string )o;
-
8/3/2019 Chuong 2 Basic C#
61/111
Using Convert class
Th ng dng khi c n chuy n i gi a c c kikhng c lin h v i nhau
Convert.toDataType( SourceValue )
V d : chuy n t chu i sang s th c
61
string s1 = "56.8" ;
double x = Convert.ToDouble(s1); // x = 56.8int i = Convert.ToInt32(s2); // i = 95
string s2 = "95" ;
byte j = Convert.ToByte(x); // j = 56, t dng
-
8/3/2019 Chuong 2 Basic C#
62/111
System.Object class
62
-
8/3/2019 Chuong 2 Basic C#
63/111
Boxing and unboxing
Dng ch vic chuy n d li u t stack sangheap v ngc l i
M c ch: ci thi n vi c th c thi chng trnh
63
-
8/3/2019 Chuong 2 Basic C#
64/111
Boxing
int i =42;Object o = i;
I is a value type, so it exists in the stack.
O is a reference type, so it must refer toobjects on the heap.
The runtime allocates a piece of memory
from the heap, copies the value of integer i tothis piece, and refers the object o to this copy.
64
-
8/3/2019 Chuong 2 Basic C#
65/111
Boxing
The automatic copying of an item from thestack to the heap is called boxing.
65
-
8/3/2019 Chuong 2 Basic C#
66/111
Boxing
If you modify the original value of a variable,the value on the heap will not change and viceversa.
66
-
8/3/2019 Chuong 2 Basic C#
67/111
Unboxing
Int i=o compile time error. The object o canrefer to absolutely anything and not just anint.
To obtain the value of the boxed copy, youmust use a cast operation
67
-
8/3/2019 Chuong 2 Basic C#
68/111
Unboxing
Int I = 42;Object o = I; //boxesI = (int)o; // compiles okay
68
b i
-
8/3/2019 Chuong 2 Basic C#
69/111
unboxingCircle c = new Circle(42);Object o = c; // doesnt box because c is a reference
variable;Int I = (int) o; //compiles okay but throws an exception
at run time
69
-
8/3/2019 Chuong 2 Basic C#
70/111
70
nh hng ca boxing/unboxing n vi c
-
8/3/2019 Chuong 2 Basic C#
71/111
nh hng ca boxing/unboxing n vi cth c thi chng trnh
71
checked & unchecked
-
8/3/2019 Chuong 2 Basic C#
72/111
checked & unchecked
72
throws OverFlowException
The checked and unchecked statements areused to control the overflow checking contextfor integral-type arithmetic operations andconversions
-
8/3/2019 Chuong 2 Basic C#
73/111
ref , out, param ref: tng t nh truy n tham chi u trong C/C++ T kho ref phi c dng lc g i h m C c tham s truy n d ng ref phi c khi t o gi
tr tr c
73
Khai bo ref trckiu d liu
s dng ref chotham s khi gi hm
-
8/3/2019 Chuong 2 Basic C#
74/111
-
8/3/2019 Chuong 2 Basic C#
75/111
ref, out, param
75
3 ph n t 6 ph n t Mng array
Lun khaib o cuidanh schtham s
-
8/3/2019 Chuong 2 Basic C#
76/111
Keyword this
76
public class list{
private int size;
...
public SetSize ( int size ){this.size = size;
}
-
8/3/2019 Chuong 2 Basic C#
77/111
Loop
Tng t nh C: while, do while, for
77Phi l gi tr bool: true, false
while < iu kin>{
// phn thn while}
do{
// phn thn do while} while < iu kin>;
for( khi tao bin lp; ; thay i bin lp){
// phn thn for
}
-
8/3/2019 Chuong 2 Basic C#
78/111
Loop
78
index = 10;while (index != 0){
Console .WriteLine(index);index--;
}
index = 0;do {
Console .WriteLine("Happens at least once");} while (index < 0);
for (index = 0; index < 100; index++){Console. Write(index);Console. Write("\t");
}
Gi tr {true, false}
-
8/3/2019 Chuong 2 Basic C#
79/111
foreach
79
foreach( typedata identifier in objectArray)
{// thn foreach
}
=
Ch s dng bin icho mi ln lp
S dng ch smng nh bnh
thng
-
8/3/2019 Chuong 2 Basic C#
80/111
switch
Bi u th c switch gm: ki u s, k t , enum v chu i
S d ng break, goto, return i u khi n lung th c thi N u ko nh n n o ph hp default N u ko c default th c hi n l nh sau switch
80
-
8/3/2019 Chuong 2 Basic C#
81/111
Jump
break Tho t kh i v ng lp
continue Qua b c lp k
goto Nhy n nh n S d ng goto case , trong switch
81
-
8/3/2019 Chuong 2 Basic C#
82/111
return
Tho t kh i h m void
Tr v 1 gi tr ca h m
82
void Func1(int x){
if (x == 0)
return;
...
}
int max(int a, int b){
if (a > b)return a;else
return b;}
Demo
-
8/3/2019 Chuong 2 Basic C#
83/111
Demo Rolling a Six-Sided Die
Let us develop an application that simulates20 rolls of a six-sided die and displays thevalue of each roll
83
-
8/3/2019 Chuong 2 Basic C#
84/111
Class Random (of namespace System)
Objects of class Random can producerandom byte, int and double values. A new random-number generator object can
be created as follows:Random randomNumbers = new Random();
The random-number generator object can
then be used to generate random byte, intand double values
84
f
-
8/3/2019 Chuong 2 Basic C#
85/111
Class Random (of namespace System)
Method Next of class Random generates arandomint randomValue = randomNumbers.Next();
Values returned by Next are actuallypseudorandom numbers a sequence of values produced by a complex mathematicalcalculation.
85
C l i bl
-
8/3/2019 Chuong 2 Basic C#
86/111
Complex variable types
C# cung c p ba lo i bi n ph c nhng r tthng d ng: Enumerations (often referred to as enums)
structs (occasionally referred to as structures) Arrays
86
h gh E i
-
8/3/2019 Chuong 2 Basic C#
87/111
nh ngh a Enumeration
Dng t kha enum nh ngh a bi n ki uenumerationsenum {,,,...}
87
h gh E i
-
8/3/2019 Chuong 2 Basic C#
88/111
nh ngh a Enumeration
M t c ch nh ngh a kh c: d a v o ki u c s(underlying type) lu tr gi tr.
enum :{,,,...
}
Underlying types of byte, sbyte, short, ushort,int, uint, long,and ulong.
88
h gh i
-
8/3/2019 Chuong 2 Basic C#
89/111
nh ngh a enumeration
Mc nh, m i gi tr c g n cho 1 gi trtng ng v i underlying type value theo tht c nh ngh a c g n gitr 0, c gi tr1
C th override php g n n y b ng to n t =
89
Kh i b d ki i
-
8/3/2019 Chuong 2 Basic C#
90/111
Khai b o v s d ng ki u enumeration
Khai b o bi n ki u enumeration:;
G n gi trcho bi n: = .;
90
-
8/3/2019 Chuong 2 Basic C#
91/111
91
St t (h t t )
-
8/3/2019 Chuong 2 Basic C#
92/111
Struct (hay structure)
L kiu d li u c hp th nh t nhi u ph nd li u c ki u d li u kh c nhau. Cho php ng i dng t nh ngh a ki u d
li u ca ring m nh.
92
h ngh ki t t
-
8/3/2019 Chuong 2 Basic C#
93/111
nh ngh a ki u structstruct {}
M i c d ng nh sau: ;
V d : struct route{ public orientation direction;
public double distance;}
93
A
-
8/3/2019 Chuong 2 Basic C#
94/111
Array
is a collection of values of a similar data type C# arrays are a reference type. Each array in C# is an object and is inherited
from the System.Array class. Arrays are declared as: [] = new []
Ex: int [] integers = new int[10];Hoc: int [] integers;integers = new int[10];
94
-
8/3/2019 Chuong 2 Basic C#
95/111
95
A
-
8/3/2019 Chuong 2 Basic C#
96/111
Array
The size of an array is fixed and must bedefined before using it.
Can use variables to define the size of thearray:
int size = 10;int [] integers = new int[size];
Array can be defined by using the valuesint [] integers = {1, 2, 3, 4, 5};
96
Truy xu t c c ph n t mng
-
8/3/2019 Chuong 2 Basic C#
97/111
y p gAccessing the values stored in an array
S d ng to n t ch m c (indexing operator)[int index]
Gi tr ch mc trong C# b t u t 0. Truy xu t c c ph n t ca mng:
Dng v ng lp for v to n t ch s Dng v ng lp foreach
Note: v ng lp foreach chcho php read-only.Khng th th c hi n:
foreach (string friendName in friendNames)
{ friendName = "Rupert the bear}; 97
V d 1
-
8/3/2019 Chuong 2 Basic C#
98/111
V d 1
static void Main(){
// declaring and initializing an array of type integerint [] integers = {3, 7, 2, 14, 65};for(int i=0; i
-
8/3/2019 Chuong 2 Basic C#
99/111
V d 2
static void Main(){
// declaring and initializing an array of type integer
int [] integers = {3, 7, 2, 14, 65};foreach(int i in integers){
Console.WriteLine(i);}
}
99
Array
-
8/3/2019 Chuong 2 Basic C#
100/111
Array
Ly kch th c mng qua thu c tnh Length int Size = myArray.Length;
N u th nh ph n ca mng l ki u nh tr c,
c th dng h m Sort ca Array s p x p Array.Sort(myArray); Dng h m Reverse ca Array o th t
c c ph n t trong mng Array.Reverse(myArray);
100
Array
-
8/3/2019 Chuong 2 Basic C#
101/111
Array
101
Dng phng thc tnh Sortca lp Array sort artists
Dng phng thc tnh Reverseca lp Array o th t artists
Multidimensional Arrays
-
8/3/2019 Chuong 2 Basic C#
102/111
Multidimensional Arrays
A multidimensional array is simply one thatuses multiple indices to access its elements.
C ch khai b o[,,,] ;
V d : t o mng 2 chi u 3 h ng 4 c tdouble[,] hillHeight = new double[3,4];
double[,] hillHeight = { { 1, 2, 3, 4 }, { 2, 3, 4, 5 }, {3, 4, 5, 6 } };
102
Multi dimensional Array
-
8/3/2019 Chuong 2 Basic C#
103/111
Multi-dimensional Array
Khai b o mng int2 dng 3 c t int[,] myMatrix = new int[2,3];
C th khi g n int[,] myMatrix = new int[,] {{1,2},{3,4},{5,6},{7,8}}; int[,] myMatrix = {{1,2},{3,4},{5,6},{7,8}}; string[,] beatleName = {{"Lennon","John"},
{"McCartney","Paul"},{"Harrison","George"},{"Starkey","Richard"}};
103
Datatype[,] array-name
-
8/3/2019 Chuong 2 Basic C#
104/111
104
Multi dimensional Array
-
8/3/2019 Chuong 2 Basic C#
105/111
Multi-dimensional Array
105
Truy cp tun t theo kiumng1 chiu
Truy cp theo dng dng ctqua ch mc i v j
Jagged Array (Arraysof Arrays)
-
8/3/2019 Chuong 2 Basic C#
106/111
Jagged Array (Arraysof Arrays)
Jagged l mng m m i ph n t l m t mng c kcth ckhc nhau
Nh ng mng con n y phi c khai b o ring Khai b o mng3 d ng, m i d ng l m t mng1 chi u
106
Datatype[ ][ ] array-name
int[][] a = new int[3][];a[0] = new int[4];a[1] = new int[3];a[2] = new int[1];
Khai bo s dng, hng
Khai bo s ct ring cho tng dng
-
8/3/2019 Chuong 2 Basic C#
107/111
107
Jagged Array
-
8/3/2019 Chuong 2 Basic C#
108/111
Jagged Array
108
Truy cp theo dng, ct Truy cp dng foreach
Jagged array
-
8/3/2019 Chuong 2 Basic C#
109/111
gg y A jagged array cannot be completely created
with a single array creation expression. Ex: int[][] c = new int[ 2 ][ 5 ]; // error
Each one-dimensional array in the jaggedarray must be initialized separately. Ex:int[][] c;
c= new int[ 2 ][ ]; // create 2 rowsc[ 0 ]= new int[ 5 ]; // create 5 columns for row 0c[ 1 ]= new int[ 3 ]; // create 3 columns for row 1
109
Demo
-
8/3/2019 Chuong 2 Basic C#
110/111
C ch t o mng chu i L m quen 1 s phng thc thng d ng ca
l p string. T o 1 mng m m i ph n t la 1 word trong 1
chuoi duoc nhap vao luc khoi tao lop private string[] myWords;
110
String class
-
8/3/2019 Chuong 2 Basic C#
111/111
String class