main method

W

Wendy Elizabeth

I have the following questions to ask about the 'generated' code I have seen
so far:
1. I have seen static void Main() method in a file called program.cs. Does
this
instantiate an object or is this just 'class' library code? Please explain.
2. I have seen where the same solution can have several static void Main()
method
in a file called program.cs. Basically there several of this program.cs
files. Thus,
can you tell me why you would see so many and how can the code be setup to
know what program.cs to use?
 
P

Peter Duniho

Wendy said:
I have the following questions to ask about the 'generated' code I have seen
so far:
1. I have seen static void Main() method in a file called program.cs. Does
this
instantiate an object or is this just 'class' library code? Please explain.

The Main() method does exactly what the code you see in it suggests it
does, whatever that code may be.

In a console application, the IDE-provided Main() method is empty, and
so does nothing at all until you add code to it. In a Forms
application, the IDE-provided implementation initializes a couple of
Forms-related settings, and then calls Application.Run(), passing a new
instance of your initial form to it.

Even in a Forms application, you can modify the Main() method to suit
whatever specific need you have.

In any case, objects are instantiated only if and when the code in the
Main() method causes them to be instantiated. The Main() method itself
is required to be static, and so requires no object for the call itself
(though of course objects might be instantiated as part of the static
initialization of the class, just as with any other class).

I don't know what "just 'class' library code" is supposed to mean, so I
can't answer that part of your question.
2. I have seen where the same solution can have several static void Main()
method
in a file called program.cs. Basically there several of this program.cs
files. Thus,
can you tell me why you would see so many and how can the code be setup to
know what program.cs to use?

As long as each Main() method is in a different class, you can select
which one to use — that is, which class containing the Main() method to
use — by configuring that in the project's properties (e.g. right-click
on the project in the Solution Explorer, and then in the application
properties section, there's a place to select the class that is to be
the "startup object" for the application).

You may find more useful information here:
http://msdn.microsoft.com/en-us/library/acy3edy3(v=VS.100).aspx

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top