首页 > 解决方案 > 数组中的 C# 类

问题描述

这是我的程序。

    using System;
    using System.Linq;

    namespace object_in_array
    {
        class objekt
        {
            public string test = "succes";
        }
    class Program
    {
        static void Main(string[] args)
        {
            object[] test = new object[5];

            objekt yes = new objekt();
            test[0] = yes;

            Console.WriteLine(test[0].test);
        }
    }
    }

我想yes.test从我放入测试对象的数组中返回。有人可以帮忙吗

标签: c#

解决方案


你可能打算做类似的事情

((objekt)test[0]).test = "yes";

那你就完全错了,@corentin 发表的评论是正确的。你会做

    static void Main(string[] args)
    {
        objekt[] test = new objekt[5];
        objekt yes = new objekt();
        test[0] = yes;
        Console.WriteLine(test[0].test);
    }

推荐阅读