How to initialize a DateTime field?

前端 未结 9 1245
Happy的楠姐
Happy的楠姐 2020-12-06 04:01

I am absolutly new in C# (I came from Java) and I have a very stupid problem

I have to initialize some DateTime fields into an object but I have som

9条回答
  •  不知归路
    2020-12-06 05:04

    If you search for the error you get, you'll find:

    This is because, in C#, single quotes ('') denote (or encapsulate) a single character, whereas double quotes ("") are used for a string of characters.

    So you'll try:

    DateTime foo = "2014,02,20";
    

    Which yields:

    Cannot implicitly convert type 'string' to 'System.DateTime'

    Now if you search for that error, you'll find:

    int StartYear = 2012;
    int StartMonth = 06;
    int StartDay = 15;
    
    DateTime dt = new DateTime(StartYear, StartMonth, StartDay);
    

提交回复
热议问题