2017-03-12 1 views
-1

で表示されます。のC# - リストを作成し、更新するリストを使用し、定期的にこれは私が目指しています何であるアプリケーション

クラスシャツは実装しています

-Aパブリック列挙をシャツのサイズを一覧表示します三つのパラメータ定義(L、M、S、XL及びXXL)

-Aコンストラクタ:色、材料およびサイズ

読み取り専用-Three自動実装プロパティ:色、材料およびサイズ

アプリケーションクラスShirtDemo:

-Uses定期型シャツのパラメータを定義するアプリケーション

-Implements一つの方法で表示されているシャツのリストを作成し、更新するためにC#ジェネリッククラスリスト。方法は

enter image description here

シャツ(シャツのすなわち、実際の素材、色やサイズ)の詳細を表示するための責任があるこれは私が持っているものですが、それはリストを作ることになると詰まります出力表示:リストを作成するには

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Cooper_ShirtDemo 
{ 
    public enum Sizes { S, M, L, XL, XXL }; 
    class Shirt 
    { 

     public Shirt(string material, string color, Sizes size) 
      { 
      this.Material = material; 
      this.Color = color; 
      this.Size = size; 
      } 

     public Shirt() 
     { 

     } 

     public string Material 
     { 
      get; 
     } 

     public string Color 
     { 
      get; 
     } 

     public Sizes Size 
     { 
      get; 
     } 


    } 
} 

    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 

    namespace Cooper_ShirtDemo 
    { 
     class Program 
     { 


      static void Main(string[] args) 
      { 
       Console.WriteLine("*** List Of Shirts ***"); 
       Console.WriteLine(); 


      } 

      public static void Display(List<string> Shirts) 
      { 
       Console.WriteLine("Material  Color  Size"); 

      } 
     } 
    } 
+0

実装に問題がありますか? –

答えて

2

を、としてそれを宣言する::次に、新しいシャツ一つずつ追加

List<Shirt> shirts = new List<Shirt>(); 
shirts.Add(new Shirt("cotton", "red", Sizes.L)); 

...

次に、foreachループを使用してそれを表示することができます:あなたはあなたのシャツのクラスを作成している

foreach (Shirt s in shirts) 
{ 
    Console.WriteLine(s.Material + "   " + s.Color + "  " + s.Size); 
} 
+0

ああ、私は見る!助けてくれてありがとう! – ecooper10

0

たら、そのタイプのリストを作成し、それを使用することができます。まずシャツのインスタンスが必要になります。シャツタイプのリストに追加する必要があります。その後、リストを反復してコンソールに書き込むことができます。これはあなたを始めるはずです。

class Program 
{ 

    //Create a list of shirts 
    static List<Shirt> shirtsList = new List<Shirt>(); 

    static void Main(string[] args) 
    { 
     //Create some shirts 
     Shirt s1 = new Shirt("Cotten", "White", Sizes.L); 
     Shirt s2 = new Shirt("Silk", "Blue", Sizes.M); 
     Shirt s3 = new Shirt("Poly", "Red", Sizes.S); 


     //Add shirts to shirt list 
     shirtsList.Add(s1); 
     shirtsList.Add(s2); 
     shirtsList.Add(s3); 

     Console.WriteLine("*** List Of Shirts ***"); 
     Display(shirtsList); 

     Console.WriteLine(); 
    } 

    public static void Display(List<Shirt> shirts) 
    { 
     Console.WriteLine("Material\tColor\tSize"); 

     foreach (Shirt s in shirtsList) 
      Console.WriteLine("{0}\t\t{1}\t{2}", s.Material, s.Color, s.Size); 
    } 
} 
0

@willieが正しいトラックにあると思います。 しかし.... 私は辞書を使うことを提案したいと思います。あなたのリストの項目をトレースすることができます。

private Dictionary<TKey,TValue> ....= new Dictionary<TKey,TValue>(); 

TKeyを使用すると、配列のように項目をポイントできます。

Dictionary<int,Shirt> shirts = new Dictionary<int,shirt>(); 
関連する問題