3

This feels like a dumb question but I couldn't find anything similar. Is there any reason not to make your main entry point class static? I only ask really because ReSharper (naively... Class 'Program' is never instantiated) warns about it.

I'm wondering if there is an objective better or worse way to define the class, and if so, why.

    public /*static any harm including this?*/ class Program
    {
        public static void Main(string[] args) => CreateHostBuilder(args).Build().Run();
    }
xandermonkey
  • 4,054
  • 2
  • 31
  • 53
  • 3
    Why the downvote and close vote? This isn't subjective - I'm genuinely asking if there are any pro / cons to doing this. – xandermonkey Aug 20 '20 at 16:02
  • its an open ended question. – Daniel A. White Aug 20 '20 at 16:02
  • @DanielA.White how is this open ended? I'm sure there are objective pro and cons to doing or not doing this. Sure, there's no specific problem where the code runs one way or doesn't run another way, but that doesn't mean it's open ended or opinion based. – xandermonkey Aug 20 '20 at 16:03
  • 2
    There are *plenty* of questions on here wherein both ways work, but one is considered better. That's what I'm asking, and why. – xandermonkey Aug 20 '20 at 16:04
  • it reads as to what _you want_ to do. hence its open ended/opinion based. – Daniel A. White Aug 20 '20 at 16:05
  • 2
    Okay, if you think the verbiage reads a certain way the right solution is editing the question, not voting to close it. – xandermonkey Aug 20 '20 at 16:06
  • @DanielA.White but then it should be stated as an answer but i think what hes asking is more why is there a warning... `ReSharper (naively) warns about it.` @OP could you include the warning reshaper is giving and is that something you can google? – Seabizkit Aug 20 '20 at 16:07
  • @DanielA.White I've taken the liberty to edit the question, hopefully it appears less about what I want and more about what is technically better. If it's not to your liking feel free to edit. – xandermonkey Aug 20 '20 at 16:08
  • @Seabizkit sure, good suggestion. I've updated the question. – xandermonkey Aug 20 '20 at 16:08
  • 2
    Very closely related (possibly a dup): https://stackoverflow.com/q/58513022/8967612 – 41686d6564 stands w. Palestine Aug 20 '20 at 16:09
  • there isn't any advantage - resharper only sees static members and its not constructed. – Daniel A. White Aug 20 '20 at 16:09
  • 2
    Its defos subjective - because it depends how your code uses you Program class. If its JUST the entry point to your app, and has no other members/functionality then obviously it makes no difference – Milney Aug 20 '20 at 16:09
  • @41686d6564 good find, that's definitely the answer I was looking for. – xandermonkey Aug 20 '20 at 16:10
  • Also https://stackoverflow.com/questions/2366952/why-is-the-main-method-entry-point-in-most-c-sharp-programs-static – StriplingWarrior Aug 20 '20 at 16:10
  • Yes. If you are calling from another application more than once static will reuse properties while removing static each instance would have its own set of properties. – jdweng Aug 20 '20 at 16:11
  • The class doesn't have to be static. If the class contains the entry point to your application, the entry method has to be static. "chicken and egg"... you can't instantiate an object before you're inside the program. – CodingYoshi Aug 20 '20 at 16:35
  • Modern versions of visual studio will probably say )if you turn on Info messages) "program is never instantiated and can be declared static" or something similar about static holder types – Caius Jard Aug 20 '20 at 16:40

0 Answers0