下面的代碼是否正確?正確設置C中TimeSpan.MaxValue的時間的方法#
[WebMethod]
[ScriptMethod]
public bool DoPost(CommunityNewsPost post)
{
MembershipHelper.ThrowUnlessAtLeast(RoleName.Administrator);
DateTime? start;
DateTime? end;
Utility.TryParse(post.PublishStart, out start);
Utility.TryParse(post.PublishEnd, out end);
if (start != null)
start -= start.Value.TimeOfDay - TimeSpan.MinValue;
if(end!=null)
end += TimeSpan.MaxValue - end.Value.TimeOfDay;
return CommunityNews.Post(post.Title, post.Markdown, post.CategoryId, start, end);
}
而且Utility.TryParse
:
public static bool TryParse(string s, out DateTime? result)
{
DateTime d;
var success = DateTime.TryParse(s, out d);
if (success)
result = d;
else
result = default(DateTime?);
return success;
}
我想start
是像09/11/2011 00:00
和end
是像09/11/2011 23:59
您不使用'Utility.TryParse()'的返回值,它包含在'result'中。我認爲你應該讓它返回結果。 – svick
我不使用它*這次*,我**可能**將來使用它。此外,每個TryParse方法都遵循這種模式。 – bevacqua
這就是每個'TryParse()'遵循的模式*,因爲它不返回可爲空的值*。你需要一些方法來表示失敗。 'TryParse'使用'false'的返回值,你可以使用'null'。 – svick