如何在特定的类构造函数中正确实现DateTime?

时间:2019-04-16 20:04:58

标签: c#

我有一个名为“ Employee”的类,其中包含以下变量:

private string name;
private DateTime birthday;
private DateTime dateOfEmployment;
private string address;
private double salary;

我的构造函数如下:

public Employee(string name = "defaultName", DateTime birthday = new DateTime(), DateTime dateOfEmployment = new DateTime(), string address = "defaultAddress", double salary = 1000)
{
    this.name = name;
    this.birthday = birthday;
    this.dateOfEmployment = dateOfEmployment;
    this.address = address;
    this.salary = salary;                    
}

如何正确实现DateTime,以使我不必在每次创建新的“ Employee”对象时都使用“ Convert.ToDateTime()”? 我可以遵循某种“最佳实践”吗?或者在这种情况下,您对如何使用DateTime有任何提示吗?

我是C#的新手(如您所见),我想学习如何使用DateTime。对我来说,仅将时间保存在字符串中是不可行的。

2 个答案:

答案 0 :(得分:1)

将员工日期存储为DateTime是正确的,因此您处在正确的轨道上。

关于您的员工构造函数:您似乎正在尝试默认所有值。这真的是您想要做的吗?通常,这种构造函数将强制调用方指定所有值,并且由调用方来发送已创建的DateTime。

如果您真的想默认日期时间,请考虑使用只读值DateTime.MinValue

或者,您可以使用DateTime附带的构造函数之一。生日那天,您最有可能的候选人是var birthday = new DateTime(year, month, day);

就“最佳实践”而言,应该在其他地方(可能是UI)创建DateTime,并将其作为参数发送给构造函数,而无需使用默认值。

答案 1 :(得分:0)

您可以将.gallery { a { display: inline-block; img { display: block; } } } 合并到您的ctor中:

DateTime.Parse

用法:

public class Employee
{
    private static readonly string[] SupportedDateFormats = new[] { "yyyy-MM-dd", /* add more as you see fits */};

    public string name;
    public DateTime birthday;
    public DateTime dateOfEmployment;
    public string address;
    public double salary;

    public Employee(string name = "defaultName", DateTime birthday = new DateTime(), DateTime dateOfEmployment = new DateTime(), string address = "defaultAddress", double salary = 1000)
    {
        this.name = name;
        this.birthday = birthday;
        this.dateOfEmployment = dateOfEmployment;
        this.address = address;
        this.salary = salary;   
    }
    public Employee(string name = "defaultName", string birthdayText = null, string dateOfEmploymentText = null, string address = "defaultAddress", double salary = 1000)
    {
        this.name = name;
        this.address = address;
        this.salary = salary;

        // or, you can use your language/country-speficic culture 
        // if you need to parse text like, "April 16, 19" or "16 Juin 19" (french)
        var culture = CultureInfo.InvariantCulture;

        this.birthday = string.IsNullOrEmpty(birthdayText)
            ? new DateTime()
            : DateTime.TryParseExact(birthdayText, SupportedDateFormats, culture, DateTimeStyles.None, out var birthdayValue)
                ? birthdayValue : throw new FormatException($"Invalid `{nameof(birthdayText)}` format: {birthdayText}");
        this.dateOfEmployment = string.IsNullOrEmpty(dateOfEmploymentText)
           ? new DateTime()
           : DateTime.TryParseExact(dateOfEmploymentText, SupportedDateFormats, culture, DateTimeStyles.None, out var dateOfEmploymentValue)
               ? dateOfEmploymentValue : throw new FormatException($"Invalid `{nameof(dateOfEmploymentText)}` format: {dateOfEmploymentText}");
    }
}