加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

为什么赋值运算符在VB.NET中不可重载?

发布时间:2020-12-17 00:31:02 所属栏目:大数据 来源:网络整理
导读:为什么VB.NET中的赋值运算符(=,– =,* =,/ =)不可重载? 也许 this是他们的推理: Thanks for the suggestion! We don’t allow you to overload the assignment operator for a type because there is currently no way to ensure that other languages or
为什么VB.NET中的赋值运算符(=,– =,* =,/ =)不可重载?
也许 this是他们的推理:

Thanks for the suggestion! We don’t allow you to overload the
assignment operator for a type because there is currently no way to
ensure that other languages or the .NET Framework itself will honor
the assignment operator. The only alternative is to restrict what
types that overload the assignment operator can do,but we felt that
this would be too restrictive to be generally useful.

Thanks! Paul Vick Technical Lead,VB

有一种叫做“缩小”和“加宽”的东西,它允许你定义从一种类型到另一种类型的显式和隐式转换器,即

Dim y as MyClass1
Dim x as MyClass2 = y

但是,这不会改变赋值运算符以分配同一个类的实例,只转换其他类.

见How to: Define a Conversion Operator

Class MyClass1
    Public Shared Widening Operator CType(ByVal p1 As MyClass1) As MyClass2

    End Operator
End Class

Same in C#

+=,-=,*=,/=,%=,&=,|=,^=,<<=,>>=

Assignment operators cannot be overloaded,but +=,for example,is evaluated using +,which can be overloaded.

=,.,?:,??,– >,=>,f(x),as,checked,unchecked,default,delegate,is,new,sizeof,typeof

These operators cannot be overloaded.

与conversion operators相同:

struct MyType1
{
    ...
    public static explicit operator MyType1(MyType2 src)  //explicit conversion operator
    {
        return new MyType1 { guts = src.guts };
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读