You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在构造函数中设置DateTime值?解决Customer实例化DateTime输入失败问题

How to Set DateTime Values in a C# Constructor

Hey there! Let's fix that DateTime issue in your Customer class. From the code snippet you shared, it sounds like either your constructor isn't set up to accept a DateTime parameter, or you're not passing the value correctly from your main method. Let's break this down step by step.

Step 1: Update Your Customer Class to Include DateTime

First, let's complete your Customer class with a DateTime field, property, and a constructor that accepts this value:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

class Customer 
{
    // String property you started
    private string _LastName;
    public string LastName 
    { 
        get { return _LastName; } 
        set { _LastName = value; } 
    }

    // Add DateTime field and property
    private DateTime _RegistrationDate;
    public DateTime RegistrationDate 
    { 
        get { return _RegistrationDate; } 
        set { _RegistrationDate = value; } 
    }

    // Constructor that accepts both LastName and DateTime
    public Customer(string lastName, DateTime registrationDate)
    {
        _LastName = lastName;
        // Assign the DateTime value to your private field
        _RegistrationDate = registrationDate;
    }
}

Step 2: Properly Pass DateTime Values in Your Main Method

Now in your main method, you can create Customer instances using valid DateTime values in a few safe ways:

class Program
{
    static void Main(string[] args)
    {
        // 1. Use the current date/time with DateTime.Now
        Customer currentCustomer = new Customer("Doe", DateTime.Now);

        // 2. Create a specific date using the DateTime constructor (year, month, day)
        Customer pastCustomer = new Customer("Smith", new DateTime(2024, 3, 10));

        // 3. Parse a string to DateTime (use TryParse to avoid exceptions)
        // Standard ISO format (yyyy-MM-dd)
        if (DateTime.TryParse("2024-05-15", out DateTime isoDate))
        {
            Customer isoCustomer = new Customer("Johnson", isoDate);
        }

        // 4. Parse a custom format (e.g., dd/MM/yyyy) with TryParseExact
        string customDate = "15/05/2024";
        if (DateTime.TryParseExact(
            customDate, 
            "dd/MM/yyyy", 
            null, 
            System.Globalization.DateTimeStyles.None, 
            out DateTime customParsedDate))
        {
            Customer customCustomer = new Customer("Williams", customParsedDate);
        }
    }
}

Common Pitfalls to Avoid

  • Missing DateTime in Constructor: If your original constructor didn't have a DateTime parameter, you couldn't pass one in—adding that parameter is key.
  • Direct String-to-DateTime Conversion: You can't just pass a string like "15/05/2024" directly to the constructor; you need to parse it to a DateTime object first.
  • Unsafe Parsing: Use DateTime.TryParse or TryParseExact instead of DateTime.Parse—they won't throw exceptions if the input format is invalid, making your code more robust.
  • Mismatched Date Formats: When using TryParseExact, make sure your format string exactly matches the input string (e.g., "MM/dd/yyyy" for month/day/year vs "dd/MM/yyyy" for day/month/year).

内容的提问来源于stack exchange,提问作者Xander

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:21:13