.NET中模棱两可的隐式用户定义转换 [英] Ambiguous implicit user defined conversions in .NET

查看:66
本文介绍了.NET中模棱两可的隐式用户定义转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在.NET中编写了一些结构,然后向其中添加了隐式转换运算符

I recently wrote a couple of structs in .NET, to which I then added implicit conversion operators

示例:

public struct Alpha
{
    internal string value;

    public static implicit operator Alpha(Beta b)
    {
        return new Alpha() { value = b.value };
    }

    public static implicit operator Beta(Alpha a)
    {
        return new Beta() { value = a.value };
    }
}

public struct Beta
{
    internal string value;

    public static implicit operator Alpha(Beta b)
    {
        return new Alpha() { value = b.value };
    }

    public static implicit operator Beta(Alpha a)
    {
        return new Beta() { value = a.value };
    }
}

测试:

Alpha a = default(Alpha);
Beta b = a;
// Ambiguous user defined conversions 'Alpha.implicit operator Beta(Alpha)' and 'Beta.implicit operator Beta(Alpha)' when converting from 'Alpha' to 'Beta'

我想知道C#中隐式转换的规则/最佳做法是什么?

I would like to know what the rules / best practices are surrounding implicit conversion in C#?

自我注释:我的直觉是类型不应该通过隐式转换返回其他类型的对象吗?即 Beta 不应通过隐式转换返回 Alpha ,反之亦然,但是, Beta 应该返回 Beta ,和 Alpha 应该返回 Alpha

Note to self: My gut feeling is that types should not return objects of another type through implicit conversion? i.e. Beta should not return Alpha via implicit conversion, and vice-versa, however, Beta should return Beta, and Alpha should return Alpha

示例(固定):

public struct Alpha
{
    internal string value;

    public static implicit operator Alpha(Beta b)
    {
        return new Alpha() { value = b.value };
    }
}

public struct Beta
{
    internal string value;

    public static implicit operator Beta(Alpha a)
    {
        return new Beta() { value = a.value };
    }
}

我的假设正确吗?

推荐答案

在两个类中都有一个 Alpha(Beta x)定义,因此使用哪个是不明确的.允许每个类仅处理自身的转换.换句话说, struct Alpha 实现 Alpha(Beta b),因为它最了解如何创建自己的实例.另外,我会考虑实现显式转换而不是隐式转换.有时可能会意外导致错误或意外的转换,并且在处理复杂的类时通常是有损的"转换.

You have a Alpha(Beta x) defined in both classes, thus it is ambiguous which one should be used. Allow each class to handle only the conversion to itself. In other words, struct Alpha implements Alpha(Beta b) because it best knows how to create an instance of itself. Also, I would consider implementing explicit conversions instead of implicit. It can sometimes cause bugs or unexpected conversions accidentally, and when dealing with complex classes are often "lossy" conversions.

public struct Alpha
{
    internal string value;

    public static implicit operator Alpha(Beta b)
    {
        return new Alpha() { value = b.value };
    }
}

public struct Beta
{
    internal string value;

    public static implicit operator Beta(Alpha a)
    {
        return new Beta() { value = a.value };
    }
}

您可能唯一一次在一个类中实现双向"的情况是,另一个类不了解您的新类.支持从现有的类/结构转换为,例如支持从框架类型转换为:

The only time you'd likely implement "both ways" in a single class, is if the other class has no knowledge of your new class. This is the more common scenario when you want to support conversion to-from a pre-existing class/struct such as supporting conversion to-from a framework type:

public struct Alpha
{
    internal string value;

    public static implicit operator Alpha(string b)
    {
        return new Alpha() { value = b };
    }

    public static implicit operator string(Alpha a)
    {
        return  a.value;
    }
}

这篇关于.NET中模棱两可的隐式用户定义转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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