Reputation: 840
The first example creates an instance of the program class. The second one doesn't. Can anybody tell me what is going on with these two simple models? (you can get down to stack frames and minutae if u wish) Why use either? I would like to understand advantages and applications of these structures.
//Example #1
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Program createStory = new Program();
createStory.PrintMe();
}
private void PrintMe()
{
Console.Write("Hello World));
}
}
}
//Example #2
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Hello World));
}
}
}
Upvotes: 2
Views: 100
Reputation: 1502166
You've said exactly what happens - one creates an instance and then calls a method, the other just does its work directly.
Typically the first form lends itself better to testing - you can create a separate instance of the app in each test, pass in the relevant parameters etc. That's the same with other classes too - if you have any state, it's easier to test separate instances in isolation than to use global state and have to clean it up between tests.
Upvotes: 2