有没有理由第二个操作数的类型必须是int?
...
// I would like to do this
public static StringList operator<<(StringList list, string s) {
list.Add(s);
return list;
}
// but only int is supported...
...
Run Code Online (Sandbox Code Playgroud)
编辑:只是肯定...我可以重载operator*for get(例如)字符串列表
class MyString {
string val;
public MyString(string s) {
val = s;
}
public static List<string> operator*(MyString s, int count) {
List<string> list = new List<string>();
while (count-- > 0) {
list.Add(s.val);
}
return list;
}
}
...
foreach (var s in new MyString("value") * 3) {
s.print(); // object extension (Console.WriteLine)
}
// output:
// value
// value
// value
...
Run Code Online (Sandbox Code Playgroud)
但是不能超载左移,从C++ std(输出重载)众所周知,因为它不清楚?当然,这只是C#设计师的决定.仍然可以在意外/不清楚的事情上重载(使用int).
真的原因是它的代码不清楚了吗?
| 归档时间: |
|
| 查看次数: |
1441 次 |
| 最近记录: |