I'm a bit new at this so bear with me. I'm currently learning C# and Java and one of their similarities is that the main function needs to be encapsulated within a class. For example
public class HelloWorld {
public static void main(String[] args) {
// Some Code
}
}
Now I understand that main is often the "entry point" when you run the program. So basically, your program will start executing wherever the main function is. But I believe in both languages you can have multiple main functions within multiple classes. So when I compile a project with multiple main functions, where is the "entry point"? How does the compiler know where to start?
In Java, the computer determines the "entry point" when you actually execute the program, not when you compile. For example, from the command-line
java MyClass
searches for main() in MyClass. All other main() functions are ignored.
If you are using an IDE, then you can set which class contains the main() function that you want to use.
In .NET, you can define which class contains the Main method you want to use when you're compiling.
http://msdn.microsoft.com/en-us/library/x3eht538.aspx
In Java, if you're bundling to a jar, you can define your entry point in the jar's manifest.
http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
In C#, you can use multiple Main methods.
If there are multiple Main methods, the compiler doesn't know which entry point to use and hence it will show you an error.
You need to specify the Main method to be used at compilation:
You can specify which method to be used as a compiler option in the Visual Studio development environment or through the csc compiler.
The main class is a special class for only one reason: when you run the Java Virtual Machine, that function is what the JVM calls. It is essentially like any other function, and in fact you can call one class's main function from another class.
When you compile a project with multiple classes, you tell the JVM to run the class with the main class you want to use, like so:
java SomeClass
and it will run the main method of SomeClass, assuming that SomeClass is compiled and that the appropriate compiled file is in your classpath. (That is something you'll have to work out with your particular OS, but I think it's fairly usual for the -cp option to specify a classpath). So this:
java -cp /home/MyName Someclass
will run the main function of SomeClass in the directory /home/MyName
In C#, you specify the entry point using the /main: compiler option.
Consider the following code containing two main() functions:
namespace Application {
class ClassOne {
static void main () {
// Code here
}
}
class ClassTwo {
static void main () {
// Code here
}
}
}
To use ClassOne.main() as your entry point, you would specify the following when compiling:
csc /main:Application.ClassOne hello.cs
For multiple main functions entry point can be declared by :
To set this compiler option in the Visual Studio development environment
Open the project's Properties page.
Click the Application property page.
Modify the Startup object property.
reference : http://msdn.microsoft.com/en-us/library/x3eht538.aspx
In Java, as others pointed out, you define the class containing your main function when you run the java command.
But you could also build an executable jar, which can be run with java -jar my.jar. In this case, you need a manifest file called MANIFEST.MF in the folder META-INF in the jar. In this file, you specify the class containing the main function using the statement: Main-Class: YourClass.
The main method is static, which means it belongs to the class rather than the object. So the object won't have another main method inside it at all. It won't have an additional main-method, as main is static. So it's once per class.
If you have multiple main-methods in your project, you will specify which one to launch when starting your application
In fact, in binary file, for example, PE format in windows and ELF format in Linux or any other system, The header of binary file will specify where is the start address and there can be only one.
Which one should be the entry point? It depends on the linker. Just like #SetFreeByTruth said that you can specify it with command line parameters.
Many linkers support specifying entry point with command line parameters. for example, gnu's gld can specify entry point with parameter -e.
I don't know the behavior of Java because it is loaded by Java virtual machine.
In Visual Studio, you select the project that you want to be the entry point, right click and Set as StartUp Project.
Related
I have a couple of functions which I would like to add to DLL. I found this programming guide How to: Create and Use C# DLLs (C# Programming Guide). It's very short and looks like a really simple thing to do but I've noticed that each single function added to dll is enlosed in a separate file, separate class and functions are static. Is it always the case? What if I have a couple of overloaded functions, such as:
public void WaitForTheKey(object o = null) {}
public void WaitForTheKey(string message, bool addlines = true, int[] quantity = null) {}
private void _WaitForTheKey(string, bool, int[]) {}
Shall I put them in separate files like in the tutorial? Thanks.
EDIT.
If projects for DLL do not require separate classes and files, what would be the reason an author of the tutorial followed this theme?
Thanks
First of all you should keep you logic split by functionality. So all these methods you should keep in one class (in most cases it means one file).
It is very simple to create dll. If you use Visual Studio you should pick Class Library project type and then simply build it. As a result you will get dll file. Or use compiler directly from command prompt like it was shown in tutorial.
You are overthinking it; the tutorial is just an arbitrary example.
Simply structure the code in a way that makes sense, with as many or few classes as you like. Everything that is public in your assembly (classes, methods, properties, fields, events etc.) can be accessed by the consumer of the DLL.
I know that for Console/Windows application in C#, "Main" method is entry point to run the application.
If we have hundreds of classes in our application, how the runtime will detect which class contains the "Main" method to run the application?
The compiler looks for
static void Main(string[])
or
static int Main(string[])
to determine the entry point. Main() may also be declared without the string[] argument. You only need to specifically set the project setting if you have multiple classes with Main() functions.
Here's MSDN's detailed answer for you.
Entry Point can be configured in Project Settings.
I have numerous projects that all contain the exact same boilerplate code inside of the main function. They all reference the same assembly (which is the one providing a majority of the functionality). This assembly is not referenced by anybody except for the ones who use this same boilerplate main code.
Is it possible to have a Main function inside of a class within the assembly and use it within the program referencing the assembly instead of a class within the current assembly? I would prefer to not even write a Main function within the programs if I can.
EDIT: I want the Main inside of the referenced assembly to BE the entry point.
No, you cannot skip the static Main function in your executable assemblies because that's the entry point but you could perfectly fine define a method in this assembly with the same signature that will be invoked from the Main methods of all projects that need this boilerplate code.
For example:
static void Main(string[] args)
{
SomeClass.SomeMethodToDoBoilerplate(args);
SomeSpecificMethods();
}
With python, I can use if __name__ == "__main__": for using the module both as a library and a program.
Can I mimic this feature in C#?
I see a class in C# can have a 'static void Main()', but I'm not sure if every class can have a Main() without a problem.
ADDED
/m:CLASS_NAME is a way to specify the class to run the Main().
You can put a Main method in as many classes as you like, although only one can be an entry point for an application. (For talks, I often have a main method in every class, and use a helper library to present all of those pseudo-entry-points when I run the project.)
Likewise you can definitely add a reference to a .exe assembly and treat it like a library. For example, you could make a unit testing assembly work like a class library in most cases, but also write a main method so that you could just run it to execute the tests without a GUI or whatever.
You can compile a C# project as a program (executable) with a Main() method, and you'd still be able to use it as a library. No special syntax required.
You could add a Main() method to every class, but I doubt it's useful.
.NET applications usually have different structures than Python ones; trying to fit the same programming model is unlikely to get you good results.
C# project files specify a startup object when multiple entry points are available.
See this article for more info.
Is main() (or Main()) in C, C++, Java or C#, a user-defined function or a built-in function?
It's a user-defined function that is necessary for a program to execute. When you go to run your program in the compiled language, the main function is what is executed. For instance, in Java, if you have a function of the signature public static void main(String ... args) in a class then that class can be executed, as the JVM will execute the contents of that main method.
Example in Java:
public class Test {
public static void main(String ... args) {
System.out.println("Hello World");
}
}
...
javac Test.java
...
java Test
Results in "Hello World" being printed to the console.
I'm not sure what you mean by built-in vs. user defined. Almost no language actually gives your user-defined function the privilege of being the true entry-point into the program. C++, any .NET language, and Java all have hidden (built-in) entry point methods that in turn call your user-defined Main method (or whatever the entrypoint method for that language is called -- in .NET it can be named anything, although C# and VB.NET force it to be called Main).
So yes, virtually every language has a concept of a method that is automatically called, and this method is a user-defined method and usually mandatory. But virtually every language also has a built-in entry point method that actually sets up the framework and/or memory management for the process before invoking your user-defined "entry-point" function.
Quote from the C Standard (emphasis is mine):
5.1.2.1 Freestanding environment
In a freestanding environment (in
which C program execution may take
place without any benefit of an
operating system), the name and
type of the function called at
program startup are
implementation-defined. Any
library facilities available to a
freestanding program, other than the
minimal set required by clause 4,
are implementation-defined.
main(), in a freestanding environment, is very much a user-defined function.
It's a required user defined function (the entry point for executables)...
It is not "built-in" in any language, in a sense that there is no standard implemented-for you main() avialable.
For C/C++/Java, it is a function with a special property, namely, the function that will be called at the start of your program after all the static setup is done. E.g. entire C program's execution path is:
Do some initialization code
Call main()
Exit.
As such, it has a standard declaration (# of parameters passed from command line + array of "strings" - however the language implements that - which are the actual arguments from command line)
In C/C++, it a standard so its built in and reconized.
Java, not sure, no experience
C# - Its a part of a class so its defined by you.
All of these are defined by you -- you tell it what to do.
It's a user defined function that is called by the language's runtime library. For example, a C runtime library will grab the command line arguments and sometimes environment variables from the operating system and pass them into to your main() function.
Different language runtimes usually perform the same operation in one form or another, and will throw some sort of error if the function it tries to call doesn't exist.
It's declaration is built-in. It's definition is user supplied, or in some cases supplied by an application framework that has some other entry point, or in the case of most event-driven GUI frameworks, no single user-defined entry point.
In Java main(String[] args) is the entry point for applications by convention (to make C++ programmers comfortable). For applets or servlets the invocation of code happens differently. Note that a jar may contain any or none of these entry points and that each class may contain a main so a given jar can be invoked in many different ways as an applcation if so desired.