喂我想为Char
类,它作为Char.IsDigit()
方法(当然这会认识到一个不同的充类型的字符)创建一个扩展方法。 我写了这个:C#创建扩展方法
namespace CharExtensions
{
public static class CompilerCharExtension
{
public static Boolean IsAddOp(this Char c)
{
return c.Equals('+') || c.Equals('-');
}
}
}
工作正常,但它的不正是我的意思。 这个扩展应该这样使用:
using CharExtensions;
char x:
...
if(x.IsAddOp())
Console.WriteLine("Add op found");
虽然我是这样的:
using CharExtensions;
char x;
...
if(Char.IsAddOp(x))
Console.WriteLine("Add op found");
谢谢大家谁可以帮助我。
你不能这样做,如果你能做到这将是非常糟糕的做法。你为什么想这么做?只需制作一个普通的方法并像这样调用它:'IsAddOp(x)'。 – DavidG
@DavidG _Just make一个正常的方法_是好的,但如果可以定义扩展方法,更好地使用它的开发人员 –
@combo_ci以什么方式扩展到'char'更好? – DavidG