Is there a way to get VS2008 to stop warning me about unreachable code?

前端 未结 8 1960
我在风中等你
我在风中等你 2021-02-05 02:04

I have a few config options in my application along the lines of

const bool ExecuteThis=true;
const bool ExecuteThat=false;

and then code that

8条回答
  •  时光取名叫无心
    2021-02-05 02:14

    The quickest way to "Just get rid of it" without modifying your code would be to use

    #pragma warning disable 0162
    

    On your Namespace, class or method where you want to supress the warning.

    For example, this wont throw the warning anymore:

    #pragma warning disable 0162
    namespace ConsoleApplication4
    {
      public class Program
      {
        public const bool something = false;
    
        static void Main(string[] args)
        {
            if (something) { Console.WriteLine(" Not something" ); }
    
        } 
     }
    

    However be warn that NO METHOD inside this namespace will throw the warning again... and well.. warnings are there for a reason (what if it happened when you did NOT planned it to be unreachable?)

    I guess a safer way would be to write the variables in a configuration file, and read them from there at the beginning of the program, that way you don't even need to recompile to have your different versions/releases! Just change the app file and go :D.

    about the speed penalty.. yes.. making it this way would inquire in a speed penalty... compared to using const but unless you are really worried about wating 1/100 of a millisecond more.. I would go for it that way.

提交回复
热议问题