我来自Java语言背景,这可能是一件好事,尽管到目前为止,事实证明这是一件坏事。

在Javascript中,我有:

function doStuff(){
    var temp = new Array();
    temp.push(new marker("bday party"));
    alert(temp[0].whatsHappening); // 'bday party' would be alerted
}

function marker(whatsHappening) {
    this.whatsHappening = whatsHappening;
}

我现在想在C#中做同样的事情。我开设了一个类(class):
class marker
{
    public string whatsHappening;

    public marker(string w) {
        whatsHappening = w;
    }
}

并添加一个新对象,但是我似乎无法以相同的方式调用数据:
ArrayList markers = new ArrayList();

markers.Add(new marker("asd"));
string temp = markers[0].whatsHappening; // This last bit isn't allowed

最佳答案

请改用通用 List<T> :

List<marker> markers = new List<marker>();
markers.Add(new marker("asd"));
string temp = markers[0].whatsHappening;

07-24 16:22