c#需要帮助处理数组

时间:2016-04-29 13:23:12

标签: c#

有没有办法将这些放入1 D阵列或2 D阵列。 ?我已经生成了代码,它看起来有点凌乱,而且可以缩短多长时间?

    double worstPrice = 6.47;
    double bestPrice = 0.99;
    double CivetCatPrice =29.14;
    double whenPrice = 10.50;
    double everythingPrice = 319.56;
    int bestStock = 3238;
    int worstStock = 8;
    int civetCatstock = 3;
    int whenStock = 37;
    int everythingStock = 2;

3 个答案:

答案 0 :(得分:1)

你可以像这样为每个双打和整数创建一个数组

double[] priceData = new double[]{ 6.47, 0.99, 29.14, 10.50, 319.56 };
int[] stockData = new int[]{ 3238, 8, 3, 37, 2 };

或者,如果您希望他们保留姓名

,您可以使用字典
Dictionary<string, double> priceDict = new Dictionary<string, double>();
priceDict.Add("worstPrice", 6.47);
//And so on for each double

Dictionary<string, int> stockDict = new Dictionary<string, int>();
priceDict.Add("bestStock", 3238);
//And so on for each int

这些中的值可以这样调用

double worstMinusBestPrices = priceData[0] - priceData[1]; //For arrays
double worstMinusBestPrices = priceDict["worstPrice"] - priceDict["bestPrice"] //For dictionaries

答案 1 :(得分:1)

您可以实现一个自定义类,它将这些值保存为具有有意义名称的属性。然后,您的代码将更具可读性,可维护性和健壮性。

例如(你不需要所有这些类,它应该只是给你一个想法):

public abstract class Animal
{
    public Animal(string animalName)
    {
        this.Name = animalName;
    }
    //insert properties and methods which all aimals share here
    public string Name { get; set; }
}

public class CibetCat : Animal
{
    public CibetCat() : base("CibetCat")
    {
    }

    //insert properties and methods which all CibetCats share here
}

现在您的班级包含价格和股票信息以及对动物本身的引用(在您的示例中为CibetCat):

public class AnimalStock // or AnimalPrice or whatever
{
    public AnimalStock(Animal animal)
    {
        this.Animal = animal;
    }

    public AnimalStock(Animal animal, decimal worstPrice, decimal bestPrice, int bestStock, int worstStock)
    {
        this.Animal = animal;
        this.Worstprice = worstPrice;
        this.BestPrice = bestPrice;
        this.BestStock = bestStock;
        this.WorstStock = worstStock;
    }

    public Animal Animal { get; set; }
    public decimal Worstprice { get; set; }
    public decimal BestPrice { get; set; }

    public int BestStock { get; set; }
    public int WorstStock { get; set; }

    // ...
}

很多代码但不复杂。现在您可以编写这个简单易读的代码:

Animal cibetCat = new CibetCat();
AnimalStock stock = new AnimalStock(cibetCat);
stock.BestPrice = 0.99m;
stock.Worstprice = 6.47m;
stock.BestStock = 3238;
// ...

稍后您可以从单个实例访问所有这些属性(或它的方法)。

Console.WriteLine("Animal's best-price is: {0}", stock.BestPrice); // etc

答案 2 :(得分:0)

正如Alfie指出的那样,你可以使用字典 - 但是你要用字符串标识符引用事物,你必须记住它。

另一种方法是使用类或结构。当然有很多方法可以做到这一点,但有些方法包括:

public class Things
{
    public double worstPrice = 6.47;
    public double bestPrice = 0.99;
    public double CivetCatPrice =29.14;
    public double whenPrice = 10.50;
    public double everythingPrice = 319.56;
    public int bestStock = 3238;
    public int worstStock = 8;
    public int civetCatstock = 3;
    public int whenStock = 37;
    public int everythingStock = 2;
}

另一种方式是:

public class Things
{
    public double WorstPrice { get; readonly set; }
    public double BestPrice = { get; readonly set; }
    // etc

    public Things(double worstPrice, double bestPrice) // etc
    {
        WorstPrice = worstPrice;
        BestPrice = bestPrice;
    }
}

两种方法都有利弊。另一个潜力是使用类/结构的集合来分组事物并以有意义的方式聚合它们。

像:

public class Thing
{
    public string ThingLabel { get; readonly set; }
    public double ThingPrice { get; readonly set; }
    public int ThingQuantity { get; readonly set; }

    // the value of your stock, calculated automatically based on other properties
    public double ThingValue { get { ThingPrice * ThingQuantity; } }

    public Thing(string thingLabel, double thingPrice, int thingQuantity)
    {
        ThingLabel = thingLabel;
        // etc
    }
}

public void DoStuff()
{
    List<Thing> list = new List<Thing>();

    Thing thing = new Thing("Civet cat", 500, 10);

    list.Add(thing);
    list.Add(new Thing("Sea flap flap", 100, 5);
    list.Add(new Thing("Nope Rope", 25, 4);

    Console.WriteLine("The value of {0}'s stock is: {1}", thing.ThingLabel, thing.Value);
}

另一种方法是使用基类并创建不同类型的子类。可能性几乎无穷无尽!您现在必须决定哪种方式最适合您,您以后以及您的潜在团队。