The answer to your question is yes and no, depending on what you mean by a “built-in function”. If you use the term as it is used in C ++ development, then the answer will be negative, you cannot do this - even a lambda expression is a function call. Although it is true that you can define inline lambda expressions to replace function declarations in C #, the compiler still finishes creating an anonymous function.
Here is some really simple code I used to test this (VS2015):
static void Main(string[] args) { Func<int, int> incr = a => a + 1; Console.WriteLine($"P1 = {incr(5)}"); }
What does the compiler generate? I used a great tool called ILSpy that displays the generated IL assembly. Take a look (I have omitted a lot of things to set up the class)
This is the main function:
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
See lines IL_0026 and IL_0027? These two instructions load number 5 and call the function. Then enter IL_0031 and IL_0036 and print the result.
And here is a function called:
.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'
This is a really short function, but it is a function.
Is it really worth the effort to optimize? Nope. Maybe if you call it thousands of times per second, but if performance is important, you should consider invoking your own C / C ++ code to do the job.
In my experience, readability and maintainability are almost always more important than optimizing for a few microseconds in speed. Use functions to make your code readable and control the scope of variables and not worry about performance.
"Premature optimization is the root of all evil (or at least most) programming." - Donald Knut
"A program that does not work correctly does not require a quick start" - Me