How to insert values into C# Dictionary on instantiation?

How to insert values into C# Dictionary on instantiation?

You can insert values into a C# dictionary when you instantiate it by passing a collection initializer to the dictionary constructor. The collection initializer is enclosed in curly braces and consists of key-value pairs separated by commas.

Here's an example of how to insert values into a dictionary on instantiation:

 using System; using System.Collections.Generic; class Program { static void Main(string[] args) { // Instantiate a dictionary with initial values Dictionary<string, int> inventory = new Dictionary<string, int> { { "apples", 10 }, { "bananas", 5 }, { "oranges", 15 } }; // Print out the contents of the dictionary foreach (KeyValuePair<string, int> item in inventory) { Console.WriteLine("{0}: {1}", item.Key, item.Value); } } } 

In this example, we create a dictionary called inventory with initial values of "apples", "bananas", and "oranges" and their corresponding quantities. The initial values are provided using the collection initializer enclosed in curly braces {}.

When we loop through the dictionary using a foreach loop, we print out each key-value pair using the Key and Value properties of the KeyValuePair<string, int> object.

Output:

 apples: 10 bananas: 5 oranges: 15 

Examples

  1. "C# Dictionary insert values on instantiation"

    Description: Learn how to insert key-value pairs into a C# Dictionary during instantiation.

    // C# Code var myDictionary = new Dictionary<string, int> { { "Key1", 1 }, { "Key2", 2 }, { "Key3", 3 } }; 
  2. "C# Dictionary initialize with values on creation"

    Description: Explore initializing a C# Dictionary with values using the Add method during instantiation.

    // C# Code var myDictionary = new Dictionary<string, int> { ["Key1"] = 1, ["Key2"] = 2, ["Key3"] = 3 }; 
  3. "C# Dictionary add values on creation with anonymous object"

    Description: Learn how to use an anonymous object to initialize a C# Dictionary with key-value pairs during instantiation.

    // C# Code var myDictionary = new Dictionary<string, int> { { "Key1", 1 }, { "Key2", 2 }, { "Key3", 3 } }; 
  4. "C# Dictionary insert multiple values on creation"

    Description: Explore inserting multiple key-value pairs into a C# Dictionary during instantiation.

    // C# Code var myDictionary = new Dictionary<string, int> { { "Key1", 1 }, { "Key2", 2 }, { "Key3", 3 } }; 
  5. "C# Dictionary add values using collection initializer"

    Description: Learn how to use the collection initializer syntax to add values to a C# Dictionary during instantiation.

    // C# Code var myDictionary = new Dictionary<string, int> { ["Key1"] = 1, ["Key2"] = 2, ["Key3"] = 3 }; 
  6. "C# Dictionary instantiate with values using Tuple"

    Description: Explore using Tuple to instantiate a C# Dictionary with key-value pairs during creation.

    // C# Code var myDictionary = new Dictionary<string, int> { Tuple.Create("Key1", 1), Tuple.Create("Key2", 2), Tuple.Create("Key3", 3) }.ToDictionary(t => t.Item1, t => t.Item2); 
  7. "C# Dictionary insert values with LINQ during instantiation"

    Description: Learn how to use LINQ to initialize a C# Dictionary with key-value pairs during instantiation.

    // C# Code var myDictionary = Enumerable.Range(1, 3) .ToDictionary(i => $"Key{i}", i => i); 
  8. "C# Dictionary initialize with values using KeyValuePair"

    Description: Explore using KeyValuePair to initialize a C# Dictionary with key-value pairs during instantiation.

    // C# Code var myDictionary = new Dictionary<string, int> { new KeyValuePair<string, int>("Key1", 1), new KeyValuePair<string, int>("Key2", 2), new KeyValuePair<string, int>("Key3", 3) }; 
  9. "C# Dictionary add values with dynamic object"

    Description: Learn how to use a dynamic object to insert key-value pairs into a C# Dictionary during instantiation.

    // C# Code dynamic keyValuePairs = new { Key1 = 1, Key2 = 2, Key3 = 3 }; var myDictionary = new Dictionary<string, int>(keyValuePairs); 
  10. "C# Dictionary initialize with values from another Dictionary"

    Description: Explore initializing a C# Dictionary with key-value pairs from another Dictionary during instantiation.

    // C# Code var sourceDictionary = new Dictionary<string, int> { { "Key1", 1 }, { "Key2", 2 }, { "Key3", 3 } }; var myDictionary = new Dictionary<string, int>(sourceDictionary); 

More Tags

liquid static-classes webpacker typeface webview dayofweek mongodb-csharp-2.0 json-server ms-word enumerable

More C# Questions

More Animal pregnancy Calculators

More Various Measurements Units Calculators

More Mortgage and Real Estate Calculators

More Entertainment Anecdotes Calculators