C# में Array.TrueForAll() मेथड यह निर्धारित करता है कि सरणी में हर तत्व निर्दिष्ट प्रीडिकेट द्वारा परिभाषित शर्तों से मेल खाता है या नहीं।
सिंटैक्स
public static bool TrueForAll<T> (T[] array, Predicate<T> 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();
int lastIndex = Array.LastIndexOf(arr, "bus");
Console.WriteLine("Last Occurrence of element bus is at index = " + lastIndex);
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.
अधिक उदाहरण
1. संख्याओं की जांच - सभी धनात्मक हैं?
int[] numbers = { 1, 5, 10, 100, 25 };
bool allPositive = Array.TrueForAll(numbers, n => n > 0);
Console.WriteLine("सभी धनात्मक: " + allPositive); // True
int[] mixed = { 1, -5, 10 };
bool allPos2 = Array.TrueForAll(mixed, n => n > 0);
Console.WriteLine("सभी धनात्मक: " + allPos2); // False
2. स्ट्रिंग लंबाई जांच
string[] words = { "cat", "dog", "bird", "fish" };
bool allShort = Array.TrueForAll(words, w => w.Length <= 4);
Console.WriteLine("सभी 4 अक्षर या कम: " + allShort); // True
string[] longWords = { "cat", "elephant", "dog" };
bool allShort2 = Array.TrueForAll(longWords, w => w.Length <= 4);
Console.WriteLine("सभी 4 अक्षर या कम: " + allShort2); // False
3. कस्टम ऑब्जेक्ट के साथ
public class Product
{
public string Name { get; set; }
public decimal Price { get; set; }
public bool InStock { get; set; }
}
Product[] products =
{
new Product { Name = "Laptop", Price = 999, InStock = true },
new Product { Name = "Mouse", Price = 25, InStock = true },
new Product { Name = "Keyboard", Price = 75, InStock = false }
};
bool allInStock = Array.TrueForAll(products, p => p.InStock);
bool allAffordable = Array.TrueForAll(products, p => p.Price < 1000);
Console.WriteLine("सभी स्टॉक में: " + allInStock); // False
Console.WriteLine("सभी किफायती: " + allAffordable); // True
TrueForAll बनाम अन्य मेथड्स
| मेथड | उद्देश्य | लौटाता है |
|---|---|---|
| TrueForAll | सभी तत्व शर्त पूरी करते हैं? | bool |
| Exists | कम से कम एक तत्व शर्त पूरी करता है? | bool |
| FindAll | शर्त पूरी करने वाले सभी तत्व ढूंढें | T[] |
| Find | पहला मिलान तत्व ढूंढें | T |
| FindLast | अंतिम मिलान तत्व ढूंढें | T |
मुख्य बिंदु
- शॉर्ट-सर्किट मूल्यांकन: पहला गलत मिलते ही रुक जाता है (प्रदर्शन)
- खाली सरणी के लिए true लौटाता है (वैक्यूसली ट्रू)
- null सरणी के लिए ArgumentNullException फेंकता है
- null प्रीडिकेट के लिए ArgumentNullException फेंकता है
- LINQ के
All()के समान लेकिन सरणी-विशिष्ट और थोड़ा तेज