C#通过显示选择迭代的简单方法

时间:2014-02-23 01:57:19

标签: c# class loops methods

Newb在这里寻求帮助。

目前我正在使用类,为此,我正在使用类根据产品的类对象打印出5个直接对象。

这是打印我的项目的代码:

Console.WriteLine("1." + prod1.GetDetails() + "\n\n");
Console.WriteLine("2." + prod2.GetDetails() + "\n\n");
Console.WriteLine("3." + prod3.GetDetails() + "\n\n");
Console.WriteLine("4." + prod4.GetDetails() + "\n\n");
Console.WriteLine("5." + prod5.GetDetails() + "\n\n");

查看我的代码,感觉非常多余,因为只有两个字符会发生变化:第一个双引号中显示的项目编号,然后是区分使用我的类对象打印哪个'prod'的数字产品(我是新手,希望我能正确地传达这一点)。

我尝试创建一个方法来迭代它,因为它更干净,可扩展,但我的方法没有正常工作。

我最近的尝试:

public void DisplayOfferings()
{
    for (int i = 1; i < 6; i++ )//prints items 1 to 5 
    {
      Console.WriteLine(i + ". " + prod(i).GetDetails() + "\n\n"); 
    }  //iterate number      
}

这就是我存储我正在尝试打印的产品的方式,我将它们安排在PROGRAM级别,因此它们可用于所有功能,因为我会在一段时间内使用它们 课程     {

    Product prod1 = new Product("iPod Shuffle", "The clip-and-go iPod shuffle. With buttons, VoiceOver, and playlists, it's the best of iPod shuffle. Available in seven colors.", 49.89, true);

    Product prod2 = new Product("iPod Nano", "About the size of a credit card — and just 5.4 mm thin — iPod nano is the thinnest iPod ever made. Available in seven colors.", 149.89, true);

    Product prod3 = new Product("iPod Classic", "With 160GB of storage, iPod classic is the take-everything-everywhere iPod. Available in Silver or Midnight", 249.89, false);

    Product prod4 = new Product("iPod Red", "With 160GB of storage, iPod [RED] PRODUCT lets other fashion victims know you aren't just some mass consumer status concious drone, your a drone with a concious because Bono said so! Available in red.", 349.89, false);

    Product prod5 = new Product("iTouch", "It's just like an iPhone accept that your parents obviously don't trust you with a phone yet.", 449.89, true);

4 个答案:

答案 0 :(得分:4)

您可以尝试将实例存储到List

var list = new List<YourType> { prod1, prod2, prod3, prod4,prod5 };

然后循环浏览该列表:

int i = 1;
foreach(var prod in list)
{
   Console.WriteLine(i + ". " + prod.GetDetails() + "\n\n"); 
   i++;
}

如果要为此定义方法,可以这样定义:

public void DisplayOfferings(List<Product> products)
{ 
    int i = 1;
    foreach(var product in products)
    { 
        Console.WriteLine(i + ". " + product.GetDetails() + "\n\n"); 
        i++;
    }
}

然后首先创建您的列表并调用它:

var productList = new List<Product> { prod1, prod2, prod3, prod4,prod5 };
DisplayOfferings(productList);

事实上,您可以在此处使用params关键字,这样可以让您的工作更轻松。例如,如果您更改方法定义,请执行以下操作:

public void DisplayOfferings(params Product[] products)

您可以在不定义列表的情况下调用它:

DisplayOfferings(prod1, prod2, prod3, prod4,prod5);

答案 1 :(得分:1)

public void DisplayOfferings(Product[] products)
{
    for (int i = 1; i <= products.length; i++ ) 
    {
      Console.WriteLine(i + ". " + products[i - 1].GetDetails() + "\n\n"); 
    }      
}

其中products是prod1 ... prodN

的数组

答案 2 :(得分:1)

您是如何通过这些单独的产品对象来的?我假设您正在创建这5个对象并测试GetDetails()方法?为了使其更清洁,您始终可以创建“产品”对象的数组/列表,并按如下所述调用方法;再次没有你的足够数据,我无法弄清楚1)你如何获得这些产品和2)你打算用它们做什么:-) - &gt;


class Program {
    static void Main(string[] args) {
        List<Product> products = new List<Product> 
                                        {   
                                            new Product("product 1"),
                                            new Product("product 2"),
                                            new Product("product 3"),
                                            new Product("product 4"),
                                            new Product("product 5")
                                        };
        /** do some more work here in this segment */

        // now call your GetDetails() method in a nicer Object Oriented (subjective) manner
        for(int i=0; i<5; i++) {
            Console.WriteLine("\"" + (i+1) + ".\"" + products[i].GetDetails() + "\n\n");
        }
        Console.ReadLine();
    }

    private class Product {
        public Product(string data) {
            Data = data;
        }

        public string Data { get; set; }

        public string GetDetails() {
            return "The initial data string is:\t" + Data;
        }
    }
}

答案 3 :(得分:1)

你总是可以走LINQ路径:

var list = new List<YourType>
{
    prod1, prod2, prod3, prod4, prod5
};

list
    .Select((prod, i) => String.Format("{0}. {1}\n\n", i, prod.GetDetails()))
    .ToList()
    .ForEach(Console.WriteLine);