wpf “日期时间”之间的区别?和“日期时间”。(试图从日期选择器获取价值)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16397228/
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-09-13 08:44:09  来源:igfitidea点击:

Difference between "DateTime?" and "DateTime". (trying to get value from datepicker)

c#wpfvisual-studio-2010datetimedatetimepicker

提问by Ms. Nobody

I'm quite new to WPFand C#so don't blame me for asking this maybe silly question.

我对WPFC#陌生,所以不要怪我问这个可能很愚蠢的问题。

I have my WPF app with two datepickers. I want to get the DateTime out of them when it changes and to use it as my variable for some other stuff in the app. So I have for each of them something like this(method was automatically generated by VS):

我的 WPF 应用程序带有两个日期选择器。我想在 DateTime 更改时从中取出 DateTime,并将其用作应用程序中其他一些内容的变量。所以我对他们每个人都有这样的事情(方法是由 VS 自动生成的):

private void datePicker1_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            date1 = datePicker1.SelectedDate;
        }

but the problem is that the date in the datepicker is format DateTime?not DateTimeand I really don't know what does that question mark there mean and why it is there. I tried some research but didn't find anything that would help me. If u see some better way of getting the date from that datepicker u can help me with it too. I just need it in my xaml.cs code not in xaml and I'm not really into using bindings cause I'm not sure if it works how I need in this case.

但问题是日期选择器中的日期格式为DateTime?不是DateTime,我真的不知道那里的问号是什么意思以及它为什么在那里。我尝试了一些研究,但没有找到任何对我有帮助的东西。如果您看到从日期选择器获取日期的更好方法,您也可以帮助我。我只需要在我的 xaml.cs 代码中而不是在 xaml 中使用它,我并不是真的喜欢使用绑定,因为我不确定在这种情况下它是否可以正常工作。

Thanks for any answer.

感谢您的任何回答。

Edit:I would like to add information that it shows me this error:

编辑:我想添加显示此错误的信息:

Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)

无法隐式转换类型“System.DateTime?” 到'System.DateTime'。存在显式转换(您是否缺少演员表?)

回答by Habib

DateTime with ?is NullableDateTime, it can hold null values. For your case you can do :

DateTime with?NullableDateTime,它可以保存空值。对于您的情况,您可以执行以下操作:

private void datePicker1_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
    if(datePicker1.SelectedDate.HasValue)
         date1 = datePicker1.SelectedDate.Value;
}

Nullable<T>C#

Nullable<T>C#

In C# and Visual Basic, you mark a value type as nullable by using the ? notation after the value type. For example, int? in C# or Integer? in Visual Basic declares an integer value type that can be assigned null.

在 C# 和 Visual Basic 中,您可以使用 ? 值类型后的符号。例如,整数?在 C# 或整数?在 Visual Basic 中声明了一个可以赋值为 null 的整数值类型。

回答by Obama

A nullable DateTime can be null.

可为空的 DateTime 可以为空。

The DateTimestruct itself does not provide a null option. But the DateTime?nullable type allows you to assign the null literal to the DateTimetype. It provides another level of indirection in the object model.

DateTime结构本身不提供一个空的选择。但是DateTime?可空类型允许您将空文字分配给该DateTime类型。它在对象模型中提供了另一个间接级别。

Program that uses null DateTime struct: C#

使用 null DateTime 结构的程序:C#

using System;

class Program
{
    static void Main()
    {
    //
    // Declare a nullable DateTime instance and assign to null.
    // ... Change the DateTime and use the Test method (below).
    //
    DateTime? value = null;
    Test(value);
    value = DateTime.Now;
    Test(value);
    value = DateTime.Now.AddDays(1);
    Test(value);
    //
    // You can use the GetValueOrDefault method on nulls.
    //
    value = null;
    Console.WriteLine(value.GetValueOrDefault());
    }

    static void Test(DateTime? value)
    {
    //
    // This method uses the HasValue property.
    // ... If there is no value, the number zero is written.
    //
    if (value.HasValue)
    {
        Console.WriteLine(value.Value);
    }
    else
    {
        Console.WriteLine(0);
    }
    }
}

Output

输出

0
9/29/2009 9:56:21 AM
9/30/2009 9:56:21 AM
1/1/0001 12:00:00 AM

Originally found Here

最初发现在这里

Good luck!

祝你好运!

回答by Kjartan

You can't create a DateTimewithout any value (ie, null). It will always have a default value (DateTime.MinValue).

您不能创建DateTime没有任何值(即空值)的 。它将始终具有默认值 ( DateTime.MinValue)。

A DateTime?, on the other hand, is a sort of wrapper around DateTime, which allows you to keep it undefined. This can be very useful, for instance if you want to let the user leave one of the date-fields blank (no date selected = Null).

DateTime?,在另一方面,是一种包装的周围DateTime,它可以让你保持未定义。这可能非常有用,例如,如果您想让用户将日期字段之一留空(未选择日期 = Null)。

Remember that you'll need to use DateTime?as the parameter type of any methods that need to do work related to this, though. If you rely on other libraries, or have to pass this data to other components, etc, then you can sometimes face awkward situations.

请记住,您需要将其DateTime?用作需要执行与此相关的工作的任何方法的参数类型。如果您依赖其他库,或者必须将这些数据传递给其他组件等,那么您有时会遇到尴尬的情况。

Typical challenge/decision:
"I'm using DateTime?in component X, which talks to component Y, which uses DateTime- should I rewrite Yto handle DateTime?, or should I translate any DateTime?-object with a value of Nullinto a DateTime-object with a value of DateTime.MinValueto represent unselected/invalid dates? Or perhaps I should just let it throw an exception in Y...?"

典型的挑战/决定:
DateTime?在组件X 中使用,它与组件Y对话,后者使用DateTime- 我应该重写Y来处理DateTime?,还是应该将任何DateTime?值为 的-objectNull转换DateTime为值为DateTime.MinValueto的-object代表未选择/无效的日期?或者也许我应该让它在Y 中抛出异常......?

Just something to be aware of and think about when working with DateTime?..

只是在使用DateTime?..时需要注意和考虑的事情

回答by Charan Ghate

A nullable DateTime can be null. The DateTime struct itself does not provide a null option. But the "DateTime?" nullable type allows you to assign the null literal to the DateTime type. It provides another level of indirection.

可为空的 DateTime 可以为空。DateTime 结构本身不提供空选项。但是“日期时间?” nullable 类型允许您将空文字分配给 DateTime 类型。它提供了另一个层次的间接性。