Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何处理类属性的字典定义?_C# - Fatal编程技术网

C# 如何处理类属性的字典定义?

C# 如何处理类属性的字典定义?,c#,C#,我上过这样的课: public class book { public string Author {get; set;} public string Genre {get; set;} } 现在,例如,流派,应该是一本字典,有一个ID的不同流派列表,因此当我创建一本新书时,我将流派设置为字典项之一 我该如何设置?我会有一个单独的流派类来定义每一个,还是。。。我想我只是不知道该怎么做。是的,一个类型的课程是最好的设置方式 public class Book {

我上过这样的课:

public class book
{
    public string Author {get; set;}
    public string Genre  {get; set;}
}
现在,例如,
流派
,应该是一本
字典
,有一个ID的不同流派列表,因此当我创建一本新书时,我将
流派
设置为
字典
项之一


我该如何设置?我会有一个单独的
流派
类来定义每一个,还是。。。我想我只是不知道该怎么做。

是的,一个
类型的
课程是最好的设置方式

 public class Book
    {
        public string Author {get; set;}
        public Genre Genre  {get; set;}
    }

    public class Genre
    {
        public string Id {get; set;}
        public string Name  {get; set;}
    }
但是,如果“dictionary”的字面意思是dictionary,那么

public class Book
{
    public string Author {get; set;}
    public Dictionary<int, string> Genre  {get; set;}
}
公共课堂教材
{
公共字符串作者{get;set;}
公共词典类型{get;set;}
}

是的,
流派
课程是设置它的最佳方式

 public class Book
    {
        public string Author {get; set;}
        public Genre Genre  {get; set;}
    }

    public class Genre
    {
        public string Id {get; set;}
        public string Name  {get; set;}
    }
但是,如果“dictionary”的字面意思是dictionary,那么

public class Book
{
    public string Author {get; set;}
    public Dictionary<int, string> Genre  {get; set;}
}
公共课堂教材
{
公共字符串作者{get;set;}
公共词典类型{get;set;}
}

也许是这样的

public class Book
{
  public string Author { get; set; }
  public Genre Genre { get; set; }

  public Book(string author, Genre genre)
  {
    Author = author;
    Genre = genre;
  }
}

public class Genre 
{
  public string Name { get; set; }

  public static ICollection<Genre> List = new List<Genre>();

  public Genre(string name)
  {
    Name = name;

    List.Add(this);
  }
}
公共课堂教材
{
公共字符串作者{get;set;}
公共类型{get;set;}
公共书籍(字符串作者、体裁)
{
作者=作者;
体裁=体裁;
}
}
公共课体裁
{
公共字符串名称{get;set;}
公共静态ICollection List=新列表();
公共类型(字符串名称)
{
名称=名称;
列表。添加(此);
}
}

也许是这样的

public class Book
{
  public string Author { get; set; }
  public Genre Genre { get; set; }

  public Book(string author, Genre genre)
  {
    Author = author;
    Genre = genre;
  }
}

public class Genre 
{
  public string Name { get; set; }

  public static ICollection<Genre> List = new List<Genre>();

  public Genre(string name)
  {
    Name = name;

    List.Add(this);
  }
}
公共课堂教材
{
公共字符串作者{get;set;}
公共类型{get;set;}
公共书籍(字符串作者、体裁)
{
作者=作者;
体裁=体裁;
}
}
公共课体裁
{
公共字符串名称{get;set;}
公共静态ICollection List=新列表();
公共类型(字符串名称)
{
名称=名称;
列表。添加(此);
}
}

那么你想要一本流派词典?这是一个有点奇怪的设置。我的意思是我只是用一个例子来解释它,但要求是我需要一本字典来解释几个属性。你想要一本类型的字典吗?这是一个有点奇怪的设置。我的意思是我只是用一个例子来解释它,但要求是我需要一个字典来处理几个属性。那么没有理由使用C#中内置的dictionary类吗?我想这就是我所困惑的。第二部分是看起来正确的,但我是否在另一个类或其他什么中定义字典项?如何填充字典?对不起,这对我来说没什么意义-
类型。添加(2,“功夫”)-
类型。添加(2,“功夫”)