c# - 将对象存储在数组中然后访问它们

标签 c# arrays string oop object

我将以下代码编写为快速而肮脏的 POC,一切正常,直到我尝试访问我创建的数组中的对象。

正如您在下面的代码中看到的,我尝试使用以下代码行访问数组中的对象:

Console.WriteLine("AWS Account Id = {0]", array1[1].AccountId);

完整代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class AWSAccount
    {
        public string AccountId { get; set; }
        public string[] Instances { get; set; }
        public AWSAccount(string accountId, string[] instances)
        {
            AccountId = accountId;
            Instances = instances;
        }
    }

    class Program
    {
        static void Main()
        {
            string[] instances1 = {"i-xxxxx01", "i-xxxxx02"};
            AWSAccount account1 = new AWSAccount("53853288254", instances1);
            Console.WriteLine("AWS Account Id = {0} Instances = {1} {2}", account1.AccountId, account1.Instances[0], account1.Instances[1]);

            string[] instances2 = { "i-zzzzz01", "i-zzzzz02" };
            AWSAccount account2 = new AWSAccount("74378834238", instances2);
            Console.WriteLine("AWS Account Id = {0} Instances = {1} {2}", account2.AccountId, account2.Instances[0], account2.Instances[1]);


            object[] array1 = new object[2];
            array1[0] = account1;
            array1[1] = account2;

            Console.WriteLine("AWS Account Id = {0}", array1[0].AccountId);
            Console.WriteLine("AWS Account Id = {0}", array1[1].AccountId);

            // Keep the console open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();

        }
    }
}

智能感知未拾取 .AccountId,并通过以下错误突出显示它。

Error   1   'object' does not contain a definition for 'AccountId' and no extension method 'AccountId' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) c:\Users\jploof\Documents\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs    37  65  ConsoleApplication1

最佳答案

使用 AWSAccount[] array1 = new AWSAccount[2] 更正您的 Main() 过程,如下所示:

static void Main(string[] args)
{

    string[] instances1 = { "i-xxxxx01", "i-xxxxx02" };
    AWSAccount account1 = new AWSAccount("53853288254", instances1);
    Console.WriteLine("AWS Account Id = {0} Instances = {1} {2}", account1.AccountId, account1.Instances[0], account1.Instances[1]);

    string[] instances2 = { "i-zzzzz01", "i-zzzzz02" };
    AWSAccount account2 = new AWSAccount("74378834238", instances2);

    Console.WriteLine("AWS Account Id = {0} Instances = {1} {2}", account2.AccountId, account2.Instances[0], account2.Instances[1]);

    AWSAccount[] array1 = new AWSAccount[2];
    array1[0] = account1;
    array1[1] = account2;

    Console.WriteLine("AWS Account Id = {0}", array1[0].AccountId);
    Console.WriteLine("AWS Account Id = {0}", array1[1].AccountId);

    // Keep the console open in debug mode.
    Console.WriteLine("Press any key to exit.");
    Console.ReadKey();
}

注意原始帖子中的语法错误:“AWS 账户 ID = {0]”

希望这会有所帮助。

关于c# - 将对象存储在数组中然后访问它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36230907/

相关文章:

javascript - 在javascript中删除多个对象键值

php - 如何在 MySQL 语句中包含 PHP 变量

c# - Windows 服务器服务总线。请求 URI 格式不正确

arrays - 用中间的最大值填充二维数组的算法

c# - 使用 Linq 将带有嵌套列表的列表转换为单独的列表

ruby-on-rails - 有没有办法通过数组将多个参数输入到方法中

c++ - 为什么此c++代码打印出长度为5,而当我打印出字符串时,程序会自动终止?

c++ - 大数求和

c# - SQL Server 的 C# 中带条件的批量复制

c# - 使用 C# 允许在 XML 序列化中重复节点名称