Код: Выделить всё
Hello, World!
Else: Bad Date Format! 'abc'
01.01.0001
< /code>
Так ясно, что он использует значение по умолчанию struct. Но каким -то образом это больше не является случаем для .net 8 (я не нашел это до сих пор, по крайней мере, здесь). Текущий метод
(не упоминает версии)internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
TryParse("abc", out DateOnly d);
Console.WriteLine(d);
Console.ReadKey();
}
public static bool TryParse(string s, out DateOnly dateOnly)
{
try
{
if (s is not null && s.Length == 8)
{
int yyyy = int.Parse(s[0..4]);
int mm = int.Parse(s[4..6]);
int dd = int.Parse(s[6..8]);
var date = new DateOnly(yyyy, mm, dd);
dateOnly = date;
return true;
}
else
{
Console.WriteLine("Else: Bad Date Format! '" + s + "'");
return false; //CS0177 in .net8
}
}
catch
{
Console.WriteLine("Catch: Bad Date Format! '" + s + "'");
return false; //CS0177 in .net8
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/789 ... t-8-cs0177