我正在使用Linq To XML
new XElement("Prefix", Prefix == null ? "" : Prefix)
但我想在将其添加到XML之前对前缀进行一些计算,例如消除空格、特殊字符、某些计算等。
我不想创建函数,因为这些函数对我的程序的其他部分没有帮助,但是有没有创建内联函数的方法?
Anonymous methods were added in C# 2.0:
Func<int, int, int> add = delegate(int x, int y)
{
return x + y;
};
Action<int> print = delegate(int x)
{
Console.WriteLine(x);
}
Action<int> helloWorld = delegate // parameters can be elided if ignored
{
Console.WriteLine("Hello world!");
}
Lambdas are new in C# 3.0 and come in two flavours.
Expression lambdas:
Func<int, int, int> add = (int x, int y) => x + y; // or...
Func<int, int, int> add = (x, y) => x + y; // types are inferred by the compiler
Statement lambdas:
Action<int> print = (int x) => { Console.WriteLine(x); };
Action<int> print = x => { Console.WriteLine(x); }; // inferred types
Func<int, int, int> add = (x, y) => { return x + y; };
Local functions have been introduced with C# 7.0:
int add(int x, int y) => x + y;
void print(int x) { Console.WriteLine(x); }
Func
和 Action
。 Func
返回值,但 Action
不返回。 Func
的最后一个类型参数是返回类型; 其他所有类型都是参数类型。Comparison<T>
就大致相当于 Func<T, T, int>
。Func<string, string, int> compare1 = (l,r) => 1;
Comparison<string> compare2 = (l, r) => 1;
Comparison<string> compare3 = compare1; // this one only works from C# 4.0 onwards
这些可以像常规方法一样直接调用:
int x = add(23, 17); // x == 40
print(x); // outputs 40
helloWorld(x); // helloWorld has one int parameter declared: Action<int>
// even though it does not make any use of it.
C# 7增加了对本地函数的支持:local functions
这是使用本地函数的先前示例:
void Method()
{
string localFunction(string source)
{
// add your functionality here
return source ;
};
// call the inline function
localFunction("prefix");
}
string localFunction(string source) => source;
阅读章节 关于明确赋值和变量捕获,这应该能够回答你的问题。 - DalSoft static void Main(string[] args)
{
Func<int, int> incr = a => a + 1;
Console.WriteLine($"P1 = {incr(5)}");
}
编译器生成什么?我使用了一个很棒的工具叫ILSpy,它可以展示实际生成的IL程序集。看一下(我省略了很多类设置的内容)
这是主函数:
IL_001f: stloc.0
IL_0020: ldstr "P1 = {0}"
IL_0025: ldloc.0
IL_0026: ldc.i4.5
IL_0027: callvirt instance !1 class [mscorlib]System.Func`2<int32, int32>::Invoke(!0)
IL_002c: box [mscorlib]System.Int32
IL_0031: call string [mscorlib]System.String::Format(string, object)
IL_0036: call void [mscorlib]System.Console::WriteLine(string)
IL_003b: ret
看到IL_0026和IL_0027这两行吗? 这两条指令加载数字5并调用一个函数。 然后,IL_0031和IL_0036格式化并打印结果。
这里是被调用的函数:
.method assembly hidebysig
instance int32 '<Main>b__0_0' (
int32 a
) cil managed
{
// Method begins at RVA 0x20ac
// Code size 4 (0x4)
.maxstack 8
IL_0000: ldarg.1
IL_0001: ldc.i4.1
IL_0002: add
IL_0003: ret
} // end of method '<>c'::'<Main>b__0_0'
这是一个非常简短的函数,但它确实是一个函数。
这个函数值得优化吗?不必要。除非你每秒钟调用它数千次,但如果性能如此重要,那么你应该考虑调用用C/C++编写的本地代码来完成工作。
在我的经验中,可读性和可维护性几乎总是比为了争取几微秒的速度而进行的优化更加重要。使用函数使你的代码易读,并控制变量作用域,不要担心性能问题。
“过早的优化是所有恶之源(或者至少是大部分),在编程中。” -- 唐纳德·克努斯
“一个不能正确运行的程序不需要快速运行” -- 我
code
Func<string, string> PrefixTrimmer = x => x ?? "";code
。我是新手,对 MSDN 文档并不是很理解。 - Joe Dixonx ?? ""
是空值合并运算符。http://msdn.microsoft.com/zh-cn/library/ms173224.aspx - SLaksx => (something)
中,x
是一个参数,(something)
是返回值。 - SLaksclass Calculator
{
public static int Sum(int x,int y) => x + y;
public static Func<int, int, int> Add = (x, y) => x + y;
public static Action<int,int> DisplaySum = (x, y) => Console.WriteLine(x + y);
}
void Method()
{
Func<string,string> inlineFunction = source =>
{
// add your functionality here
return source ;
};
// call the inline function
inlineFunction("prefix");
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- BrainSlugs83