<分区>
在扩展方法内部,有时使用其他扩展方法是有意义的。我拿不定主意你是否应该使用“this”语法。
public static class StringExtensions
{
// Foo vs Foo1
public static string Foo(this string s)
{
return s + "Foo" + s.Bar(); // "this" syntax
}
public static string Foo1(this string s)
{
return s + "Foo" + Bar(s);
}
public static string Bar(this string s)
{
return s + "Bar";
}
}
Foo
与 Foo1
。出现两个问题:
- 表现。生成的
IL
代码有什么不同吗? - 代码设计。这两种方法中哪一种更可取?为什么?