在C#中重载复合赋值运算符的简单方法? [英] Simple way to overload compound assignment operator in C#?

查看:100
本文介绍了在C#中重载复合赋值运算符的简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有一个非常简单的示例,说明如何在C#中重载复合赋值运算符?

Does anyone have a very simple example of how to overload the compound assignment operator in C#?

推荐答案

您不能显式重载复合赋值运算符.但是,您可以重载main运算符,编译器会对其进行扩展.

You can't explicitly overload the compound assignment operators. You can however overload the main operator and the compiler expands it.

x += 1纯粹是x = x + 1的语法糖,后者将被翻译为.如果重载+运算符,它将被调用.

x += 1 is purely syntactic sugar for x = x + 1 and the latter is what it will be translated to. If you overload the + operator it will be called.

MSDN操作员重载教程

public static Complex operator +(Complex c1, Complex c2) 
{
   return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}

这篇关于在C#中重载复合赋值运算符的简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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