如何添加新列表到 C# 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 ASP.NET 2,它可能使用 C# 2 编译器(实际上根据您的标题) - 它不支持您正在使用的对象初始值设定项语法。如果可能,请尝试升级以使用 C# 3 编译器 - 或创建一个采用所有相关参数的
Employee
构造函数。因此,像这样的表达式:
将变为
请注意,如果您使用 C# 3 编译器,则无需手动调用
Add
...可以这样写:如果需要,您仍然可以使用对象初始值设定项语法,但集合初始值设定项语法使您无论采用哪种方法构造
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:
would become
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: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.你的代码对我来说编译得很好。您运行的 C# 版本是否支持对象初始值设定项 (v. 3.0+)?
这是使用构造函数的完整代码示例:
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: