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
+=,-=,*=,/=,%=,&=,|=,^=,<<=,>>=
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.
struct MyType1 { ... public static explicit operator MyType1(MyType2 src) //explicit conversion operator { return new MyType1 { guts = src.guts }; } }