gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 05:37:55 24 4
gpt4 key购买 nike

为什么赋值运算符(+=、-=、*=、/=)在 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 };
}
}

关于vb.net - 为什么赋值运算符在 VB.NET 中不可重载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10282565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com