C# 如何使用三元运算符检查空值或日期时间值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18821690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:18:36  来源:igfitidea点击:

How to check null or datetime value using ternary operator?

c#asp.netternary-operator

提问by Neo

I want to check passed value is null or datetime value using ternary operator in c#?

我想在 c# 中使用三元运算符检查传递的值是空值还是日期时间值?

I tried like this

我试过这样

fromDate == null ? null : Convert.ToDateTime(fromDate)

getting error:

得到错误:

type of conditional expression cannot be determined

I want to check whether variable fromDateis null or having date time value ?

我想检查变量是否fromDate为空或具有日期时间值?

variable fromDate is coming from Querystring and type of string.

变量 fromDate 来自 Querystring 和字符串类型。

采纳答案by Soner G?nül

From ?: Operator:

来自?: Operator

Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.

first_expression 和 second_expression 的类型必须相同,或者必须存在从一种类型到另一种类型的隐式转换。

condition ? first_expression : second_expression;

Convert.ToDateTimereturns DateTimeand there is no implicit conversionbetween nulland DateTime. And the conditional operatoris an expression and that needs a return type.

Convert.ToDateTime返回DateTime并且在和之间没有隐式转换。而条件运算符是一个表达式,并且需要一个返回类型。nullDateTime

One option seems logical to me using DateTime.TryParse(which returns boolean) as a first expressionand use another boolean (trueor false) as a second expression. Damiths' answerseems logical.

一个选项对我来说似乎合乎逻辑,使用DateTime.TryParse(返回boolean)作为第一个表达式并使用另一个布尔值(truefalse)作为第二个表达式达米斯的回答似乎合乎逻辑。

Or instead you can use nullable DateTime like DateTime?

或者,您可以使用可为空的 DateTime 之类的 DateTime?

DateTime? foo;

if(foo.HasValue)
{
   //Nullable DateTime has a value..
}

回答by Hamlet Hakobyan

romDate == null ? null : Convert.ToDateTime(fromDate)

nulland Convert.ToDateTime(fromDate)haven't common type. Compiler must be able to cast both expressions in canditional operator to same type.

null并且Convert.ToDateTime(fromDate)没有共同类型。编译器必须能够将条件运算符中的两个表达式转换为相同的类型。

回答by Ali Baghdadi

you must use String.IsNullOrEmpty to check if fromDate is null like this:

您必须使用 String.IsNullOrEmpty 来检查 fromDate 是否为空,如下所示:

DateTime? date = String.IsNullOrEmpty(fromDte) ? null : (DateTime?)Convert.ToDateTime(fromDate)

回答by Damith

if you have string value for fromDatedo as below

如果你有字符串值fromDate,如下做

DateTime dt;

bool isValidDate =  String.IsNullOrEmpty(fromDate) ? false : DateTime.TryParse(fromDate, out dt); 

if you know the datetime format/formats which your input having you better use DateTime.TryParseExactmethod

如果您知道输入的日期时间格式/格式让您更好地使用DateTime.TryParseExact方法

if fromDate is DateTime, then simple you can check as below

如果fromDate是DateTime,那么简单你可以检查如下

fromDate.HasValue

you don't need ?: Operator

你不需要 ?: Operator

回答by Sain Pradeep

I hope it will help you

我希望它会帮助你

   string format = "ddd dd MMM h:mm tt yyyy";
    DateTime dateTime;

    fromDate=(DateTime.TryParseExact(fromDate, format, CultureInfo.InvariantCulture,
        DateTimeStyles.None, out dateTime))?dateTime:null;

回答by Lummo

It looks like the main problem is that you are trying to assign nullto a DateTime

看起来主要问题是您试图分配nullDateTime

DateTime is a structure and not a reference type so this can't be done.

DateTime 是一个结构而不是一个引用类型,所以这不能完成。

Either use a nullable DateTime (DateTime?) or a specific value to indicate null, such as DateTime.MinValue

使用可为空的 DateTime ( DateTime?) 或特定值来指示空值,例如DateTime.MinValue

Have a look here for an example: http://www.dotnetperls.com/nullable-datetime

看看这里的一个例子:http: //www.dotnetperls.com/nullable-datetime

回答by TK-421

The problem is that Convert.ToDateTime(fromDate) is of type DateTime, which cannot accept a null value, that is why this code won't work in this form. You have two choices to make. Either change it to regular if:

问题是 Convert.ToDateTime(fromDate) 是 DateTime 类型,它不能接受空值,这就是为什么这段代码不能在这种形式下工作。你有两个选择。如果出现以下情况,请将其更改为常规:

if(fromDate != null)
{
        Convert.ToDateTime(fromDate)
}

Or cast DateTime to nullable DateTime:

或将 DateTime 转换为可为空的 DateTime:

    fromDate == null ? null : (DateTime?)Convert.ToDateTime(fromDate)

Ternary operator in C# needs both values to be of the same type and it is explained here.

C# 中的三元运算符需要两个值的类型相同,这里解释。