就像是
public static class StringHelpers
{
public static char first(this string p1)
{
return p1[0];
}
public static implicit operator Int32(this string s) //this doesn't work
{
return Int32.Parse(s);
}
}
Run Code Online (Sandbox Code Playgroud)
所以:
string str = "123";
char oneLetter = str.first(); //oneLetter = '1'
int answer = str; // Cannot implicitly convert ...
Run Code Online (Sandbox Code Playgroud)
不,没有扩展操作符(或属性等) - 只有扩展方法.
C#团队已经考虑过了 - 可以做各种有趣的事情(想象扩展构造函数) - 但它不是在C#3.0或4.0中.有关更多信息,请参阅Eric Lippert的博客(一如既往).
| 归档时间: |
|
| 查看次数: |
1350 次 |
| 最近记录: |