Science, asked by aniruddhmuv2935, 1 year ago

Difference between stringdictionary and listdictionary in c# example

Answers

Answered by surya38138
0
StringDictionary Class.NET Framework (current version)Other Versions NoteThe .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Implements a hash table with the key and the value strongly typed to be strings rather than objects.

Namespace:   System.Collections.Specialized
Assembly:  System (in System.dll)

Inheritance HierarchySystem.Object
  System.Collections.Specialized.StringDictionary

SyntaxC#C++F#VB[SerializableAttribute] public class StringDictionary : IEnumerable ConstructorsNameDescriptionStringDictionary()Initializes a new instance of the StringDictionary class.PropertiesNameDescriptionCountGets the number of key/value pairs in the StringDictionary.IsSynchronizedGets a value indicating whether access to the StringDictionary is synchronized (thread safe).Item[String]Gets or sets the value associated with the specified key.KeysGets a collection of keys in the StringDictionary.SyncRootGets an object that can be used to synchronize access to the StringDictionary.ValuesGets a collection of values in the StringDictionary.MethodsNameDescriptionAdd(String, String)Adds an entry with the specified key and value into the StringDictionary.Clear()Removes all entries from the StringDictionary.ContainsKey(String)Determines if the StringDictionary contains a specific key.ContainsValue(String)Determines if the StringDictionary contains a specific value.CopyTo(Array, Int32)Copies the string dictionary values to a one-dimensional Array instance at the specified index.Equals(Object)Determines whether the specified object is equal to the current object.(Inherited from Object.)Finalize()Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)GetEnumerator()Returns an enumerator that iterates through the string dictionary.GetHashCode()Serves as the default hash function. (Inherited from Object.)GetType()Gets the Type of the current instance.(Inherited from Object.)MemberwiseClone()Creates a shallow copy of the current Object.(Inherited from Object.)Remove(String)Removes the entry with the specified key from the string dictionary.ToString()Returns a string that represents the current object.(Inherited from Object.)Extension MethodsNameDescriptionAsParallel()Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.)AsQueryable()Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.)Cast<TResult>()Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.)OfType<TResult>()Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.)Remarks

A key cannot be null, but a value can.

The key is handled in a case-insensitive manner; it is translated to lowercase before it is used with the string dictionary.

In .NET Framework version 1.0, this class uses culture-sensitive string comparisons. However, in .NET Framework version 1.1 and later, this class uses CultureInfo.InvariantCulture when comparing strings. For more information about how culture affects comparisons and sorting, see Performing Culture-Insensitive String Operations.

Examples

The following code example demonstrates several of the properties and methods of StringDictionary.

C#C++VBusing System; using System.Collections; using System.Collections.Specialized; public class SamplesStringDictionary { public static void Main() { // Creates and initializes a new StringDictionary. StringDictionary myCol = new StringDictionary(); myCol.Add( "red", "rojo" ); myCol.Add( "green", "verde" ); myCol.Add( "blue", "azul" ); // Display the contents of the collection using foreach. This is the preferred method. Console.WriteLine( "Displays the elements using foreach:" ); PrintKeysAndValues1( myCol ); // Display the contents of the collection using the enumerator. Console.WriteLine( "Displays the elements using the IEnumerator:" ); PrintKeysAndValues2( myCol ); // Display the contents of the collection using the Keys, Values, Count, and Item properties. Console.WriteLine( "Displays the elements using the Keys, Values, Count, and Item properties:" ); PrintKeysAndValues3( myCol ); // Copies the StringDictionary to an array with DictionaryEntry elements. DictionaryEntry[] myArr = new DictionaryEntry[myCol.Count]; myCol.CopyTo( myArr, 0 ); // Displays the values in the array. Console.WriteLine( "Displays the elements in the array:" ); Console.WriteLine( " KEY VALUE" ); for ( int i = 0; i < myArr.Length; i++ ) Console.WriteLine( " {0,-10} {1}", myArr[i].Key, myArr[i].Value ); Console.WriteLine(); // Searches for a value. if ( myCol.ContainsValue( "amarillo" ) ) Console.WriteLine( "The collection contains the value \"amarillo\"." ); else Console.WriteLine( "The collection does not contain the value \"amarillo\"." ); Console.WriteLine(); // Searches for a key and deletes it. if ( myCol.ContainsKey( "green" ) ) myCol.Remove( 
Similar questions