Trang chủ
Bài viết mới
Diễn đàn
Bài mới trên hồ sơ
Hoạt động mới nhất
VIDEO
Mùa Tết
Văn Học Trẻ
Văn Học News
Media
New media
New comments
Search media
Đại Học
Đại cương
Chuyên ngành
Triết học
Kinh tế
KHXH & NV
Công nghệ thông tin
Khoa học kĩ thuật
Luận văn, tiểu luận
Phổ Thông
Lớp 12
Ngữ văn 12
Lớp 11
Ngữ văn 11
Lớp 10
Ngữ văn 10
LỚP 9
Ngữ văn 9
Lớp 8
Ngữ văn 8
Lớp 7
Ngữ văn 7
Lớp 6
Ngữ văn 6
Tiểu học
Thành viên
Thành viên trực tuyến
Bài mới trên hồ sơ
Tìm trong hồ sơ cá nhân
Credits
Transactions
Xu: 0
Đăng nhập
Đăng ký
Có gì mới?
Tìm kiếm
Tìm kiếm
Chỉ tìm trong tiêu đề
Bởi:
Hoạt động mới nhất
Đăng ký
Menu
Đăng nhập
Đăng ký
Install the app
Cài đặt
Chào mừng Bạn tham gia Diễn Đàn VNKienThuc.com -
Định hướng Forum
Kiến Thức
- HÃY TẠO CHỦ ĐỀ KIẾN THỨC HỮU ÍCH VÀ CÙNG NHAU THẢO LUẬN Kết nối:
VNK X
-
VNK groups
| Nhà Tài Trợ:
BhnongFood X
-
Bhnong groups
-
Đặt mua Bánh Bhnong
CÔNG NGHỆ
Công Nghệ Thông Tin
Code
Hướng dẫn chuyển đổi kiểu dữ liệu trong C#
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Trả lời chủ đề
Nội dung
<blockquote data-quote="uocmo_kchodoi" data-source="post: 177504" data-attributes="member: 165510"><p>Chuyển đổi kiểu dữ liệu trong C# là biến đổi một kiểu dữ liệu này thành kiểu dữ liệu khác. Nó còn được gọi là Ép kiểu. Trong C#, ép kiểu có hai mẫu sau:</p><ul> <li data-xf-list-type="ul">Chuyển đổi kiểu ngầm định (implicit) - Việc chuyển đổi này được thực hiện bởi C# theo một phương thức an toàn kiểu (type-safe). Ví dụ: việc chuyển đổi từ các lớp kế thừa thành các lớp cơ sở.<br /> <br /> </li> <li data-xf-list-type="ul">Chuyển đổi kiểu tường minh (explicit) - Việc chuyển đổi này được thực hiện một cách rõ ràng bởi người dùng bằng việc sử dụng các hàm được định nghĩa trước. Các chuyển đổi kiểu tường minh cần một toán tử cast.</li> </ul><p>Ví dụ sau minh họa một sự chuyển đổi kiểu tường minh trong C#:</p><p></p><p>using System;</p><p><em><span style="color: #5900b3">namespace VietJackCsharp</span></em></p><p><em><span style="color: #5900b3">{</span></em></p><p><em><span style="color: #5900b3"> class TestCsharp</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> static void Main(string[] args)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Chuyen doi kieu du lieu trong C#");</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("-------------------------------");</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> double d = 5678.74;</span></em></p><p><em><span style="color: #5900b3"> int i;</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> // cast kieu du lieu double thanh kieu du lieu int.</span></em></p><p><em><span style="color: #5900b3"> i = (int)d;</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Gia tri cua i = " +i);</span></em></p><p><em><span style="color: #5900b3"> Console.ReadKey();</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3">}</span></em></p><p>Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau:</p><p></p><p><img src="https://vietjack.com/csharp/images/chuyen-doi-kieu.PNG" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></p><p></p><p><span style="font-size: 18px"><strong>Phương thức Chuyển đổi kiểu dữ liệu trong C#</strong></span></p><p><span style="font-size: 18px"></span></p><p>C# cung cấp các phương thức chuyển đổi kiểu có sẵn được liệt kê trong bảng sau:</p><p></p><p>1 ToBoolean: Chuyển đổi một kiểu thành một giá trị Boolean, nếu có thể</p><p></p><p>2 ToByte: Chuyển đổi một kiểu thành một byte</p><p></p><p>3 ToChar: Chuyển đổi một kiểu thành một Unicode character, nếu có thể</p><p></p><p>4 ToDateTime: Chuyển đổi một kiểu (kiểu integer hoặc string) thành các cấu trúc date-time</p><p></p><p>5 ToDecimal: Chuyển đổi một kiểu số thực hoặc số nguyên thành một kiểu thập phân</p><p></p><p>6 ToDouble: Chuyển đổi một kiểu thành một kiểu double</p><p></p><p>7 ToInt16: Chuyển đổi một kiểu thành một 16-bit integer</p><p></p><p>8 ToInt32: Chuyển đổi một kiểu thành một 32-bit integer</p><p></p><p>9 ToInt64: Chuyển đổi một kiểu thành một 64-bit integer</p><p></p><p>10 ToSbyte: Chuyển đổi một kiểu thành một kiểu signed byte</p><p></p><p>11 ToSingle: Chuyển đổi một kiểu thành một số small floating point</p><p></p><p>12 ToString: Chuyển đổi một kiểu thành một string</p><p></p><p>13 ToType: Chuyển đổi một kiểu thành một kiểu đã xác định</p><p></p><p>14 ToUInt16: Chuyển đổi một kiểu thành một kiểu unsigned int</p><p></p><p>15 ToUInt32: Chuyển đổi một kiểu thành một kiểu unsigned long</p><p></p><p>16 ToUInt64: Chuyển đổi một kiểu thành một unsigned big integer</p><p></p><p>Ví dụ sau minh họa cách chuyển đổi các kiểu dữ liệu đa dạng thành kiểu dữ liệu string trong C#:</p><p></p><p><em><span style="color: #5900b3">using System;</span></em></p><p><em><span style="color: #5900b3">namespace VietJackCsharp</span></em></p><p><em><span style="color: #5900b3">{</span></em></p><p><em><span style="color: #5900b3"> class TestCsharp</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> static void Main(string[] args)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Chuyen doi kieu du lieu trong C#");</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("-------------------------------");</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> int i = 75;</span></em></p><p><em><span style="color: #5900b3"> float f = 53.005f;</span></em></p><p><em><span style="color: #5900b3"> double d = 2345.7652;</span></em></p><p><em><span style="color: #5900b3"> bool b = true;</span></em></p><p><em><span style="color: #5900b3"> //su dung phuong thuc ToString()</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine(i.ToString());</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine(f.ToString());</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine(d.ToString());</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine(b.ToString());</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> Console.ReadKey();</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3">}</span></em></p><p>Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau:</p><p></p><p><img src="https://vietjack.com/csharp/images/chuyen-doi-kieu-du-lieu.PNG" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></p><p></p><p></p><p>Trên đây là bài hướng dẫn cách chuyển kiểu dữ liệu trong C#. Thật đơn giản phải không nào. Chúc các bạn thành công!</p><p></p><p>Nguồn: vietjack.com</p></blockquote><p></p>
[QUOTE="uocmo_kchodoi, post: 177504, member: 165510"] Chuyển đổi kiểu dữ liệu trong C# là biến đổi một kiểu dữ liệu này thành kiểu dữ liệu khác. Nó còn được gọi là Ép kiểu. Trong C#, ép kiểu có hai mẫu sau: [LIST] [*]Chuyển đổi kiểu ngầm định (implicit) - Việc chuyển đổi này được thực hiện bởi C# theo một phương thức an toàn kiểu (type-safe). Ví dụ: việc chuyển đổi từ các lớp kế thừa thành các lớp cơ sở. [*]Chuyển đổi kiểu tường minh (explicit) - Việc chuyển đổi này được thực hiện một cách rõ ràng bởi người dùng bằng việc sử dụng các hàm được định nghĩa trước. Các chuyển đổi kiểu tường minh cần một toán tử cast. [/LIST] Ví dụ sau minh họa một sự chuyển đổi kiểu tường minh trong C#: using System; [I][COLOR=#5900b3]namespace VietJackCsharp { class TestCsharp { static void Main(string[] args) { Console.WriteLine("Chuyen doi kieu du lieu trong C#"); Console.WriteLine("-------------------------------"); double d = 5678.74; int i; // cast kieu du lieu double thanh kieu du lieu int. i = (int)d; Console.WriteLine("Gia tri cua i = " +i); Console.ReadKey(); } } }[/COLOR][/I] Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau: [IMG]https://vietjack.com/csharp/images/chuyen-doi-kieu.PNG[/IMG] [SIZE=5][B]Phương thức Chuyển đổi kiểu dữ liệu trong C#[/B] [/SIZE] C# cung cấp các phương thức chuyển đổi kiểu có sẵn được liệt kê trong bảng sau: 1 ToBoolean: Chuyển đổi một kiểu thành một giá trị Boolean, nếu có thể 2 ToByte: Chuyển đổi một kiểu thành một byte 3 ToChar: Chuyển đổi một kiểu thành một Unicode character, nếu có thể 4 ToDateTime: Chuyển đổi một kiểu (kiểu integer hoặc string) thành các cấu trúc date-time 5 ToDecimal: Chuyển đổi một kiểu số thực hoặc số nguyên thành một kiểu thập phân 6 ToDouble: Chuyển đổi một kiểu thành một kiểu double 7 ToInt16: Chuyển đổi một kiểu thành một 16-bit integer 8 ToInt32: Chuyển đổi một kiểu thành một 32-bit integer 9 ToInt64: Chuyển đổi một kiểu thành một 64-bit integer 10 ToSbyte: Chuyển đổi một kiểu thành một kiểu signed byte 11 ToSingle: Chuyển đổi một kiểu thành một số small floating point 12 ToString: Chuyển đổi một kiểu thành một string 13 ToType: Chuyển đổi một kiểu thành một kiểu đã xác định 14 ToUInt16: Chuyển đổi một kiểu thành một kiểu unsigned int 15 ToUInt32: Chuyển đổi một kiểu thành một kiểu unsigned long 16 ToUInt64: Chuyển đổi một kiểu thành một unsigned big integer Ví dụ sau minh họa cách chuyển đổi các kiểu dữ liệu đa dạng thành kiểu dữ liệu string trong C#: [I][COLOR=#5900b3]using System; namespace VietJackCsharp { class TestCsharp { static void Main(string[] args) { Console.WriteLine("Chuyen doi kieu du lieu trong C#"); Console.WriteLine("-------------------------------"); int i = 75; float f = 53.005f; double d = 2345.7652; bool b = true; //su dung phuong thuc ToString() Console.WriteLine(i.ToString()); Console.WriteLine(f.ToString()); Console.WriteLine(d.ToString()); Console.WriteLine(b.ToString()); Console.ReadKey(); } } }[/COLOR][/I] Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau: [IMG]https://vietjack.com/csharp/images/chuyen-doi-kieu-du-lieu.PNG[/IMG] Trên đây là bài hướng dẫn cách chuyển kiểu dữ liệu trong C#. Thật đơn giản phải không nào. Chúc các bạn thành công! Nguồn: vietjack.com [/QUOTE]
Tên
Mã xác nhận
Gửi trả lời
CÔNG NGHỆ
Công Nghệ Thông Tin
Code
Hướng dẫn chuyển đổi kiểu dữ liệu trong C#
Top