简体   繁体   中英

Pre-build in Visual Studio 2010

Does there probable run a C# function at Pre-build event and replace the function call to constant value?

For example:

class A{
    A(){
        var aVar = B.Func1("a");
    }
}
class B{
    static String Func1(String str){
        //Do some things
        return str;
    }
}

After Pre-build event:

class A{
    A(){
        var aVar = "A";
    }
}
class B{
    public static String Func1(String str){
        //Do some things
        return str;
    }
}

This has nothing to do with "pre-build events". This is an optimization that may or may not be applied by the compiler during compilation.

When targeting the .NET Framework, you add an additional layer of optimization complexity. Many of the possible optimizations are not performed by the C# compiler when you initially compile the code to IL, but rather by the JIT compiler right before your code executes (when the IL gets compiled into native code).

And yes, the JIT compiler will most certainly apply such trivial optimizations as inlining a function call whenever possible. This is low-hanging fruit for any optimizer. (Although, there are conditions where inlining functions can make things slower , and the optimizer is generally smart enough to recognize this. You should always trust the optimizer, rather than trying to force it to do what you think makes sense. It's usually smarter than you.)

Your focus should be on writing code that is clear and easy to read/maintain. Don't worry about how to optimize it until after you've profiled it and determined that it is too slow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM