How does a class function without creating it.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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.
 
Davinci_Jeremie said:
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.
 
Davinci_Jeremie said:
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
 
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.
 
Nicholas Paldino said:
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.
 
Thank you that answered my questions.

Thanks to everyone that responed to my post.

Davinci
 
Back
Top