Iterating Collections c# In Hindi
C# प्रोग्रामिंग लैंग्वेज में C# प्रोग्रामर मल्टीप्ल इंडिविजुअल प्रोग्राम लूप कंस्ट्रक्ट और मेथड को अप्लाई करके List<T>, Stack<T>, Queue<T>, Dictionary<TKey, TValue>, और अन्य कलेक्शन डाटा टाइप पर इटरेट या लूप कर सकते हैं। कलेक्शन डाटा टाइप में सामान्य रूप से इटरेट करने के आपको कई मेथड या तरीके मिलते हैं. जिसमे, foreach, for, या while लूप का यूज़ करना, इसके साथ ही मोर काम्प्लेक्स प्रोग्राम क्वेरी के लिए LINQ डाटा कनेक्टेड मेथड का यूज़ करना।

So, let’s learn how to iterate over collections List<T>, Stack<T>, Queue<T>, and Dictionary<TKey, TValue> in C# programming.
Iterating over the List<T> collection data type.
C# प्रोग्राम में List<T> एक आम कलेक्शन डाटा टाइप कांसेप्ट है, जिस पर C# यूजर foreach लूप, for लूप, या LINQ मेथड का यूज़ करके इटरेट कर सकते हैं।
Using a foreach loop in the List<T> data type.
यहाँ C# यूजर प्रोग्राम में foreach लूप List<T> में इटरेट करने के लिए इस लूप का यूज़ कर सकते है।
Foreach loop example in the List<T>.
using System;
using System.Collections.Generic;
class ListIterate
{
static void Main()
{
List<int> integers = new List<int> { 8, 9, 1, 7, 3, 2, 10 };
// here we Iterating list data type using foreach loop
foreach (int intgrs in integers)
{
Console.WriteLine(intgrs); // Result is – 8 9 1 7 3 2 10
}
}
}
Using a for loop in a list collection data type.
जब C# यूजर को प्रोग्राम में हर एलिमेंट का इंडेक्स लोकेशन एक्सेस करना हो, तो for loop लिस्ट डाटा में स्टार्ट से एंड तक इटरेट करने का एक बेस्ट और मेथड है।
For loop example in the List<T>.
using System;
using System.Collections.Generic;
class ListIForIterate
{
static void Main()
{
List<string> employees = new List<string> { “Harry”, “Siddhi”, “Bhavishi” , “Amit”};
// here it Iterating using for loop accessing the index)
for (int p = 0; p < employees.Count; p++)
{
Console.WriteLine(employees[p]); // Result is – Harry”, Siddhi, Bhavishi, Amit
}
}
}
Using LINQ with the collection data type.
C# यूजर कलेक्शन डाटा टाइप पर मल्टीप्ल लूप प्रोग्रामिंग ऑपरेशन अप्लाई करने के लिए LINQ (लैंग्वेज इंटीग्रेटेड क्वेरी) का भी यूज़ कर सकते हैं। जैसे, किसी लिस्ट डाटा टाइप एलिमेंट में लूप इटरेट करने और सिर्फ़ कुछ पर्टिकुलर एलिमेंट्स को सलेक्ट करने के लिए इसका यूज़ कर सकते है।
LINQ with the collection data type example.
using System;
using System.Collections.Generic;
using System.Linq;
class LinqIterate
{
static void Main()
{
List<int> integers = new List<int> { 21, 28, 33, 44, 57, 88, 91, 100 };
// here we Using LINQ method to filter even integers numeric and print them console screen
var evenIntegers = integers.Where(n => n % 2 == 0);
foreach (var intgrs in evenIntegers)
{
Console.WriteLine(intgrs); // Result is – 28 44 88 100
}
}
}
Iterating over the Stack<T> collection data type.
C# प्रोग्रामिंग में एक Stack<T> कलेक्शन डाटा टाइप लास्ट इन, फर्स्ट आउट (LIFO) कांसेप्ट आर्डर को फॉलो करता है. इस वजह से C# यूजर इसे List<T> डाटा टाइप की तरह ही लूप या इटरेट कर सकते हैं। जैसा की, यहाँ स्टैक डाटा टाइप LIFO आर्डर में है, इस वजह से इसे डायरेक्ट इटरेट करने से स्टैक आइटम ऐड किए जाने का ऑर्डर नहीं रहेगा। C# यूजर स्टैक में स्टोर डाटा एलिमेंट को प्रीव्यू करने के लिए अभी भी foreach लूप का यूज़ कर सकते हैं. यहाँ स्टैक का ऑर्डर सबसे हाल ही में ऐड किए गए स्टैक आइटम से सबसे कम हाल ही में ऐड किए गए आइटम तक होगा।
Example of a foreach loop in a Stack<T> collection data.
using System;
using System.Collections.Generic;
class StackIterate
{
static void Main()
{
Stack<string> stack = new Stack<string>();
stack.Push(“C#”);
stack.Push(“Java”);
stack.Push(“Python”);
stack.Push(“Matlab”);
stack.Push(“MySql”);
// here it Iterating stack element using foreach loop
foreach (var element in stack)
{
Console.WriteLine(element); // Result is – MySql Matlab Python Java C# (LIFO stack element order)
}
}
}
Using the Stack<T> collection data type for a while loop.
यदि C# यूजर को स्टैक कलेक्शन डाटा टाइप एलिमेंट्स को ठीक उसी ऑर्डर सीक्वेंस में प्रोसेस या रिट्रीव करना है, जिस आर्डर में वे स्टैक में ऐड किए गए थे. जैसे, स्टैक से डाटा एलिमेंट्स को पॉप करना, तो C# यूजर सभी स्टैक डाटा एलिमेंट्स को डीक्यू करने के लिए while लूप को अप्लाई कर सकते हैं।
Stack<T> collection data type example with while loop.
using System;
using System.Collections.Generic;
class StackWhileIterate
{
static void Main()
{
Stack<int> stack = new Stack<int>();
stack.Push(9);
stack.Push(8);
stack.Push(7);
stack.Push(6);
stack.Push(5);
// here it Iterating using while loop to popping stack element items
while (stack.Count > 0)
{
Console.WriteLine(stack.Pop()); // Result is – 5 6 7 8 9 (LIFO stack element order)
}
}
}
Iterating the Queue<T> collection data type.
C# प्रोग्रामिंग में एक Queue<T> कलेक्शन डाटा टाइप फर्स्ट इन, फर्स्ट आउट (FIFO) कांसेप्ट या आर्डर को फॉलो करता है। Queue<T> डाटा टाइप को सामान्य रूप से उसी ऑर्डर में इटरेट किया जाता है. जिस ऑर्डर में Queue<T> डाटा टाइप में न्यू डाटा आइटम ऐड किए गए थे।
Queue<T> collection data type foreach loop example.
using System;
using System.Collections.Generic;
class QueueIterate
{
static void Main()
{
Queue<string> queue = new Queue<string>();
queue.Enqueue(“MBA”);
queue.Enqueue(“BBA”);
queue.Enqueue(“BCA”);
queue.Enqueue(“MCA”);
queue.Enqueue(“MTECH”);
// here it Iterating queue data using foreach loop method
foreach (var element in queue)
{
Console.WriteLine(element); // Result is – MBA BBA BCA MCA MTECH (FIFO queue element order)
}
}
}
Using a while loop on the Queue<T> collection data type.
क्यू कलेक्शन डाटा टाइप एलिमेंट्स को डीक्यू करके क्यू एलिमेंट में लूप से इटरेट करने के लिए, c# यूजर while लूप को अप्लाई कर सकते है।
Queue<T> collection data type while Loop example.
using System;
using System.Collections.Generic;
class QueueWhileIterate
{
static void Main()
{
Queue<int> queue = new Queue<int>();
queue.Enqueue(41);
queue.Enqueue(31);
queue.Enqueue(21);
queue.Enqueue(10);
queue.Enqueue(01);
// here it Iterating using while loop dequeueing queue element items
while (queue.Count > 0)
{
Console.WriteLine(queue.Dequeue()); // Result is – 41 31 21 10 1 (FIFO queue element order)
}
}
}
Iterating the Dictionary<TKey, TValue> collection data type.
C# प्रोग्रामिंग में एक Dictionary<TKey, TValue> कलेक्शन डाटा टाइप एक की-वैल्यू पेयर आर्डर में डाटा और इनफार्मेशन को स्टोर करता है, और C# यूजर डिक्शनरी की और वैल्यू दोनों को लूप के माध्यम से इटरेट कर सकते हैं।
Using a foreach loop in a Dictionary<TKey, TValue>.
C# यूजर डिक्शनरी में KeyValuePair<TKey, TValue> कलेक्शन डाटा एलिमेंट ऑब्जेक्ट को इटरेट करने के लिए foreach लूप को अप्लाई कर सकते हैं।
Example of a foreach loop in a Dictionary<TKey, TValue>.
using System;
using System.Collections.Generic;
class DictionaryForeach
{
static void Main()
{
Dictionary<string, int> salary = new Dictionary<string, int>();
salary.Add(“Bhavshi Deora”, 77000);
salary.Add(“Siddhi Deora”, 67000);
salary.Add(“Harry Deora”, 99000);
salary.Add(“Vivek”, 100000);
// here it Iterating Dictionary element using foreach loop with Accessing keys and its values
foreach (var pair in salary)
{
Console.WriteLine($”\n Empployee Name – {pair.Key} \n Salary – {pair.Value}”);
}
}
}
Using the Using the Dictionary Keys and Values Properties.
C# यूजर डिक्शनरी कीज़ और वैल्यूज़ प्रॉपर्टीज़ का यूज़ करके डिक्शनरी की कीज़ या वैल्यूज़ पर अलग-अलग लूप के माध्यम से इटरेट कर सकते हैं।
Using the Dictionary Keys and Values foreach Example.
using System;
using System.Collections.Generic;
class DictionaryKeyValues
{
static void Main()
{
Dictionary<string, int> salary = new Dictionary<string, int>();
salary.Add(“Bhavshi Deora”, 77000);
salary.Add(“Siddhi Deora”, 67000);
salary.Add(“Harry Deora”, 99000);
salary.Add(“Vivek”, 100000);
// here it Iterating dictionary element over keys
foreach (var key in salary.Keys)
{
Console.WriteLine(“Employee Name – ” + key);
}
// here it Iterating over values
foreach (var elements in salary.Values)
{
Console.WriteLine(“Salary – ” + elements);
}
}
}
Using LINQ methods to iterate over a Dictionary.
C# प्रोग्राम में LINQ (लैंग्वेज इंटीग्रेटेड क्वेरी) कलेक्शन डाटा टाइप पर क्वेरी करने और लूप के माध्यम से डाटा एलिमेंट ऑब्जेक्ट पर इटरेट करने का एक पावरफुल मेथड प्रोवाइड करता है। LINQ मेथड का यूज़ List<T>, Queue<T>, Stack<T>, Dictionary<TKey, TValue>, आदि कलेक्शन डाटा टाइप के साथ आसानी से लूप के माध्यम से किया जा सकता है।
Example of a LINQ method to filter and iterate.
using System;
using System.Collections.Generic;
using System.Linq;
class LinqMethodIterate
{
static void Main()
{
List<int> integers = new List<int> { 33, 12, 21, 24, 55, 66, 77, 100 };
// here we Using LINQ method to filter even integers and iterate with foreach loop
var evenIntegers = integers.Where(i => i % 2 == 0);
foreach (var intgrs in evenIntegers)
{
Console.WriteLine(intgrs); // Result is – 12 24 66 100
}
}
}
Detail Information About List<T>, Stack<T>, Queue<T>, Dictionary<TKey, TValue> Collections Data Iteration Methods.
| Collection Data Type | Common Data Iteration Method | About Information |
| List<T> data type | List data type use foreach, for loop for data value iteration | We can access list collection data type Access elements in any sequence or order. |
| Stack<T> data type | foreach, while loop used for (using Pop()) operation in stack element | Stack data type use LIFO (Last In, First Out), order may be reversed element display pattern. |
| Queue<T> data type | foreach, while used in queue data type for (using Dequeue()) loop operation | Queue data type used FIFO (First In, First Out), order is maintained to access its element. |
| Dictionary<TKey, TValue> data type | foreach, Keys, Values used in dictionary data type with start to end | Dictionary data type Access both keys and values or just keys/values in given object or element. |
| LINQ method | Linq method used to get data query with Where(), Select(), ForEach() loop or sql method | Linq method Efficient way to filter and transform group of collections data extraction. |
List<T>, Stack<T>, Queue<T>, Dictionary<TKey, TValue> Collections Data Type Iteration Summary.
C# डाटा टाइप में कलेक्शन्स डाटा पर इटरेट करना foreach, for, while जैसे अलग-अलग लूपिंग कांसेप्ट कंस्ट्रक्ट्स मेथड को डायरेक्ट अप्लाई कर सकते है, या मोर एडवांस्ड डाटा एलिमेंट फ़िल्टरिंग और क्वेरी के लिए LINQ कांसेप्ट मेथड को अप्लाई करना है। जिसमे C# यूजर अलग अलग कलेक्शन डाटा टाइप के आधार पर, C# यूजर डेटा पर लूप के माध्यम से इटरेट करने और उसे अच्छे से मैनिपुलेट करने के लिए आप अपने हिसाब से सबसे सही तरीका चुन सकते हैं।
