Skip to content

Instantly share code, notes, and snippets.

@ichiroku11
Created October 9, 2012 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ichiroku11/3858020 to your computer and use it in GitHub Desktop.
Save ichiroku11/3858020 to your computer and use it in GitHub Desktop.
HashSet<T>のメソッドの使い方
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp {
class Program {
static void Main( string[] args ) {
var items = new HashSet<int> { 0, 1, 2, 3, 4, };
// otherが同じ要素を含んでいるかどうか
Console.WriteLine( "SetEquals(other)" );
Console.WriteLine( items.SetEquals( new[] { 0, 1, 2, 3, 4, } ) ); // True
Console.WriteLine( items.SetEquals( new[] { 0, 1, 2, 3, } ) ); // False
Console.WriteLine( items.SetEquals( new[] { 0, 1, 2, 3, 4, 5, } ) ); // False
// otherの部分(下位)集合かどうか
Console.WriteLine( "IsSubsetOf(other)" );
Console.WriteLine( items.IsSubsetOf( new[] { 0, 1, 2, 3, 4, } ) ); // True
Console.WriteLine( items.IsSubsetOf( new[] { 0, 1, 2, 3, } ) ); // False
Console.WriteLine( items.IsSubsetOf( new[] { 0, 1, 2, 3, 4, 5, } ) ); // True
// otherの真部分(下位)集合かどうか
Console.WriteLine( "IsProperSubsetOf(other)" );
Console.WriteLine( items.IsProperSubsetOf( new[] { 0, 1, 2, 3, 4, } ) ); // False
Console.WriteLine( items.IsProperSubsetOf( new[] { 0, 1, 2, 3, } ) ); // False
Console.WriteLine( items.IsProperSubsetOf( new[] { 0, 1, 2, 3, 4, 5, } ) ); // True
// otherの上位集合かどうか
Console.WriteLine( "IsSupersetOf(other)" );
Console.WriteLine( items.IsSupersetOf( new[] { 0, 1, 2, 3, 4, } ) ); // True
Console.WriteLine( items.IsSupersetOf( new[] { 0, 1, 2, 3, } ) ); // True
Console.WriteLine( items.IsSupersetOf( new[] { 0, 1, 2, 3, 4, 5, } ) ); // False
// otherの真?上位集合かどうか
Console.WriteLine( "IsProperSupersetOf(other)" );
Console.WriteLine( items.IsProperSupersetOf( new[] { 0, 1, 2, 3, 4, } ) ); // False
Console.WriteLine( items.IsProperSupersetOf( new[] { 0, 1, 2, 3, } ) ); // True
Console.WriteLine( items.IsProperSupersetOf( new[] { 0, 1, 2, 3, 4, 5, } ) ); // False
// 実行結果
/*
SetEquals(other)
True
False
False
IsSubsetOf(other)
True
False
True
IsProperSubsetOf(other)
False
False
True
IsSupersetOf(other)
True
True
False
IsProperSupersetOf(other)
False
True
False
*/
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment