Tài liệu Lập trình C# và cấu hình Visual Studio 2013

Tóm tắt Tài liệu Lập trình C# và cấu hình Visual Studio 2013: ... = Name;           Console.WriteLine("- Animal(string)");       }       // Phương thức mô tả hành vi di chuyển của con vật.       // virtual: Nói rằng phương thức này có thể ghi đè tại các class con.       public virtual void Move()       {           Console.WriteLine("Animal Move");       }... Mouse)            {                // Ép kiểu                Mouse mouse = (Mouse)canEat2;                // Gọi method drink (Thừa kế từ CanDrink).                mouse.Drink();            }            Console.ReadLine();        }    } } Kết quả chạy ví dụ: Access Modifier trong C# 1-....        public static E GetFirstElement(List list, E defaultValue)        {            if (list == null || list.Count == 0)            {                return defaultValue;            }            E first = list.ElementAt(0);            return first;        }    } } Ví dụ sử dụng phương...

docx159 trang | Chia sẻ: havih72 | Lượt xem: 180 | Lượt tải: 0download
Nội dung tài liệu Tài liệu Lập trình C# và cấu hình Visual Studio 2013, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
eTimeKind kind)
TODO - EXAMPLE
Now là một thuộc tính tĩnh của DateTime nó trả về đối tượng DateTime mô tả thời điểm hiện tại.
?
1
2
3
4
5
// Đối tượng mô tả thời điểm hiện tại.
DateTime now = DateTime.Now;
Console.WriteLine("Now is "+ now);
2- Các thuộc tính DateTime
DateTimePropertiesExample.cs
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DateTimeTutorial
{
   class DateTimePropertiesExample
   {
       public static void Main(string[] args)
       {
           // Tạo một đối tượng DateTime (năm, tháng, ngày, giờ, phút, giây).
           DateTime aDateTime = new DateTime(2005, 11, 20, 12, 1, 10);
           // In ra các thông tin:
           Console.WriteLine("Day:{0}", aDateTime.Day);
           Console.WriteLine("Month:{0}", aDateTime.Month);
           Console.WriteLine("Year:{0}", aDateTime.Year);
           Console.WriteLine("Hour:{0}", aDateTime.Hour);
           Console.WriteLine("Minute:{0}", aDateTime.Minute);
           Console.WriteLine("Second:{0}", aDateTime.Second);
           Console.WriteLine("Millisecond:{0}", aDateTime.Millisecond);
           // Enum {Monday, Tuesday,... Sunday}
           DayOfWeek dayOfWeek = aDateTime.DayOfWeek;  
           Console.WriteLine("Day of Week:{0}", dayOfWeek );
           Console.WriteLine("Day of Year: {0}", aDateTime.DayOfYear);
           // Một đối tượng chỉ mô tả thời gian (giờ phút giây,..)
           TimeSpan timeOfDay = aDateTime.TimeOfDay;
           Console.WriteLine("Time of Day:{0}", timeOfDay);
           // Quy đổi ra Ticks (1 giây = 10.000.000 Ticks)
           Console.WriteLine("Tick:{0}", aDateTime.Ticks);
           // {Local, Itc, Unspecified}
           DateTimeKind kind = aDateTime.Kind;
           Console.WriteLine("Kind:{0}", kind);
           Console.Read();
       }
   }
}
Chạy ví dụ:
3- Thêm và bớt thời gian
DateTime cung cấp các phương thức cho phép bạn thêm, hoặc trừ một khoảng thời gian. TimeSpan là một class chứa thông tin một khoảng thời gian, nó có thể tham gia như một tham số trong các phương thức thêm bớt thời gian của DateTime.
Ví dụ:
AddSubtractExample.cs
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DateTimeTutorial
{
   class AddSubtractExample
   {
       public static void Main(string[] args)
       {
           // Thời điểm hiện tại.
           DateTime aDateTime = DateTime.Now;
           Console.WriteLine("Now is " + aDateTime);
           // Một khoảng thời gian.
           // 1 giờ + 1 phút
           TimeSpan aInterval = new System.TimeSpan(0, 1, 1, 0);
           // Thêm khoảng thời gian.
           DateTime newTime = aDateTime.Add(aInterval);
           Console.WriteLine("After add 1 hour, 1 minute: " + newTime);
           // Trừ khoảng thời gian.
           newTime = aDateTime.Subtract(aInterval);
           Console.WriteLine("After subtract 1 hour, 1 minute: " + newTime);
           Console.Read();
       }
   }
}
Chạy ví dụ:
Class DateTime cũng có các phương thức cho phép thêm bớt một loại đơn vị thời gian chẳng hạn:
AddYears
AddDays
AddMinutes
...
Ví dụ:
AddSubtractExample2.cs
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DateTimeTutorial
{
   class AddSubtractExample2
   {
       public static void Main(string[] args)
       {
           // Thời điểm hiện tại.
           DateTime aDateTime = DateTime.Now;
           Console.WriteLine("Now is " + aDateTime);          
           // Thêm 1 năm
           DateTime newTime = aDateTime.AddYears(1);
           Console.WriteLine("After add 1 year: " + newTime);
           // Trừ 1 giờ
           newTime = aDateTime.AddHours(-1);
           Console.WriteLine("After add -1 hour: " + newTime);
           Console.Read();
       }
   }
}
Chạy ví dụ:
Đôi khi bạn cần tìm ngày đầu tiên hoặc ngày cuối cùng của một tháng hoặc một năm cụ thể. Chẳng hạn bạn đặt ra câu hỏi tháng 2 năm 2015 là ngày bao nhiêu? ngày 28 hay 29. Ví dụ dưới đây có một vài phương thức tiện ích để làm điều này:
FirstLastDayDemo.cs
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DateTimeTutorial
{
   class FirstLastDayDemo
   {
       public static void Main(string[] args)
       {
           Console.WriteLine("Today is " + DateTime.Today);
           DateTime yesterday = GetYesterday();
           Console.WriteLine("Yesterday is " + yesterday);
           // Ngày đầu tiên của tháng 2 năm 2015
           DateTime aDateTime = GetFistDayInMonth(2015, 2);
           Console.WriteLine("First day of 2-2015: " + aDateTime);
           // Ngày cuối cùng của tháng 2 năm 2015
           aDateTime = GetLastDayInMonth(2015, 2);
           Console.WriteLine("Last day of 2-2015: " + aDateTime);
           // Ngày đầu tiên của năm 2015
           aDateTime = GetFirstDayInYear(2015);
           Console.WriteLine("First day year 2015: " + aDateTime);
           // Ngày cuối cùng của năm 2015
           aDateTime = GetLastDayInYear(2015);
           Console.WriteLine("First day year 2015: " + aDateTime);
           Console.Read();
       }
       // Trả về ngày hôm qua.
       public static DateTime GetYesterday()
       {
           // Ngày hôm nay.
           DateTime today = DateTime.Today;
           // Trả về ngày trước 1 ngày.
           return today.AddDays(-1);
       }
       // Trả về ngày đầu tiên của năm
       public static DateTime GetFirstDayInYear(int year)
       {
           DateTime aDateTime = new DateTime(year, 1, 1);
           return aDateTime;
       }
       // Trả về ngày cuối cùng của năm.
       public static DateTime GetLastDayInYear(int year)
       {
           DateTime aDateTime = new DateTime(year +1, 1, 1);
           // Trừ đi một ngày.
           DateTime retDateTime = aDateTime.AddDays(-1);
           return retDateTime;
       }
       // Trả về ngày đầu tiên của tháng
       public static DateTime GetFistDayInMonth(int year, int month)
       {
           DateTime aDateTime = new DateTime(year, month, 1);
           return aDateTime;
       }
       // Trả về ngày cuối cùng của tháng.
       public static DateTime GetLastDayInMonth(int year, int month)
       {
           DateTime aDateTime = new DateTime(year, month, 1);
           // Cộng thêm 1 tháng và trừ đi một ngày.
           DateTime retDateTime = aDateTime.AddMonths(1).AddDays(-1);
           return retDateTime;
       }
   }
}
Chạy ví dụ:
4- Đo khoảng thời gian
Bạn có hai đối tượng DateTime, bạn có thể tính được khoảng thời gian giữa 2 đối tượng này, kết quả nhận được là một đối tượng TimeSpan.
IntervalDemo.cs
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DateTimeTutorial
{
   class IntervalDemo
   {
       public static void Main(string[] args)
       {
           // Thời điểm hiện tại.
           DateTime aDateTime = DateTime.Now;
           // Thời điểm năm 2000
           DateTime y2K = new DateTime(2000,1,1);
           // Khoảng thời gian từ năm 2000 tới nay.
           TimeSpan interval = aDateTime.Subtract(y2K);
           Console.WriteLine("Interval from Y2K to Now: " + interval);
           Console.WriteLine("Days: " + interval.Days);
           Console.WriteLine("Hours: " + interval.Hours);
           Console.WriteLine("Minutes: " + interval.Minutes);
           Console.WriteLine("Seconds: " + interval.Seconds);
           Console.Read();
       }
   }
}
Chạy ví dụ:
5- So sánh hai đối tượng DateTime
DateTime có một phương thức tĩnh là Compare. Phương thức dùng để so sánh 2 đối tượng DateTime xem đối tượng nào sớm hơn đối tượng còn lại:
?
1
2
3
4
5
// Nếu giá trị < 0 nghĩa là firstDateTime sớm hơn (đứng trước)
// Nếu giá trị > 0 nghĩa là secondDateTime sớm hơn (đứng trước).
// Nếu giá trị = 0 nghĩa là 2 đối tượng này giống nhau về mặt thời gian.
public static int Compare(DateTime firstDateTime, DateTime secondDateTime);
CompareDateTimeExample.cs
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DateTimeTutorial
{
   class CompareDateTimeExample
   {
       public static void Main(string[] args)
       {
           // Thời điểm hiện tại.
           DateTime firstDateTime = new DateTime(2000, 9, 2);
           DateTime secondDateTime = new DateTime(2011, 1, 20);
           int compare = DateTime.Compare(firstDateTime, secondDateTime);
           Console.WriteLine("First DateTime: " + firstDateTime);
           Console.WriteLine("Second DateTime: " + secondDateTime);
           Console.WriteLine("Compare value: " + compare);// -1
           if (compare < 0)
           {
               // firstDateTime sớm hơn secondDateTime
               Console.WriteLine("firstDateTime is earlier than secondDateTime");
           }
           else
           {
               // firstDateTime muộn hơn secondDateTime
               Console.WriteLine("firstDateTime is laster than secondDateTime");
           }
           Console.Read();
       }
   }
}
Chạy ví dụ:
6- Định dạng tiêu chuẩn DateTime
Định dạng DateTime nghĩa là chuyển đổi đối tượng DateTime thành một string theo một khuôn mẫu nào đó, chẳng hạn theo định dạng ngày/tháng/năm, ... hoặc định dạng dựa vào địa phương (locale) cụ thể.
Phương thức GetDateTimeFormats của DateTime:
Chuyển đổi giá trị của đối tượng này (DateTime) thành một mảng các string đã định dạng theo các chuẩn được hỗ trợ.
AllStandardFormatsDemo.cs
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DateTimeTutorial
{
   class AllStandardFormatsDemo
   {
       public static void Main(string[] args)
       {
           DateTime aDateTime = new DateTime(2015, 12, 20, 11, 30, 50);
           // Một mảng các string kết quả định dạng được hỗ trợ.
           string[] formattedStrings = aDateTime.GetDateTimeFormats();
           foreach (string format in formattedStrings)
           {
               Console.WriteLine(format);
           }
           Console.Read();
       }
   }
}
Chạy ví dụ:
Ví dụ trên liệt kê ra các string sau khi định dạng một đối tượng DateTime theo các tiêu chuẩn có sẵn được hỗ trợ bởi .NET. Để lấy định dạng theo một mẫu cụ thể bạn sử dụng một trong các phương thức sau:
Methods
Description
ToString(String, IFormatProvider)
Chuyển đổi các giá trị của đối tượng DateTime hiện thành chuỗi đại diện tương đương của nó bằng cách sử dụng định dạng quy định (Tham số String) và các thông tin định dạng văn hóa cụ thể (Tham số IFormatProvider).
ToString(IFormatProvider)
Chuyển đổi giá trị của đối tượng DateTime hiện tại thành một string tương ứng với thông tin định dạng văn hóa (culture) cho bởi tham số.
ToString(String)
Chuyển đổi các giá trị của đối tượng DateTime hiện thành một chuỗi tương đương của nó bằng cách sử dụng định dạng quy định và các quy ước định dạng của các nền văn hóa hiện nay.
Ví dụ dưới đây định dạng DateTime theo định dạng 'd', và chỉ định rõ văn hóa trong tham số.
SimpleDateTimeFormat.cs
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
namespace DateTimeTutorial
{
   class SimpleDateTimeFormat
   {
       public static void Main(string[] args)
       {
           DateTime aDateTime = new DateTime(2015, 12, 20, 11, 30, 50);
           Console.WriteLine("DateTime: " + aDateTime);
           String d_formatString = aDateTime.ToString("d");
           Console.WriteLine("Format 'd' : " + d_formatString);
           // Theo văn hóa Mỹ.
           CultureInfo enUs = new CultureInfo("en-US");
           // ==> 12/20/2015 (MM/dd/yyyy)
           Console.WriteLine("Format 'd' & en-US: " + aDateTime.ToString("d", enUs));
           // Theo văn hóa Việt Nam.
           CultureInfo viVn = new CultureInfo("vi-VN");
           // ==> 12/20/2015 (dd/MM/yyyy)
           Console.WriteLine("Format 'd' & vi-VN: " + aDateTime.ToString("d", viVn));
           Console.Read();
       }
   }
}
Chạy ví dụ:
Các ký tự định dạng tiêu chuẩn.
Code
Pattern
"d"
Ngày tháng năm ngắn
"D"
Ngày tháng năm dài
"f"
Ngày tháng năm dài, thời gian ngắn
"F"
Ngày tháng năm dài, thời gian dài.
"g"
Ngày tháng thời gian nói chung. Thời gian ngắn.
"G"
Ngày tháng thời gian nói chung. Thời gian dài.
"M", 'm"
Tháng/ngày.
"O", "o"
Round-trip date/time.
"R", "r"
RFC1123
"s"
Ngày tháng thời gian có thể sắp xếp
"t"
Thời gian ngắn
"T"
Thời gian dài
"u"
Ngày tháng năm có thể sắp xếp phổ biến (Universal sortable date time).
"U"
Ngày tháng năm thời gian dài, phổ biến (Universal full date time).
"Y", "y"
Năm tháng
SimpleDateTimeFormatAll.cs
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DateTimeTutorial
{
   class SimpleDateTimeFormatAll
   {
       public static void Main(string[] args)
       {
           char[] formats = {'d', 'D','f','F','g','G','M', 'm','O', 'o','R', 'r','s','t','T','u','U','Y', 'y'};
           DateTime aDateTime = new DateTime(2015, 12, 20, 11, 30, 50);
           foreach (char ch in formats)
           {
               Console.WriteLine("\n======" + ch + " ========\n");
               // Một mảng các string kết quả định dạng được hỗ trợ.
               string[] formattedStrings = aDateTime.GetDateTimeFormats(ch);
               foreach (string format in formattedStrings)
               {
                   Console.WriteLine(format);
               }
           }
           Console.ReadLine();
       }
   }
}
Chạy ví dụ:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
======d ========
12/20/2015
12/20/15
12/20/15
12/20/2015
15/12/20
2015-12-20
20-Dec-15
======D ========
Sunday, December 20, 2015
December 20, 2015
Sunday, 20 December, 2015
20 December, 2015
======f ========
Sunday, December 20, 2015 11:30 AM
Sunday, December 20, 2015 11:30 AM
Sunday, December 20, 2015 11:30
Sunday, December 20, 2015 11:30
December 20, 2015 11:30 AM
December 20, 2015 11:30 AM
December 20, 2015 11:30
December 20, 2015 11:30
Sunday, 20 December, 2015 11:30 AM
Sunday, 20 December, 2015 11:30 AM
Sunday, 20 December, 2015 11:30
Sunday, 20 December, 2015 11:30
20 December, 2015 11:30 AM
20 December, 2015 11:30 AM
20 December, 2015 11:30
20 December, 2015 11:30
======F ========
Sunday, December 20, 2015 11:30:50 AM
Sunday, December 20, 2015 11:30:50 AM
Sunday, December 20, 2015 11:30:50
Sunday, December 20, 2015 11:30:50
December 20, 2015 11:30:50 AM
December 20, 2015 11:30:50 AM
December 20, 2015 11:30:50
December 20, 2015 11:30:50
Sunday, 20 December, 2015 11:30:50 AM
Sunday, 20 December, 2015 11:30:50 AM
Sunday, 20 December, 2015 11:30:50
Sunday, 20 December, 2015 11:30:50
20 December, 2015 11:30:50 AM
20 December, 2015 11:30:50 AM
20 December, 2015 11:30:50
20 December, 2015 11:30:50
======g ========
12/20/2015 11:30 AM
12/20/2015 11:30 AM
12/20/2015 11:30
12/20/2015 11:30
12/20/15 11:30 AM
12/20/15 11:30 AM
12/20/15 11:30
12/20/15 11:30
12/20/15 11:30 AM
12/20/15 11:30 AM
12/20/15 11:30
12/20/15 11:30
12/20/2015 11:30 AM
12/20/2015 11:30 AM
12/20/2015 11:30
12/20/2015 11:30
15/12/20 11:30 AM
15/12/20 11:30 AM
15/12/20 11:30
15/12/20 11:30
2015-12-20 11:30 AM
2015-12-20 11:30 AM
2015-12-20 11:30
2015-12-20 11:30
20-Dec-15 11:30 AM
20-Dec-15 11:30 AM
20-Dec-15 11:30
20-Dec-15 11:30
======G ========
12/20/2015 11:30:50 AM
12/20/2015 11:30:50 AM
12/20/2015 11:30:50
12/20/2015 11:30:50
12/20/15 11:30:50 AM
12/20/15 11:30:50 AM
12/20/15 11:30:50
12/20/15 11:30:50
12/20/15 11:30:50 AM
12/20/15 11:30:50 AM
12/20/15 11:30:50
12/20/15 11:30:50
12/20/2015 11:30:50 AM
12/20/2015 11:30:50 AM
12/20/2015 11:30:50
12/20/2015 11:30:50
15/12/20 11:30:50 AM
15/12/20 11:30:50 AM
15/12/20 11:30:50
15/12/20 11:30:50
2015-12-20 11:30:50 AM
2015-12-20 11:30:50 AM
2015-12-20 11:30:50
2015-12-20 11:30:50
20-Dec-15 11:30:50 AM
20-Dec-15 11:30:50 AM
20-Dec-15 11:30:50
20-Dec-15 11:30:50
======M ========
December 20
======m ========
December 20
======O ========
2015-12-20T11:30:50.0000000
======o ========
2015-12-20T11:30:50.0000000
======R ========
Sun, 20 Dec 2015 11:30:50 GMT
======r ========
Sun, 20 Dec 2015 11:30:50 GMT
======s ========
2015-12-20T11:30:50
======t ========
11:30 AM
11:30 AM
11:30
11:30
======T ========
11:30:50 AM
11:30:50 AM
11:30:50
11:30:50
======u ========
2015-12-20 11:30:50Z
======U ========
Sunday, December 20, 2015 4:30:50 AM
Sunday, December 20, 2015 04:30:50 AM
Sunday, December 20, 2015 4:30:50
Sunday, December 20, 2015 04:30:50
December 20, 2015 4:30:50 AM
December 20, 2015 04:30:50 AM
December 20, 2015 4:30:50
December 20, 2015 04:30:50
Sunday, 20 December, 2015 4:30:50 AM
Sunday, 20 December, 2015 04:30:50 AM
Sunday, 20 December, 2015 4:30:50
Sunday, 20 December, 2015 04:30:50
20 December, 2015 4:30:50 AM
20 December, 2015 04:30:50 AM
20 December, 2015 4:30:50
20 December, 2015 04:30:50
======Y ========
December, 2015
======y ========
December, 2015
7- Tùy biến định dạng DateTime

File đính kèm:

  • docxtai_lieu_lap_trinh_c_va_cau_hinh_visual_studio_2013.docx