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

前端之家收集整理的这篇文章主要介绍了为什么赋值运算符在VB.NET中不可重载?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
为什么VB.NET中的赋值运算符(=,– =,* =,/ =)不可重载?
也许 @L_404_0@是他们的推理:

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 };
    }
}
原文链接:https://www.f2er.com/vb/255095.html

猜你在找的VB相关文章