我希望我有以下方法
int num;
int.TryParse("5",out num, 10);
这将与 TryParse 执行相同的操作,但如果解析失败,则附加, out 参数将获得默认值 10
我能实现吗?
通过扩展方法,我可以实现以下功能:
int num;
num.TryParse("5",out num, 10);
但这看起来与其余的 TryParse 方法不同..
最佳答案
不能给已有的类添加静态方法,但是可以给自己的类添加自己的静态方法,例如:
public static class MyConversions
{
public static bool TryParse(string value, out int num, int defaultValue)
{
...
}
}
关于c# - 我可以扩展 int.TryParse 方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7498661/