By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,070 Members | 1,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,070 IT Pros & Developers. It's quick & easy.

How does a class function without creating it.

P: n/a
Hi Newbee here to C# I have a simple questions...

In a Hello world example how does the class object Hello exist with out
creating it? I come from object pascal where everything object is created
then used. In object pascal you can call a method a "class" and it means you
can call it by the name of the class and the function name with out creating
it like "Hello.Main()" is that true for key word "static"?

Also what if I have more than one class that has "Main"?
Can I tell the system what "Main" to use (not via comand line)?
class Hello
{
static void Main( )
{
// Use the system console object
System.Console.WriteLine("Hello World");
}
}

Davinci
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Davinci,

Yes, static is the same thing in this scenario.

As for the name of the Main method to use, the only option there is to
use the command line compiler, using the /main flag to indicate the type
that has the Main method on it to use as the entry point.

I believe that the option to set this in the IDE exists in VS.NET 2005
though.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Davinci_Jeremie" <Da*************@discussions.microsoft.com> wrote in
message news:4B**********************************@microsof t.com...
Hi Newbee here to C# I have a simple questions...

In a Hello world example how does the class object Hello exist with out
creating it? I come from object pascal where everything object is created
then used. In object pascal you can call a method a "class" and it means
you
can call it by the name of the class and the function name with out
creating
it like "Hello.Main()" is that true for key word "static"?

Also what if I have more than one class that has "Main"?
Can I tell the system what "Main" to use (not via comand line)?
class Hello
{
static void Main( )
{
// Use the system console object
System.Console.WriteLine("Hello World");
}
}

Davinci

Nov 16 '05 #2

P: n/a
Davinci_Jeremie <Da*************@discussions.microsoft.com> wrote:
Hi Newbee here to C# I have a simple questions...

In a Hello world example how does the class object Hello exist with out
creating it? I come from object pascal where everything object is created
then used. In object pascal you can call a method a "class" and it means you
can call it by the name of the class and the function name with out creating
it like "Hello.Main()" is that true for key word "static"?
"static" means "in the context of the type rather than any one specific
instance of the type". You can use static methods/properties/fields
without ever creating an instance.
Also what if I have more than one class that has "Main"?
Can I tell the system what "Main" to use (not via comand line)?


Yes - there's a property (Startup Object or something similar, IIRC) in
one of the build settings for the project.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

P: n/a
I'm not up on my Pascal, but I believe that the OP is confusing the
"class" keyword with the class itself.

You don't call a method a "class". You call it a "class method",
meaning that it is associated with the class as a whole rather than
with every instance of that class individually. The keyword "static" in
C# has the same meaning.

Nov 16 '05 #4

P: n/a
Davinci_Jeremie wrote:
Hi Newbee here to C# I have a simple questions...

In a Hello world example how does the class object Hello exist with out
creating it? I come from object pascal where everything object is created
then used. In object pascal you can call a method a "class" and it means you
can call it by the name of the class and the function name with out creating
it like "Hello.Main()" is that true for key word "static"?

Static methods work as you suspect. Main is a static method, which means
that the class defining it does not have to be instantiated before
calling the method. Main and it's overload gets special treating by the
compiler, which marks it as .entrypoint.
Also what if I have more than one class that has "Main"?
Can I tell the system what "Main" to use (not via comand line)?


You have to use the command line argument /main:<class-name> to specify
to csc.ece which of the classes Main method to call at application
startup. You can also specify the 'Startup Object' attribute on the
property page for your property in Visual Studio.

Regards,
Joakim
Nov 16 '05 #5

P: n/a
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
Yes, static is the same thing in this scenario.

As for the name of the Main method to use, the only option there is to
use the command line compiler, using the /main flag to indicate the type
that has the Main method on it to use as the entry point.


It's in VS.NET 2003, too - at least in C#.

The option is in project properties, General | Startup Object.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

P: n/a
Thank you that answered my questions.

Thanks to everyone that responed to my post.

Davinci
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.