我想在我的C#类中重载除法运算符.所以,我写道:
public string[] operator/ (object obj) {
}
Run Code Online (Sandbox Code Playgroud)
并得到错误:"解析器错误:超载一元运算符除外".
那么,我不能超载那个运算符?
在MSDN上,我没有看到任何示例:http://msdn.microsoft.com/en-us/library/3b1ff23f.aspx
谢谢.
//如果需要,我在Ubuntu 14.10上使用MonoDevelop.
您可以重载除法运算符,但是:
例如:
using System;
class Program
{
public static string operator/ (Program lhs, int rhs)
{
return "I'm divided!";
}
static void Main(string[] args)
{
Console.WriteLine(new Program() / 10);
}
}
Run Code Online (Sandbox Code Playgroud)