简体   繁体   English

ToString的重载方法没有2个参数

[英]No overload method for ToString takes 2 arguments

I am trying to format a datetime to a string of the form "c", by using the ToString method: 我试图通过使用ToString方法将日期时间格式化为“c”形式的字符串:

string end = evt.End.DateTime.ToString("c", DataController.culture);

DataController.culture is a reference to a valid culture object and evt is an Event object from the Google Calendar v3 API. DataController.culture是对有效文化对象的引用,evt是来自Google Calendar v3 API的Event对象。 I am sure DateTime is of the DatetTime class because the debugger says so in the type of the object. 我确信DateTime属于DatetTime类,因为调试器在对象的类型中这样说。

However I keep getting the issue mentioned in the title, why? 但是我不断得到标题中提到的问题,为什么? Even the MSDN docs show how to use that method but somehow I can't: 即使MSDN文档显示如何使用该方法,但不知何故我不能:

Console.WriteLine(date1.ToString("d MMMM", 
                  CultureInfo.CreateSpecificCulture("es-MX")));
// Displays 29 agosto    

The API documentation indicates that the property you're attempting to ToString() is actually a System.Nullable<DateTime> , which has different usability considerations from a normal DateTime . API文档表明您尝试ToString()的属性实际上是System.Nullable<DateTime> ,它具有与普通DateTime不同的可用性考虑因素。 In order to invoke the ToString method you wish, you need to retrieve a concrete DateTime struct from the nullable one provided to you - in your case, by doing the following: 为了调用您希望的ToString方法,您需要从提供给您的可为空的方法中检索具体的DateTime结构 - 在您的情况下,通过执行以下操作:

string end = evt.End.DateTime.Value.ToString("c", DataController.culture);

Mind you, you normally will want to first check that your nullable object (eg. evt.End.DateTime ) actually has a value by checking the HasValue property before invoking ToString() or any other method or property. 请注意,通常在调用ToString()或任何其他方法或属性之前,通过检查HasValue属性,首先检查您的可空对象(例如, evt.End.DateTime )是否实际具有值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM