We cannot covert the Dictionary keys to List<string> directly. Because the return type of the Key is KeyCollection. Here is the code to convert the keys to string list.
Method 1
- Dictionary<string, string> dictionary = new Dictionary<string, string>();
- dictionary.Add("One", "1");
- dictionary.Add("Two", "2");
- List<string> keys=new List<string>(dictionary.Keys);
Method 2
- Dictionary<string, string> dictionary = new Dictionary<string, string>();
- dictionary.Add("One", "1");
- dictionary.Add("Two", "2");
- var key = dictionary.Keys;
- List<string> keys1= key.Cast<string>().ToList();