简体   繁体   English

是否可以创建一个字典,其中的值可以具有多种数据类型? (另外我如何遍历一个数组作为我的值之一)

[英]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.

相关问题 使用具有一些空值的linq遍历数组 - Iterate through an array with linq which have some null values 测试我可以遍历字典并将键和值写入控制台 - Test that I can iterate through dictionary and write keys and values to console 如何遍历包含字典的字典,该字典包含另一个包含列表的字典 - How can I iterate through a dictionary that contains a dictionary that contains another dictionary which contains a list 如何存储可空值并遍历它们? - How Do I Store Nullable values and Iterate Through Them? 如何将表/数组的多个值传递给控制器 - How can I pass multiple values of a table/array to my controller 如果您有多个具有不同值的变量,并且我想将这些值通过处理该值的代码块传递,我该怎么做? - If you have multiple variables with different values and i want to pass these values through a block of code that process the value,how do i do that? CodeGo.net&gt;如何遍历字典和比较值? - c# - How to iterate through a dictionary and compare values? 如何使用一种方法将字符串的多种不同类型的日期值转换为datetime? - How can I convert date values from the multiple different types that are string to datetime with one method? 如何逐步遍历大小为n的字节数组的所有可能值? - How to incrementally iterate through all possible values of a byte array of size n? 我如何 select 数组中的多个值? - How do I select multiple values in an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM