为什么Nullable< DateTime>可以分配给只能从DateTime隐式转换的参数? [英] Why Nullable<​DateTime> can be assigned to a paramter which only can be implict converted from DateTime?

查看:113
本文介绍了为什么Nullable< DateTime>可以分配给只能从DateTime隐式转换的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有从Nullable 到DynamoDB条目的隐式转换。
但是我有这样的代码。

There is no implicit conversion from Nullable<​DateTime> to DynamoDB​Entry. But I have code like this. It works well.

class DocumentData {
    private readonly Document doc;

    protected void SetValue(string key, DateTime? dateTime)
    {
        DateTime? old = GetDateTime(key);
        if (old != dateTime)
            doc[key] = dateTime;
    }
}






实际上,我测试了其他一些代码。我认为这与DynamoDB无关。


In fact, I tested some other code. I think it's nothing to do with DynamoDB.

class TestDateTIme
{           
    public static void Test() { 
        DateTime? a = DateTime.UtcNow;
        Convert(a);
    }
    public static void Convert(MyClass m){
        return;
    }
}

class MyClass 
{
    public static implicit operator MyClass(DateTime date)
    {
         return new MyClass ();
    }
}


推荐答案

好题。 dexcribe是什么:

Good question. What you dexcribe is this:

class MyClass 
{
    public static implicit operator MyClass(DateTime date)
    {
         return new MyClass ();
    }
}

这是由非可为空的值类型(此处为 DateTime )为类类型的引用类型。

That's an implicit user-defined conversion from a non-nullable value type (here DateTime) to the class type, a reference type.

然后进行转换 DateTime MyClass 引发转换 DateTime? MyClass

Then a conversion DateTimeMyClass "induces" a conversion DateTime?MyClass as it seems.

在上面的示例中,编译如下:

With the above example, this compiles:

DateTime? nullableDateTime = XXX;
MyClass myClass = nullableDateTime;  // implicit conversion from nullable!

我试图仔细阅读C#语言规范的以下部分:

I tried to read carefully the part of the C# Language Specification beginning with:


用户定义的隐式转换

键入 S 键入 T 的过程如下:
[...]

A user-defined implicit conversion from type S to type T is processed as follows: [...]

此处源 S DateTime吗?,而目标 T MyClass 。有了规范标记, S0 Sx 成为 DateTime ,并且您编写的转换是选定的。

Here the source S is DateTime?, and the target T is MyClass. With the notation of the spec, S0 and Sx become DateTime and the conversion you wrote is "selected".

nullableDateTime 具有值时,很明显将该值解开,然后输入到您的用户定义的转换中。 似乎符合规范。

When nullableDateTime has a value, it is pretty clear that this value is unwrapped, then fed to the user-definede conversion of yours. It seems to be in agreement with the spec.

nullableDateTime 没有一个值(为null),看起来结果 myClass 变成该类型的 null ,即<类类型的code> null 引用。这是基于实验的。 我不确定在规范的何处描述此行为(从结构到类的 null )。

When nullableDateTime does not have a value (is null), it looks like the resulting myClass becomes a null of that type, that is a null reference of the class type. This is based on experimenting. I am not sure where in the spec this behavior, with null from struct to class, is described.

结论:您所询问的行为可能是规范编写方式的结果,但是我不确定在哪里说 null 应该是转到 null 而不实际调用转换方法。

Conclusion: The behavior you asked about, is probably a consequence of the way the specification is written, but I am not sure where it says that null shall go to null without actually invoking your conversion method.

这篇关于为什么Nullable&lt; DateTime&gt;可以分配给只能从DateTime隐式转换的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆