c# - 从返回 DateTime 的 Function 返回 null

标签 c# asp.net

 public static DateTime ResolveDate()
 {
   return null;
 }

我需要从返回 DateTime 类型的函数返回一个 null 值。我的主要目标是不使用 MinValue或返回一个 new Datetime(); [返回默认日期] 并且前端将日期显示为空值(空白)-> ""

最佳答案

public static DateTime? ResolveDate() 
{ 
    if ( someconditon = true )
    {
        return DateTime.Now
    }
    else
    {
        return null; 
    } 
}

另一种方法是使用类似 TryParse 的东西

Public static bool TryResolve (out Resolvedate)
{
    if ( someconditon = true ) 
    { 
        Resolvedate = DateTime.Now 
        return true;
    } 
    else 
    {
        return false;  
    }  
}

关于c# - 从返回 DateTime 的 Function 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8601774/

相关文章:

javascript - 如何使用 javascript 在 ASP.NET Web 表单上动态添加文本框

javascript - 无法访问中继器中自动完成的文本框

c# - CoCreateInstance 返回 "Class not registered"

c# - 获取分配给单个用户的数据

c# - ASP.Net MVC3 模型绑定(bind)错误

c# - 旋转后获取坐标

javascript - 如何使用 Javascript 在 Gridview 中设置来自数据库的文本区域行数

asp.net - 奇怪的VS2005编译错误: unable to locate resource file (because the compiler keeps deleting it)

C# 模拟技术

c# - 哪个 C# 连接器支持嵌入式 MySql?