खोज…


HashSet

यह ओ (1) लुकअप के साथ, अद्वितीय वस्तुओं का एक संग्रह है।

HashSet<int> validStoryPointValues = new HashSet<int>() { 1, 2, 3, 5, 8, 13, 21 };
bool containsEight = validStoryPointValues.Contains(8); // O(1)

तुलना के माध्यम से, सूची में Contains होने से प्रदर्शन खराब होता है:

List<int> validStoryPointValues = new List<int>() { 1, 2, 3, 5, 8, 13, 21 };
bool containsEight = validStoryPointValues.Contains(8); // O(n)

HashSet.Contains एक हैश तालिका का उपयोग करता है, ताकि संग्रह में मदों की संख्या की परवाह किए बिना, लुकअप बहुत तेज़ हो।

SortedSet

// create an empty set
var mySet = new SortedSet<int>();

// add something
// note that we add 2 before we add 1
mySet.Add(2);
mySet.Add(1);

// enumerate through the set
foreach(var item in mySet)
{
    Console.WriteLine(item);
}

// output:
// 1
// 2

टी [] (टी का सरणी)

// create an array with 2 elements
var myArray = new [] { "one", "two" };

// enumerate through the array
foreach(var item in myArray)
{
    Console.WriteLine(item);
}

// output:
// one
// two

// exchange the element on the first position
// note that all collections start with the index 0
myArray[0] = "something else";


// enumerate through the array again
foreach(var item in myArray)
{
    Console.WriteLine(item);
}

// output:
// something else
// two

सूची

List<T> एक दिए गए प्रकार की एक सूची है। आइटम को इंडेक्स द्वारा जोड़ा, डाला, हटाया और संबोधित किया जा सकता है।

using System.Collections.Generic;

var list = new List<int>() { 1, 2, 3, 4, 5 };
list.Add(6);
Console.WriteLine(list.Count); // 6
list.RemoveAt(3);
Console.WriteLine(list.Count); // 5
Console.WriteLine(list[3]);    // 5

List<T> एक सरणी के रूप में सोचा जा सकता है जिसे आप आकार बदल सकते हैं। आदेश में संग्रह पर गणना करना त्वरित है, क्योंकि उनके सूचकांक के माध्यम से व्यक्तिगत तत्वों तक पहुंच है। तत्वों को उनके मूल्य, या किसी अन्य कुंजी के आधार पर एक्सेस करने के लिए, एक Dictionary<T> तेजी से लुकअप प्रदान करेगा।

शब्दकोश

शब्दकोश <TKey, TValue> एक नक्शा है। किसी दिए गए कुंजी के लिए शब्दकोश में एक मान हो सकता है।

using System.Collections.Generic;

var people = new Dictionary<string, int>
{
    { "John", 30 }, {"Mary", 35}, {"Jack", 40}
};

// Reading data
Console.WriteLine(people["John"]); // 30
Console.WriteLine(people["George"]); // throws KeyNotFoundException

int age;
if (people.TryGetValue("Mary", out age))
{ 
    Console.WriteLine(age); // 35
}

// Adding and changing data
people["John"] = 40;    // Overwriting values this way is ok
people.Add("John", 40); // Throws ArgumentException since "John" already exists

// Iterating through contents
foreach(KeyValuePair<string, int> person in people)
{
    Console.WriteLine("Name={0}, Age={1}", person.Key, person.Value);
}

foreach(string name in people.Keys)
{
    Console.WriteLine("Name={0}", name);
}

foreach(int age in people.Values)
{
    Console.WriteLine("Age={0}", age);
}

संग्रह आरंभीकरण का उपयोग करते समय डुप्लिकेट कुंजी

var people = new Dictionary<string, int>
{
    { "John", 30 }, {"Mary", 35}, {"Jack", 40}, {"Jack", 40}
}; // throws ArgumentException since "Jack" already exists

ढेर

// Initialize a stack object of integers
var stack = new Stack<int>(); 

// add some data
stack.Push(3);
stack.Push(5);
stack.Push(8);

// elements are stored with "first in, last out" order.
// stack from top to bottom is: 8, 5, 3

// We can use peek to see the top element of the stack.
Console.WriteLine(stack.Peek()); // prints 8

// Pop removes the top element of the stack and returns it.
Console.WriteLine(stack.Pop()); // prints 8
Console.WriteLine(stack.Pop()); // prints 5
Console.WriteLine(stack.Pop()); // prints 3

लिंक्ड सूची

// initialize a LinkedList of integers
LinkedList list = new LinkedList<int>();

// add some numbers to our list.
list.AddLast(3);
list.AddLast(5);
list.AddLast(8);

// the list currently is 3, 5, 8

list.AddFirst(2);
// the list now is 2, 3, 5, 8

list.RemoveFirst();
// the list is now 3, 5, 8

list.RemoveLast();
// the list is now 3, 5

ध्यान दें कि LinkedList<T> दोहरी लिंक की गई सूची का प्रतिनिधित्व करता है। तो, यह केवल नोड्स का संग्रह है और प्रत्येक नोड में T का एक तत्व होता है। प्रत्येक नोड पूर्ववर्ती नोड और निम्न नोड से जुड़ा हुआ है।

पंक्ति

// Initalize a new queue of integers
var queue = new Queue<int>();

// Add some data
queue.Enqueue(6);
queue.Enqueue(4);
queue.Enqueue(9);

// Elements in a queue are stored in "first in, first out" order.
// The queue from first to last is: 6, 4, 9

// View the next element in the queue, without removing it.
Console.WriteLine(queue.Peek()); // prints 6

// Removes the first element in the queue, and returns it.
Console.WriteLine(queue.Dequeue()); // prints 6
Console.WriteLine(queue.Dequeue()); // prints 4
Console.WriteLine(queue.Dequeue()); // prints 9

थ्रेड सुरक्षित सिर! मल्टी-थ्रेड वातावरण में समवर्ती क्यू का उपयोग करें।



Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow