Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对象列表未存储项目且未显示任何内容_C#_List_Object - Fatal编程技术网

C# 对象列表未存储项目且未显示任何内容

C# 对象列表未存储项目且未显示任何内容,c#,list,object,C#,List,Object,我试图创建一个计算机对象来保存存储在列表中的数据,当我试图查看任何数据时,它返回时没有输出数据 我创建的对象如下所示: class Computer { string companyName; string locationName; string serverName; string deviceName; string lastOnline; public Computer(string companyName, string locatio

我试图创建一个计算机对象来保存存储在列表中的数据,当我试图查看任何数据时,它返回时没有输出数据

我创建的对象如下所示:

class Computer
{
    string companyName;
    string locationName;
    string serverName;
    string deviceName;
    string lastOnline;

    public Computer(string companyName, string locationName, string serverName, 
        string deviceName, string lastOnline)
    {
        this.companyName = companyName;
        this.locationName = locationName;
        this.serverName = serverName;
        this.deviceName = deviceName;
        this.lastOnline = lastOnline;
    }

    public string CompanyName { get; set; }    
    public string LocationName { get; set; }    
    public string ServerName { get; set; }    
    public string DeviceName { get; set; }
    public string LastOnline { get; set; }
}
我将列表初始化为:

List<Computer> computerList = new List<Computer>();
要输出我的项目,我使用:

foreach (var author in computerList)
{
    Console.WriteLine("Test : {0}", author.LastOnline);
}
有人知道为什么我没有得到任何输出,除了:

测试:
测试:
测试:
测试:
测试:


您的属性设置不完全正确,如注释中所述。尝试这两种方法中的一种,然后删除您拥有的属性和访问方法

1:

公共字符串CompanyName{get;set;}

2:

编辑


或者,正如在另一个答案中提到的,修复构造函数以指向属性而不是局部变量。您应该删除本地变量,以免混淆。

您的属性没有按照注释中所述正确设置。尝试这两种方法中的一种,然后删除您拥有的属性和访问方法

1:

公共字符串CompanyName{get;set;}

2:

编辑


或者,正如在另一个答案中提到的,修复构造函数以指向属性而不是局部变量。您应该删除本地变量,以免混淆。

您正在设置私有变量,以便公共属性仍具有默认值

只需修改构造函数即可设置公共属性:

public Computer(string companyName, string locationName, string serverName, string deviceName, string lastOnline)
        {
            this.CompanyName = companyName;
            this.LocationName = locationName;
            this.ServerName = serverName;
            this.DeviceName = deviceName;
            this.LastOnline = lastOnline;
        }

您正在设置私有变量,以便公共属性仍具有默认值

只需修改构造函数即可设置公共属性:

public Computer(string companyName, string locationName, string serverName, string deviceName, string lastOnline)
        {
            this.CompanyName = companyName;
            this.LocationName = locationName;
            this.ServerName = serverName;
            this.DeviceName = deviceName;
            this.LastOnline = lastOnline;
        }

Computer.lastOnline
Computer.lastOnline
彼此没有关系。您可能希望完全删除前者。您需要编辑属性getter以返回私有实例字段。现在,编译器正在为属性生成新的支持字段。上面的注释是即时的。请阅读此处()以了解有关所发生事件的更多信息。@St.Pat我已尝试获取{return lastOnline;}设置{lastOnline=value;}如果这是您的意思?至于杰伦,你指的是哪一个too@Ollie是的,这就是我所指的。这将使属性返回构造函数
计算机中给定值的私有字段。lastOnline
计算机。lastOnline
彼此没有关系。您可能希望完全删除前者。您需要编辑属性getter以返回私有实例字段。现在,编译器正在为属性生成新的支持字段。上面的注释是即时的。请阅读此处()以了解有关所发生事件的更多信息。@St.Pat我已尝试获取{return lastOnline;}设置{lastOnline=value;}如果这是您的意思?至于杰伦,你指的是哪一个too@Ollie是的,这就是我所指的。这将使属性返回构造函数中给定值的私有字段
public Computer(string companyName, string locationName, string serverName, string deviceName, string lastOnline)
        {
            this.CompanyName = companyName;
            this.LocationName = locationName;
            this.ServerName = serverName;
            this.DeviceName = deviceName;
            this.LastOnline = lastOnline;
        }