如何添加新列表到 C# 2.0 中的另一个列表?

发布于 2024-09-30 17:11:27 字数 1378 浏览 2 评论 0原文

我只是将项目添加到列表中,但无法使其工作。它不断抛出预期的错误;,预期的)。

using System;
using System.Collections.Generic;

public class Employee
{
    private int _id;
    private string _FName;
    private string _MName;
    private string _LName;
    private DateTime _DOB;
    private char _sex;

public int ID { get { return _id; } set { _id=value; } }
public string FName{get{return _FName;}set{_FName=value;}}
public string MName { get { return _MName; } set { _MName = value; } }
public string LName { get { return _LName; } set { _LName = value; } }
public DateTime DOB { get { return _DOB; } set { _DOB = value; } }
public char Sex { get { return _sex; } set { _sex = value; } }

public List<Employee> GetEmployeeList()
{
    List<Employee> empList=new List<Employee>();
    empList.Add(new Employee() { ID = 1, FName = "John", MName = "", LName = "Shields", DOB = DateTime.Parse("12/11/1971"), Sex = 'M' });
    empList.Add(new Employee() { ID = 2, FName = "Mary", MName = "Matthew", LName = "Jacobs", DOB = DateTime.Parse("01/17/1961"), Sex = 'F' });
    empList.Add(new Employee() { ID = 3, FName = "Amber", MName = "Carl", LName = "Agar", DOB = DateTime.Parse("12/23/1971"), Sex = 'M' });
    empList.Add(new Employee() { ID = 4, FName = "Kathy", MName = "", LName = "Berry", DOB = DateTime.Parse("11/15/1976"), Sex = 'F' });
    return empList;
}
}

I am just adding items to a list but can't get it to work. It keeps throwing errors expected ;, expected ).

using System;
using System.Collections.Generic;

public class Employee
{
    private int _id;
    private string _FName;
    private string _MName;
    private string _LName;
    private DateTime _DOB;
    private char _sex;

public int ID { get { return _id; } set { _id=value; } }
public string FName{get{return _FName;}set{_FName=value;}}
public string MName { get { return _MName; } set { _MName = value; } }
public string LName { get { return _LName; } set { _LName = value; } }
public DateTime DOB { get { return _DOB; } set { _DOB = value; } }
public char Sex { get { return _sex; } set { _sex = value; } }

public List<Employee> GetEmployeeList()
{
    List<Employee> empList=new List<Employee>();
    empList.Add(new Employee() { ID = 1, FName = "John", MName = "", LName = "Shields", DOB = DateTime.Parse("12/11/1971"), Sex = 'M' });
    empList.Add(new Employee() { ID = 2, FName = "Mary", MName = "Matthew", LName = "Jacobs", DOB = DateTime.Parse("01/17/1961"), Sex = 'F' });
    empList.Add(new Employee() { ID = 3, FName = "Amber", MName = "Carl", LName = "Agar", DOB = DateTime.Parse("12/23/1971"), Sex = 'M' });
    empList.Add(new Employee() { ID = 4, FName = "Kathy", MName = "", LName = "Berry", DOB = DateTime.Parse("11/15/1976"), Sex = 'F' });
    return empList;
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

永言不败 2024-10-07 17:11:27

如果您使用 ASP.NET 2,它可能使用 C# 2 编译器(实际上根据您的标题) - 它不支持您正在使用的对象初始值设定项语法。如果可能,请尝试升级以使用 C# 3 编译器 - 或创建一个采用所有相关参数的 Employee 构造函数。

因此,像这样的表达式:

new Employee() { ID = 1, FName = "John", MName = "", LName = "Shields", 
                 DOB = DateTime.Parse("12/11/1971"), Sex = 'M' }

将变为

new Employee(1, "John", "", "Shields", "DOB", 'M')

请注意,如果您使用 C# 3 编译器,则无需手动调用 Add...可以这样写:

var employees = new List<Employee>
{
    new Employee(... stuff here...),
    new Employee(... stuff here...)
};

如果需要,您仍然可以使用对象初始值设定项语法,但集合初始值设定项语法使您无论采用哪种方法构造 Employee 对象都变得更加简单。

If you're using ASP.NET 2, it's possibly using the C# 2 compiler (as per your title, actually) - which doesn't support the object initializer syntax you're using. If possible, try to upgrade to use the C# 3 compiler - or create an Employee constructor taking all of the relevant parameters.

So an expression like this:

new Employee() { ID = 1, FName = "John", MName = "", LName = "Shields", 
                 DOB = DateTime.Parse("12/11/1971"), Sex = 'M' }

would become

new Employee(1, "John", "", "Shields", "DOB", 'M')

Note that if you were using the C# 3 compiler, you wouldn't have to have all those manual calls to Add... you could just write:

var employees = new List<Employee>
{
    new Employee(... stuff here...),
    new Employee(... stuff here...)
};

You could still use object initializer syntax if you wanted, but the collection initializer syntax makes it simpler whatever approach you take to constructing Employee objects.

云醉月微眠 2024-10-07 17:11:27

你的代码对我来说编译得很好。您运行的 C# 版本是否支持对象初始值设定项 (v. 3.0+)?

这是使用构造函数的完整代码示例:

using System;
using System.Collections.Generic;

public class Employee
{
    private int _id;
    private string _FName;
    private string _MName;
    private string _LName;
    private DateTime _DOB;
    private char _sex;

    public int ID { get { return _id; } set { _id = value; } }
    public string FName { get { return _FName; } set { _FName = value; } }
    public string MName { get { return _MName; } set { _MName = value; } }
    public string LName { get { return _LName; } set { _LName = value; } }
    public DateTime DOB { get { return _DOB; } set { _DOB = value; } }
    public char Sex { get { return _sex; } set { _sex = value; } }

    public Employee(int id, string fname, string mname, string lname, DateTime dob, char sex)
    {
        ID = id;
        FName = fname;
        MName = mname;
        LName = lname;
        DOB = dob;
        Sex = sex;
    }

    public List<Employee> GetEmployeeList()
    {
        List<Employee> empList = new List<Employee>();
        empList.Add(new Employee(1, "John", "", "Shields", DateTime.Parse("12/11/1971"), 'M'));
        //etc
        return empList;
    }
}

Your code compiles just fine for me. Are you running a version of C# that supports object initializers (v. 3.0+)?

Here is the full code sample using a constructor:

using System;
using System.Collections.Generic;

public class Employee
{
    private int _id;
    private string _FName;
    private string _MName;
    private string _LName;
    private DateTime _DOB;
    private char _sex;

    public int ID { get { return _id; } set { _id = value; } }
    public string FName { get { return _FName; } set { _FName = value; } }
    public string MName { get { return _MName; } set { _MName = value; } }
    public string LName { get { return _LName; } set { _LName = value; } }
    public DateTime DOB { get { return _DOB; } set { _DOB = value; } }
    public char Sex { get { return _sex; } set { _sex = value; } }

    public Employee(int id, string fname, string mname, string lname, DateTime dob, char sex)
    {
        ID = id;
        FName = fname;
        MName = mname;
        LName = lname;
        DOB = dob;
        Sex = sex;
    }

    public List<Employee> GetEmployeeList()
    {
        List<Employee> empList = new List<Employee>();
        empList.Add(new Employee(1, "John", "", "Shields", DateTime.Parse("12/11/1971"), 'M'));
        //etc
        return empList;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文