如评论中所述,您可以制作仅包含您需要的数据的构造函数,即:
public User(string firstName, string sirname)
{
FirstName = firstName,
Surname = sirname,
AddedDateTime = DateTime.Now,
ModifiedDateTime = DateTime.Now
};
您还可以使用辅助方法来创建类的实例。它们可能在课堂内或不在课堂内(如果你无法控制课堂本身,这很有用)
public static User CreateUser(string firstName, string sirname)
{
User newUser = new User();
newUser.FirstName = firstName,
newUser.Surname = sirname,
newUser.AddedDateTime = DateTime.Now,
newUser.ModifiedDateTime = DateTime.Now
return newUser;
};
你也可以有类似的东西:
public static User CreateUser(string firstName, string sirname, string otherdata)
{
User newUser = new User();
newUser.FirstName = firstName,
newUser.Surname = sirname,
newUser.AddedDateTime = DateTime.Now,
newUser.ModifiedDateTime = DateTime.Now,
newUser.SomeField = otherdata,
newUser.SomeOtherField = otherdata
return newUser;
};