C# by Patrik

Get Dictionary Keys as a Comma-Separated String

How to Convert Dictionary Keys into a Comma-Separated String in C#

When working with a Dictionary<string, object> in C#, you may need to get all the keys as a single string. This can be done easily using the built-in string.Join method.

Here’s a simple example:

var dict = new Dictionary<string, object>
{
    { "Name", "Alice" },
    { "Age", 30 },
    { "Country", "USA" }
};

string keys = string.Join(",", dict.Keys);
Console.WriteLine(keys); // Output: Name,Age,Country
  • dict.Keys gives you the collection of keys
  • string.Join combines them with commas

This approach is clean, fast, and works well for logging, debugging, or exporting keys.

csharp
dictionary
strings
tips
beginners

Comments