VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > C#教程 >
  • C# |检查散列集合是否包含指定的元素

C# |检查散列集合是否包含指定的元素

原文:https://www . geesforgeks . org/c-sharp-check-if-a-hashset-contains-specific-element/

一个 HashSet 是独特元素的无序集合。在 系统中找到。Collections.Generic 命名空间。它用于我们希望防止在集合中插入重复项的情况。就性能而言,与列表相比更好。 HashSet 。Contains(T)方法 用于检查一个 HashSet 对象是否包含指定的元素。

语法:

mySet.Contains(T item);

这里, mySet 是 HashSet 的名称,是 HashSet 对象中需要定位的元素。

返回类型:如果 HashSet 对象包含指定元素,则该方法返回true;否则,为假

下面给出的是一些例子来更好地理解实现:

例子 1:

// C# code to check if a HashSet
// contains the specified element
using System;
using System.Collections.Generic;

class GFG {

    // Driver code
    public static void Main()
    {

        // Creating a HashSet of strings
        HashSet<string> mySet = new HashSet<string>();

        // Inserting elements in HashSet
        mySet.Add("DS");
        mySet.Add("C++");
        mySet.Add("Java");
        mySet.Add("JavaScript");

        // Check if a HashSet contains
        // the specified element
        if (mySet.Contains("Java"))
            Console.WriteLine("Required Element is present");
        else
            Console.WriteLine("Required Element is not present");
    }
}

输出:

Required Element is present

例 2:

// C# code to check if a HashSet
// contains the specified element
using System;
using System.Collections.Generic;

class GFG {

    // Driver code
    public static void Main()
    {

        // Creating a HashSet of integers
        HashSet<int> mySet = new HashSet<int>();

        // Inserting elements in HashSet
        for (int i = 0; i < 5; i++) {
            mySet.Add(i * 2);
        }

        // Check if a HashSet contains
        // the specified element
        if (mySet.Contains(5))
            Console.WriteLine("Required Element is present");
        else
            Console.WriteLine("Required Element is not present");
    }
}

Output:

Required Element is not present

参考:

  • https://docs . Microsoft . com/en-us/dotnet/API/system . collections . generic . hashset-1 . contains?视图=netframework-4.7.2


相关教程