目錄
How to Check if Value Exists in Hashtable?
ContainsValue方法
Example
範例
輸出
Output
結論
首頁 後端開發 C#.Net教程 C# 程式檢查雜湊表中是否存在值

C# 程式檢查雜湊表中是否存在值

Aug 29, 2023 am 10:37 AM

C# 程序检查哈希表中是否存在值

The hashtable is an organized collection of key-value pairs wherein the keys are arranged as per the hash code of the key calculated using the hash function. While the keys shull be non-n and unique in a hashtable, the values can be null and duplicate.

哈希表中的元素是透過鍵來存取的。在C#中,類別"Hashtable"表示雜湊表集合。這個類別提供了各種屬性和方法,我們可以使用它們來執行操作並存取哈希表中的資料。

在本文中,我們將看到如何確定雜湊表中是否存在特定的值。

How to Check if Value Exists in Hashtable?

#要檢查雜湊表中是否存在某個值,我們可以利用Hashtable類別提供的「containsValue」方法。此方法傳回布林值,指示指定的值是否存在於雜湊表中。

Let’s have a look at the method first before proceeding with programming examples.

ContainsValue方法

Syntax − public virtual bool ContainsValue (object value);

Description − used to find if the Hashtable contains a specified value.

參數 - 要在雜湊表中定位的值(物件)。可以是空值。

傳回值  Boolean: true=> 雜湊表中包含指定值的元素。

False=> 哈希表不包含指定值的元素。

命名空間 - System.Collections

#Let’s now see few programming examples where we check if the specified value is present in the hashtable or not.

Example

的中文翻譯為:

範例

檢查值是否存在於雜湊表中的第一個程式如下所示。

using System;
using System.Collections;
class Program {
   public static void Main(){
      // Create a Hashtable
      Hashtable langCodes = new Hashtable();
      
      // Add elements to the Hashtable
      langCodes.Add("C++", "CPlusPlus");
      langCodes.Add("C#", "CSharp");
      langCodes.Add("Java", "Java");
      langCodes.Add("PL", "Perl");
      
      // use ContainsValue method to check if the HashTable contains the 
      //required Value or not.
      if (langCodes.ContainsValue("CSharp"))
         Console.WriteLine("langCodes hashtable contain the Value = CSharp");
      else
         Console.WriteLine("langCodes hashtable doesn't contain the Value = CSharp");
   }
}
登入後複製

上述程式宣告了一個langCodes雜湊表,其中包含語言程式碼和語言名稱作為鍵和值。接下來,我們有一個「if」結構,檢查雜湊表中是否存在值「CSharp」。如果存在,它將相應地顯示訊息。

輸出

程式的輸出如下所示。

#
langCodes hashtable contain the Value = CSharp
登入後複製

由於hashtable中存在value = CSharp,所以程式會顯示上述訊息。

Now change the argument of the ContainsValue method to “C#” i.e. the key instead of value.

if (langCodes.ContainsValue("C#"))
登入後複製

現在用這個變更來執行上面的程式。

輸出

在這種情況下,由於雜湊表中不存在值“C#”,程式將傳回適當的訊息。因此,我們將得到−

#
langCodes hashtable doesn't contain the Value = CSharp
登入後複製

Example

的中文翻譯為:

範例

Now let’s look at the following example.

using System;
using System.Collections;
class Program {
   public static void Main() {
      // Create a Hashtable
      Hashtable NumberNames = new Hashtable();
      // Add elements to the Hashtable
      NumberNames.Add(1, "One");
      NumberNames.Add(3, "Three");
      NumberNames.Add(5, "Five");
      NumberNames.Add(7, "Seven");
      // use ContainsValue method to check if the HashTable contains the
      //required Value or not.
      if (NumberNames.ContainsValue("Two"))
         Console.WriteLine("NumberNames hashtable contain the Value = Two");
      else
         Console.WriteLine("NumberNames hashtable doesn't contain the Value = Two");
      if (NumberNames.ContainsValue("Five"))
         Console.WriteLine("NumberNames hashtable contain the Value = Five");
      else
         Console.WriteLine("NumberNames hashtable doesn't contain the Value = Five");
   }
}
登入後複製

這個程式有一個名為「NumberNames」的表,以數字作為鍵,對應的名稱作為值。在這裡,我們首先使用“containsKey()”方法檢查雜湊表是否包含值= Two。接下來,我們使用containsKey()方法檢查值=“Five”。 

Output

The output for the program is shown below.

#
NumberNames hashtable doesn't contain the Value = Two
NumberNames hashtable contain the Value = Five
登入後複製

從程式中定義的雜湊表中可以看出,它不包含值 = Two,但包含值 = Five。因此,程序適當地給出了相應的訊息。

結論

Thus using the “containsKey()” method of the Hashtable class in C#, we can determine if an element with a specific value is present in the hashtable or not. Depending on whether the value is present or not, we can output the appropriate results, or in the case of a complex program, proceed with the appropriate code.

當我們需要檢查hashtable中是否存在指定的值,並採取相應的行動時,方法「containsKey()」就變得非常有用。

以上是C# 程式檢查雜湊表中是否存在值的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1658
14
CakePHP 教程
1415
52
Laravel 教程
1309
25
PHP教程
1257
29
C# 教程
1231
24
C#.NET面試問題和答案:提高您的專業知識 C#.NET面試問題和答案:提高您的專業知識 Apr 07, 2025 am 12:01 AM

C#.NET面試問題和答案包括基礎知識、核心概念和高級用法。 1)基礎知識:C#是微軟開發的面向對象語言,主要用於.NET框架。 2)核心概念:委託和事件允許動態綁定方法,LINQ提供強大查詢功能。 3)高級用法:異步編程提高響應性,表達式樹用於動態代碼構建。

測試C#.NET應用程序:單元,集成和端到端測試 測試C#.NET應用程序:單元,集成和端到端測試 Apr 09, 2025 am 12:04 AM

C#.NET應用的測試策略包括單元測試、集成測試和端到端測試。 1.單元測試確保代碼的最小單元獨立工作,使用MSTest、NUnit或xUnit框架。 2.集成測試驗證多個單元組合的功能,常用模擬數據和外部服務。 3.端到端測試模擬用戶完整操作流程,通常使用Selenium進行自動化測試。

C#.NET:探索核心概念和編程基礎知識 C#.NET:探索核心概念和編程基礎知識 Apr 10, 2025 am 09:32 AM

C#是一種現代、面向對象的編程語言,由微軟開發並作為.NET框架的一部分。 1.C#支持面向對象編程(OOP),包括封裝、繼承和多態。 2.C#中的異步編程通過async和await關鍵字實現,提高應用的響應性。 3.使用LINQ可以簡潔地處理數據集合。 4.常見錯誤包括空引用異常和索引超出範圍異常,調試技巧包括使用調試器和異常處理。 5.性能優化包括使用StringBuilder和避免不必要的裝箱和拆箱。

從網絡到桌面:C#.NET的多功能性 從網絡到桌面:C#.NET的多功能性 Apr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

c#.net的持續相關性:查看當前用法 c#.net的持續相關性:查看當前用法 Apr 16, 2025 am 12:07 AM

C#.NET依然重要,因為它提供了強大的工具和庫,支持多種應用開發。 1)C#結合.NET框架,使開發高效便捷。 2)C#的類型安全和垃圾回收機制增強了其優勢。 3).NET提供跨平台運行環境和豐富的API,提升了開發靈活性。

高級C#.NET教程:ACE您的下一次高級開發人員面試 高級C#.NET教程:ACE您的下一次高級開發人員面試 Apr 08, 2025 am 12:06 AM

C#高級開發者面試需要掌握異步編程、LINQ、.NET框架內部工作原理等核心知識。 1.異步編程通過async和await簡化操作,提升應用響應性。 2.LINQ以SQL風格操作數據,需注意性能。 3..NET框架的CLR管理內存,垃圾回收需謹慎使用。

c#.net適合您嗎?評估其適用性 c#.net適合您嗎?評估其適用性 Apr 13, 2025 am 12:03 AM

c#.netissutableforenterprise-levelapplications withemofrosoftecosystemdueToItsStrongTyping,richlibraries,androbustperraries,androbustperformance.however,itmaynotbeidealfoross-platement forment forment forment forvepentment offependment dovelopment toveloperment toveloperment whenrawspeedsportor whenrawspeedseedpolitical politionalitable,

C#.NET與未來:適應新技術 C#.NET與未來:適應新技術 Apr 14, 2025 am 12:06 AM

C#和.NET通過不斷的更新和優化,適應了新興技術的需求。 1)C#9.0和.NET5引入了記錄類型和性能優化。 2).NETCore增強了雲原生和容器化支持。 3)ASP.NETCore與現代Web技術集成。 4)ML.NET支持機器學習和人工智能。 5)異步編程和最佳實踐提升了性能。

See all articles