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
Tìm hiểu về Lớp Hashtable 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: 177605" data-attributes="member: 165510"><p><a href="https://vnkienthuc.com/forums/code.377/" target="_blank"><span style="font-size: 18px">Lớp Hashtable</span></a><span style="font-size: 18px"> trong C# sử dụng một cặp key-value để truy cập các phần tử trong collection này.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Một Hash Table được sử dụng khi bạn cần truy cập các phần tử bởi sử dụng key, và bạn có thể nhận diện một giá trị key hữu ích. Mỗi item trong Hash Table có <a href="https://vnkienthuc.com/threads/tim-hieu-ve-lop-stack-trong-c.77362/" target="_blank">một cặp key và value</a>. Key được sử dụng để truy cập các item trong dạng collection này.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #0000ff"><strong>Phương thức và Thuộc tính của lớp Hashtable trong C#</strong></span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong>- Dưới đây liệt kê các thuộc tính được sử dụng phổ biến của lớp Hashtable trong C#:</strong></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">1. Count:</span> Lấy số cặp key và value được chứa trong Hashtable</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">2. IsFixedSize:</span> Lấy một giá trị chỉ rằng có hay không Hashtable là có một kích cỡ cố định</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">3. IsReadOnly:</span> Lấy một giá trị chỉ rằng có hay không Hashtable là read-only</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">4. Item: </span>Lấy hoặc thiết lập giá trị được liên kết với key đã xác định</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">5. Keys:</span> Lấy một ICollection chứa các key trong Hashtable</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">6. Values: </span>Lấy một ICollection chứa các value trong Hashtable</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong>- Dưới đây liệt kê các phương thức được sử dụng phổ biến của lớp Hashtable trong C#:</strong></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">1 public virtual void Add(object key, object value);</span></span></p><p><span style="font-size: 18px">Thêm một phần tử với key và value đã xác định vào trong Hashtable</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">2 public virtual void Clear();</span></span></p><p><span style="font-size: 18px">Gỡ bỏ tất cả phần tử từ Hashtable</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">3 public virtual bool ContainsKey(object key);</span></span></p><p><span style="font-size: 18px">Xác định có hay không Hashtable là chứa một key cụ thể</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">4 public virtual bool ContainsValue(object value);</span></span></p><p><span style="font-size: 18px">Xác định có hay không Hashtable là chứa một value cụ thể</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">5 public virtual void Remove(object key);</span></span></p><p><span style="font-size: 18px">Gỡ bỏ phần tử với key đã xác định từ Hashtable</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong>Ví dụ</strong></span></p><p><span style="font-size: 18px">Dưới đây là ví dụ minh họa các thuộc tính và phương thức của lớp Hashtable trong C#:</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">using System;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">using System.Collections;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">namespace VietJackCsharp</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">{</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> class TestCsharp</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> static void Main(string[] args)</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Vi du minh hoa Hashtable trong C#");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("-------------------------------------");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Hashtable ht = new Hashtable();</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> ht.Add("001", "Tran Nam");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> ht.Add("002", "Minh Chinh");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> ht.Add("003", "Thu Phuong");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> ht.Add("004", "Hong Ngoc");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> ht.Add("005", "Lam Truong");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> ht.Add("006", "Thuy Hang");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> ht.Add("007", "Minh Hieu");</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> if (ht.ContainsValue("Phuong Thanh"))</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Sinh vien co ten tren da ton tai trong danh sach");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> else</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> ht.Add("008", "Phuong Thanh");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> // lay tap hop cac key</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> ICollection key = ht.Keys;</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> foreach (string k in key)</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine(k + ": " + ht[k]);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.ReadKey();</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">}</span></em></span></p><p><span style="font-size: 18px">Nếu bạn không<a href="https://vnkienthuc.com/threads/tim-hieu-ve-generic-trong-c.77387/" target="_blank"> sử dụng lệnh </a>Console.ReadKey(); thì chương trình sẽ chạy và kết thúc luôn (nhanh quá đến nỗi bạn không kịp nhìn kết quả). Lệnh này cho phép chúng ta nhìn kết quả một cách rõ ràng hơn.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau:</span></p><p><span style="font-size: 18px"></span></p><p style="text-align: center"><span style="font-size: 18px"><img src="https://vietjack.com/csharp/images/hashtable.PNG" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></span></p> <p style="text-align: center"><span style="font-size: 18px"></span></p> <p style="text-align: right"><span style="font-size: 18px">Nguồn: vietjack.com</span></p></blockquote><p></p>
[QUOTE="uocmo_kchodoi, post: 177605, member: 165510"] [URL='https://vnkienthuc.com/forums/code.377/'][SIZE=5]Lớp Hashtable[/SIZE][/URL][SIZE=5] trong C# sử dụng một cặp key-value để truy cập các phần tử trong collection này. Một Hash Table được sử dụng khi bạn cần truy cập các phần tử bởi sử dụng key, và bạn có thể nhận diện một giá trị key hữu ích. Mỗi item trong Hash Table có [URL='https://vnkienthuc.com/threads/tim-hieu-ve-lop-stack-trong-c.77362/']một cặp key và value[/URL]. Key được sử dụng để truy cập các item trong dạng collection này. [COLOR=#0000ff][B]Phương thức và Thuộc tính của lớp Hashtable trong C#[/B][/COLOR] [B]- Dưới đây liệt kê các thuộc tính được sử dụng phổ biến của lớp Hashtable trong C#:[/B] [COLOR=#ff8000]1. Count:[/COLOR] Lấy số cặp key và value được chứa trong Hashtable [COLOR=#ff8000]2. IsFixedSize:[/COLOR] Lấy một giá trị chỉ rằng có hay không Hashtable là có một kích cỡ cố định [COLOR=#ff8000]3. IsReadOnly:[/COLOR] Lấy một giá trị chỉ rằng có hay không Hashtable là read-only [COLOR=#ff8000]4. Item: [/COLOR]Lấy hoặc thiết lập giá trị được liên kết với key đã xác định [COLOR=#ff8000]5. Keys:[/COLOR] Lấy một ICollection chứa các key trong Hashtable [COLOR=#ff8000]6. Values: [/COLOR]Lấy một ICollection chứa các value trong Hashtable [B]- Dưới đây liệt kê các phương thức được sử dụng phổ biến của lớp Hashtable trong C#:[/B] [COLOR=#ff8000]1 public virtual void Add(object key, object value);[/COLOR] Thêm một phần tử với key và value đã xác định vào trong Hashtable [COLOR=#ff8000]2 public virtual void Clear();[/COLOR] Gỡ bỏ tất cả phần tử từ Hashtable [COLOR=#ff8000]3 public virtual bool ContainsKey(object key);[/COLOR] Xác định có hay không Hashtable là chứa một key cụ thể [COLOR=#ff8000]4 public virtual bool ContainsValue(object value);[/COLOR] Xác định có hay không Hashtable là chứa một value cụ thể [COLOR=#ff8000]5 public virtual void Remove(object key);[/COLOR] Gỡ bỏ phần tử với key đã xác định từ Hashtable [B]Ví dụ[/B] Dưới đây là ví dụ minh họa các thuộc tính và phương thức của lớp Hashtable trong C#: [I][COLOR=#5900b3]using System;[/COLOR][/I] [I][COLOR=#5900b3]using System.Collections;[/COLOR][/I] [I][COLOR=#5900b3]namespace VietJackCsharp[/COLOR][/I] [I][COLOR=#5900b3]{[/COLOR][/I] [I][COLOR=#5900b3] class TestCsharp[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] static void Main(string[] args)[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Vi du minh hoa Hashtable trong C#");[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("-------------------------------------");[/COLOR][/I] [I][COLOR=#5900b3] Hashtable ht = new Hashtable();[/COLOR][/I] [I][COLOR=#5900b3] ht.Add("001", "Tran Nam");[/COLOR][/I] [I][COLOR=#5900b3] ht.Add("002", "Minh Chinh");[/COLOR][/I] [I][COLOR=#5900b3] ht.Add("003", "Thu Phuong");[/COLOR][/I] [I][COLOR=#5900b3] ht.Add("004", "Hong Ngoc");[/COLOR][/I] [I][COLOR=#5900b3] ht.Add("005", "Lam Truong");[/COLOR][/I] [I][COLOR=#5900b3] ht.Add("006", "Thuy Hang");[/COLOR][/I] [I][COLOR=#5900b3] ht.Add("007", "Minh Hieu");[/COLOR][/I] [I][COLOR=#5900b3] if (ht.ContainsValue("Phuong Thanh"))[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Sinh vien co ten tren da ton tai trong danh sach");[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] else[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] ht.Add("008", "Phuong Thanh");[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] // lay tap hop cac key[/COLOR][/I] [I][COLOR=#5900b3] ICollection key = ht.Keys;[/COLOR][/I] [I][COLOR=#5900b3] foreach (string k in key)[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine(k + ": " + ht[k]);[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] Console.ReadKey();[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3]}[/COLOR][/I] Nếu bạn không[URL='https://vnkienthuc.com/threads/tim-hieu-ve-generic-trong-c.77387/'] sử dụng lệnh [/URL]Console.ReadKey(); thì chương trình sẽ chạy và kết thúc luôn (nhanh quá đến nỗi bạn không kịp nhìn kết quả). Lệnh này cho phép chúng ta nhìn kết quả một cách rõ ràng hơn. Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau: [/SIZE] [CENTER][SIZE=5][IMG]https://vietjack.com/csharp/images/hashtable.PNG[/IMG] [/SIZE][/CENTER] [RIGHT][SIZE=5]Nguồn: vietjack.com[/SIZE][/RIGHT] [/QUOTE]
Tên
Mã xác nhận
Gửi trả lời
CÔNG NGHỆ
Công Nghệ Thông Tin
Code
Tìm hiểu về Lớp Hashtable trong C#
Top