To convert a dictionary to an array of values in C#, you can use the `Values` property of the `Dictionary
We can convert a dictionary to an array of values in C# using the `Values` property and the `ToArray` method. This example demonstrates how to declare and initialize a dictionary, use the `Values` property to get the values, convert them to an array, and print the array of values.
For example,
using System.Collections.Generic;
and using System.Linq;
.myDictionary
with integer keys and string values.using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// Declare and initialize a dictionary
Dictionary<int, string> myDictionary = new Dictionary<int, string>
{
{1, "One"},
{2, "Two"},
{3, "Three"}
};
// Convert the dictionary to an array of values
string[] valuesArray = myDictionary.Values.ToArray();
// Print the array of values
Console.WriteLine("Array of values:");
foreach (string value in valuesArray)
{
Console.WriteLine(value);
}
}
}
Array of values: One Two Three
We can convert an empty dictionary to an array of values in C# using the `Values` property and the `ToArray` method. This example demonstrates how to declare and initialize an empty dictionary, use the `Values` property to get the values, convert them to an array, and print the array of values.
For example,
using System.Collections.Generic;
and using System.Linq;
.myDictionary
.using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// Declare and initialize an empty dictionary
Dictionary<int, string> myDictionary = new Dictionary<int, string>();
// Convert the dictionary to an array of values
string[] valuesArray = myDictionary.Values.ToArray();
// Print the array of values
Console.WriteLine("Array of values:");
foreach (string value in valuesArray)
{
Console.WriteLine(value);
}
}
}
Array of values:
In this tutorial, we learned How to Convert a Dictionary to an Array of Values in C# language with well detailed examples.