具有数组的类,其中键是字符串并且采用整数,双精度和字符串

时间:2013-10-10 09:47:53

标签: c#

我正在做一个C#在线自学课程,并且有一项我无法弄清楚的作业。 我有一个半就绪的应用程序,我需要创建一个类和所有成员,以使其工作。在“类应用程序”下我无法改变任何内容。这是我给出的模板:

using System;

namespace ObjectOriented
{
// Your Code Here

class Application
{
    static void Main()
    {
        TypeCollection collection = new TypeCollection(3);
        collection["integer"] = 123;
        collection["double"] = 456.78;
        collection["string"] = "Hello world!";

        double sum = (double)(int)collection["integer"] + (double)collection["double"];
        Console.WriteLine("The sum of all numbers is {0}", sum);

        Console.WriteLine();

        TypeCollection collection2 = new TypeCollection(2);
        collection2["integer"] = 123;
        collection2["double"] = 456.78;
        collection2["string"] = "Hello world!";
    }
}
}

这是打印的内容:

 Added integer = 123
 Added double = 456.78
 Added string = Hello world!
 The sum of all numbers is 579.78

 Added integer = 123
 Added double = 456.78
 Collection is full

我最大的问题是如何使用键作为字符串创建数组。这就是我尝试过的,但我无法接受字符串作为键。

public class TypeCollection
{
    private object[] colType;

    public TypeCollection(object length)
    {
        this.tyyppi = new object[(int) length];
    }
    public object this[string key]
    {
        get
        {
            return colType[key];
        }
        set
        {
            colType[key] = value;
        }
    }
}

3 个答案:

答案 0 :(得分:1)

由于这是一项作业,我会尝试提供一些提示,而不是给出完整的答案。在这种情况下,[]运算符不仅可以用于C#中的数组。该类也称为TypeCollection,因此您应该查看C#中的一些集合文档,以找到满足您需求的集合。

http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx

答案 1 :(得分:0)

使用Hashtable。它提供了你想要的东西。请参阅下面的代码。

static void Main(string[] args)
    {
        Hashtable collection = new Hashtable();

        collection["integer"] = 123;
        collection["double"] = 456.78;
        collection["string"] = "Hello world!";

        double sum = (double)(int)collection["integer"] + (double)collection["double"];
        Console.WriteLine("The sum of all numbers is {0}", sum);

        Console.WriteLine();
    }

答案 2 :(得分:0)

如果你想要只使用数组和类的东西,你必须使用这样的东西:

public class TypeCollection
{
    class KeyValue // define your key / value object to store the data
    {
        public string Key
        {
            get;
            set;
        }

        public object Value
        {
            get;
            set;
        }
    }

    private KeyValue[] colType;

    public TypeCollection(object length) // why use object instead of int???
    {
        this.colType = new KeyValue[(int)length];
    }
    public object this[string key]
    {
        get
        {
            for (var i = 0; i < colType.Length; i++) // find the key inside the array
            {
                if (colType[i].Key == key)
                {
                    return colType[i].Value;
                }
            }

            return null;
        }
        set
        {
            // this should add new elements so you have to resize the array etc.
            for (var i = 0; i < colType.Length; i++)
            {
                if (colType[i].Key == key)
                {
                    colType[i].Value = value;

                    return;
                }
            }
        }
    }
}

以此为出发点。