1

私はそのようなクラスを持っています:

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

Genreたとえば、はDictionary、IDを持つさまざまなジャンルのリストを持つになっているので、新しい本を作成するときに、をアイテムGenreの1つとして設定しますDictionary

これをどのように設定しますか?それぞれを定義する個別Genreのクラスがあるのでしょうか、それとも...どのようにアプローチすればよいのかわからないと思います。

4

2 に答える 2

1

はい、Genreクラスはそれを設定するための最良の方法です。

 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である場合、

public class Book
{
    public string Author {get; set;}
    public Dictionary<int, string> Genre  {get; set;}
}
于 2013-02-15T19:55:55.553 に答える
0

おそらくこのようなものですか?

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);
  }
}
于 2013-02-15T19:57:07.463 に答える