C#-এ Array.TrueForAll() মেথডটি নির্ধারণ করে যে অ্যারের প্রতিটি উপাদান নির্দিষ্ট প্রেডিকেট (শর্ত) পূরণ করে কিনা। এটি LINQ-এর All()-এর সমতুল্য কিন্তু অ্যারে-স্পেসিফিক।
সিনট্যাক্স (Syntax)
public static bool TrueForAll(T[] array, Predicate match);
প্যারামিটার:
array- যাচাই করার এক-মাত্রিক অ্যারেmatch-Predicate<T>ডেলিগেট যা শর্ত নির্ধারণ করে
উদাহরণ: সব উপাদান 'b' দিয়ে শুরু হয় কিনা
using System;
public class Demo
{
public static void Main()
{
Console.WriteLine("Array elements...");
string[] arr = { "bike", "bus" };
for (int i = 0; i < arr.Length; i++)
{
Console.Write("{0} ", arr[i]);
}
Console.WriteLine();
// 'bus'-এর লাস্ট ইন্ডেক্স খুঁজে বের করা (অতিরিক্ত)
int lastIndex = Array.LastIndexOf(arr, "bus");
Console.WriteLine("Last Occurrence of element bus is at index = " + lastIndex);
// TrueForAll: সব উপাদান 'b' দিয়ে শুরু হয় কিনা
bool res = Array.TrueForAll(arr, ele => ele.StartsWith("b", StringComparison.Ordinal));
if (res)
Console.Write("Every element in the array matches the conditions defined by the specified predicate.");
else
Console.Write("Every element in the array does not match the conditions defined by the specified predicate.");
}
}
আউটপুট
Array elements... bike bus Last Occurrence of element bus is at index = 1 Every element in the array matches the conditions defined by the specified predicate.
অন্যান্য উদাহরণ
উদাহরণ: সংখ্যা অ্যারে - সবই পজিটিভ কিনা
using System;
public class Demo
{
public static void Main()
{
int[] numbers = { 1, 5, 10, 100, 50 };
int[] numbersWithNegative = { 1, -5, 10, 100 };
bool allPositive1 = Array.TrueForAll(numbers, n => n > 0);
bool allPositive2 = Array.TrueForAll(numbersWithNegative, n => n > 0);
Console.WriteLine($"{{1, 5, 10, 100, 50}} all positive: {allPositive1}"); // True
Console.WriteLine($"{{1, -5, 10, 100}} all positive: {allPositive2}"); // False
}
}
উদাহরণ: স্ট্রিং দৈর্ঘ্য চেক
using System;
public class Demo
{
public static void Main()
{
string[] words = { "cat", "dog", "bird", "fish" };
string[] longWords = { "elephant", "hippopotamus", "cat" };
bool allShort = Array.TrueForAll(words, w => w.Length <= 4);
bool allShort2 = Array.TrueForAll(longWords, w => w.Length <= 4);
Console.WriteLine($"All <= 4 chars: {allShort}"); // True
Console.WriteLine($"All <= 4 chars: {allShort2}"); // False
}
}
TrueForAll() vs LINQ All()
| ফিচার | Array.TrueForAll() | LINQ Enumerable.All() |
|---|---|---|
| নেমস্পেস | System.Array | System.Linq |
| ইনপুট | T[] (অ্যারে) | IEnumerable<T> (যেকোনো) |
| পারফরম্যান্স | লiger-weight | এনুমারেটর ওভারহেড |
| নাল চেক | array null হলে ArgumentNullException | source null হলে ArgumentNullException |
প্রেডিকেট (Predicate) বনাম ল্যাম্বডা
// Predicateডেলিগেট ব্যবহার Predicate startsWithB = delegate(string s) { return s.StartsWith("b"); }; bool result1 = Array.TrueForAll(arr, startsWithB); // ল্যাম্বডা এক্সপ্রেশন (আধুনিক এবং সুপারিশকৃত) bool result2 = Array.TrueForAll(arr, s => s.StartsWith("b")); // মেথড গ্রুপ bool result3 = Array.TrueForAll(arr, "b".Equals); // ভিন্ন সেম্যান্টিক্স
সতর্কতা: খালি অ্যারে (new T[0]) এর জন্য TrueForAll সবসময় true ফেরত দেয় (vacuous truth)।