C#|检查哈希集是否包含指定的元素

A. 哈希集 是一个无序的 独特元素 .在 系统收藏。通用的 名称空间。它用于我们希望防止在集合中插入重复项的情况。就性能而言,它比列表要好。 哈希集 .Contains(T)方法 用于检查哈希集 对象包含指定的元素。

null

语法:

mySet.Contains(T item);

在这里 迈塞特 是哈希集的名称,并且 项目 是要在哈希集中定位的必需元素 对象

返回类型: 此方法返回 符合事实的 如果 哈希集 对象包含指定的元素;否则 错误的 .

下面给出了一些例子,以更好地理解实施:

例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" );
}
}


输出:

Required Element is not present

参考:

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享