为什么我们将运算符重载用作静态? [英] why we use operator overloading as static?

查看:232
本文介绍了为什么我们将运算符重载用作静态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们将public static关键字与运算符重载一起使用,为什么我们可能不使用这些关键字?

why we use public static keyword with operator overloading can it is possible that we not use these keywords?

推荐答案

这是C#的设计决策,请参见Eric Lippert的为什么重载的运算符在C#中始终是静态的?" 以获取一些见识.
It was a C# design decision, see Eric Lippert''s "Why are overloaded operators always static in C#?" for some insight.


请参阅说明 ^ ].


只是因为运算符重载没有引用类:它需要参数并返回值,但是它无法访问this对象或该类的任何非静态成员.
如果为逻辑非运算符``!''写一个重载,则它只需要一个参数-类实例即可对其执行操作.

这是一个静态方法,因此需要static关键字.
Simply because an operator overload does not reference a class: it takes parameters and returns a value, but it does not have access to the this object or any non static members of the class.
If you write a overload for the logical not operator ''!'' then it takes a single parameter - the class instance to perform the operation on.

It is a static method, so it needs the static keyword.


这篇关于为什么我们将运算符重载用作静态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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