为什么赋值运算符不能在VB.NET中重载? [英] Why aren't assignment operators overloadable in VB.NET?

查看:88
本文介绍了为什么赋值运算符不能在VB.NET中重载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么赋值运算符(+ =,-=,* =,/=)在VB.NET中不能重载?

Why aren't the assignment operators (+=, -=, *=, /=) overloadable in VB.NET?

推荐答案

也许

谢谢你的建议!我们不允许您超载 类型的赋值运算符,因为当前没有办法 确保使用其他语言或.NET Framework本身 赋值运算符.唯一的选择是限制什么 使赋值运算符可以重载的类型,但是我们觉得 这太过严格而无法普遍使用.

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.

谢谢! Paul Vick VB技术主管

Thanks! Paul Vick Technical Lead, VB

有一种叫做变窄"和"Widening"的东西,它允许您定义从一种类型到另一种类型(即

There's something called 'Narrowing' and 'Widening' which allows you to define explicit and implicit converters from one type to another, i.e.

Dim y as MyClass1
Dim x as MyClass2 = y

但是,这不允许更改用于分配相同类的实例的赋值运算符,而只能转换其他类.

But that doesn't let change the assignment operator for assigning an instance of the same class, only converting other classes.

请参见如何:定义转换操作符

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

    End Operator
End Class


与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.

使用相同的转换运算符:

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

这篇关于为什么赋值运算符不能在VB.NET中重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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