[英]Is it possible to create a dictionary in which values can have multiple data types? (Also how do I iterate through an array as one of my values)
How do I iterate through profile["Names"] in this scenario?在这种情况下,如何遍历 profile["Names"]?
using System.Collections.Generic;使用 System.Collections.Generic;
Dictionary<object, object> profile = new Dictionary<object, object>();
profile.Add("Names", new string[]{"Joel", "Sean"});
profile.Add("Ethnicity", "Asian");
profile.Add("Language", "English");
for (int i = 0; i < profile["Names"].length; i ++)
{
System.Console.WriteLine($"Name: {i}");
}
Console.WriteLine("Ethnicity - " + profile["Ethnicity"]);
Console.WriteLine("Language - " + profile["Language"]);
You can use this where the key of the dictionary is type of string.您可以在字典的键是字符串类型的情况下使用它。
We need to cast the value object into string array when you know it is that.当您知道它是时,我们需要将值 object 转换为字符串数组。
Dictionary<string, object> profile = new Dictionary<string, object>();
profile.Add("Names", new string[] { "Joel", "Sean" });
profile.Add("Ethnicity", "Asian");
profile.Add("Language", "English");
var names = (string[])profile["Names"];
for ( int i = 0; i < names.Length; i++ )
{
Console.WriteLine($"Name: {names[i]}");
}
Console.WriteLine("Ethnicity - " + profile["Ethnicity"]);
Console.WriteLine("Language - " + profile["Language"]);
But you should consider to rethink your design because the actual is not very clean not stringly typed.但是您应该考虑重新考虑您的设计,因为实际不是很干净而不是字符串类型。
Perhaps:也许:
using System.Collections.Generics;
public class Profile
{
public string Name { get; set; }
public string Ethnicity { get; set; }
public string Language { get; set; }
public Profile(string name, string ethnicity, string language)
{
Name = name;
Ethnicity = ethnicity;
Language = language;
}
}
var profiles = new List<Profile>();
profiles.Add(new Profile("Joel", "Asian", "English"));
profiles.Add(new Profile("Sean", "Asian", "English"));
foreach ( var item in profiles )
{
Console.WriteLine("Name: " + item.Name);
Console.WriteLine("Ethnicity: " + item.Ethnicity);
Console.WriteLine("Language: " + item.Language);
Console.WriteLine();
}
You can also use enums or lookup collections instead of string for ethnicity and language:您还可以使用枚举或查找 collections 而不是字符串来获取种族和语言:
enum Ethnicity
{
Asian,
Caucasian
}
enum Language
{
English,
French
}
Now a profile can be:现在配置文件可以是:
public class Profile
{
public string Name { get; set; }
public Ethnicity Ethnicity { get; set; }
public Language Language { get; set; }
public Profile(string name, Ethnicity ethnicity, Language language)
{
Name = name;
Ethnicity = ethnicity;
Language = language;
}
}
Used like that:像这样使用:
var profiles = new List<Profile>();
profiles.Add(new Profile("Joel", Ethnicity.Asian, Language.English));
profiles.Add(new Profile("Sean", Ethnicity.Asian, Language.English));
foreach ( var item in profiles )
{
Console.WriteLine("Name: " + item.Name);
Console.WriteLine("Ethnicity: " + item.Ethnicity);
Console.WriteLine("Language: " + item.Language);
Console.WriteLine();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.