下面的代码是否正确?正确设置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