SortedSet<T>.GetViewBetween(lower, upper) মেথড ব্যবহার করে নির্দিষ্ট পরিসীমার মধ্যে একটি ভিউ (সাবসেট) পাওয়ার সুযোগ থাকে। এটি মূল সেটের একটি লাইভ ভিউ রিটার্ন করে, অর্থাৎ মূল সেটে পরিবর্তন হলে ভিউটিও প্রতিফলিত হবে। নিচের এবং উপরের বাউন্ড ইনক্লুসিভ (inclusive)।
উদাহরণ ১: স্ট্রিং SortedSet-এ সাবসেট
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedSet<string> set1 = new SortedSet<string>();
set1.Add("AB");
set1.Add("BC");
set1.Add("CD");
set1.Add("EF");
Console.WriteLine("SortedSet1-এ উপাদানসমূহ...");
foreach (string res in set1) {
Console.WriteLine(res);
}
SortedSet<string> set2 = new SortedSet<string>();
set2.Add("BC");
set2.Add("CD");
set2.Add("DE");
set2.Add("EF");
set2.Add("AB");
set2.Add("HI");
set2.Add("JK");
Console.WriteLine("SortedSet2-এ উপাদানসমূহ (Enumerator)...");
SortedSet<string>.Enumerator demoEnum = set2.GetEnumerator();
while (demoEnum.MoveNext()) {
string res = demoEnum.Current;
Console.WriteLine(res);
}
SortedSet<string> set3 = set2.GetViewBetween("CD", "EF");
Console.WriteLine("SortedSet3-এ উপাদানসমূহ (CD থেকে EF)...");
foreach (string res in set3) {
Console.WriteLine(res);
}
}
}
আউটপুট
SortedSet1-এ উপাদানসমূহ... AB BC CD EF SortedSet2-এ উপাদানসমূহ (Enumerator)... AB BC CD DE EF HI JK SortedSet3-এ উপাদানসমূহ (CD থেকে EF)... CD DE EF
উদাহরণ ২: ইন্টিজার SortedSet-এ সাবসেট
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedSet<int> set1 = new SortedSet<int>();
set1.Add(50);
set1.Add(100);
set1.Add(150);
set1.Add(200);
set1.Add(250);
set1.Add(300);
set1.Add(350);
set1.Add(400);
set1.Add(450);
set1.Add(500);
Console.WriteLine("SortedSet1-এ উপাদানসমূহ...");
foreach (int res in set1) {
Console.WriteLine(res);
}
SortedSet<int> set2 = set1.GetViewBetween(150, 400);
Console.WriteLine("SortedSet2-এ উপাদানসমূহ (150 থেকে 400)...");
foreach (int res in set2) {
Console.WriteLine(res);
}
}
}
আউটপুট
SortedSet1-এ উপাদানসমূহ... 50 100 150 200 250 300 350 400 450 500 SortedSet2-এ উপাদানসমূহ (150 থেকে 400)... 150 200 250 300 350 400
মূল বিন্দু: GetViewBetween একটি রিড-ওনلی ভিউ রিটার্ন করে; এর মাধ্যমে সরাসরি যোগ/বিরতি করা যায় না, কিন্তু মূল সেট পরিবর্তন করলে ভিউ আপডেট হয়।