+ 1

understanding the output on a question.

Why is the output "hisolo"? static void Main(string[] args) { new Super(); Console.WriteLine(); } class Super { static Super() { Console.Write("hi"); } public Super() { Console.Write("solo"); } }

23rd Jun 2019, 8:58 PM
laura
6 Réponses
+ 4
In practice I do not see static constructors being used often. It is in the books because it can be used. In this article below they show. That all busses start with the same time. To initialize the time they use a static constructor. This is efficient. Now all object use the same start time and this time is set only once at the beginning of the program. Every object can make use of this variable which is part of the class not the object it self. https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-constructors Static -> part of class non static -> bound to object Start by using non static constructor, later. When you do more advanced programming you will know when you want to use a static constructor.
24th Jun 2019, 7:56 PM
sneeze
sneeze - avatar
+ 3
sneeze ~ swim ~ thanks so much! :)
25th Jun 2019, 12:45 AM
laura
+ 1
because the code writes hi and then writes solo
23rd Jun 2019, 10:42 PM
slick
slick - avatar
0
slick ty for your response. I am trying to better understand why... Why not ignore one or the other? I'm looking at this trying to better understand static vs nonstatic... Is there a scenario.. with modification. . one line would print out and not the other
24th Jun 2019, 2:24 AM
laura