0

我正在上 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;
        }
    }
}
4

3 回答 3

1

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

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

于 2013-10-10T09:56:24.073 回答
0

Use Hashtable. It gives what you want. See the code below.

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();
    }
于 2013-10-10T10:05:43.937 回答
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;
                }
            }
        }
    }
}

看看这个作为起点。

于 2013-10-10T10:47:08.870 回答