Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Way to name Main() method by yourself?

Tags:

main

c#

Quick question, is there a way to call your main method whatever you like ? Or does it have to be called "Main()" ?

like image 228
Chilln Avatar asked Apr 09 '10 19:04

Chilln


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


2 Answers

Note this is a C# convention, not a .NET Runtime convention. You can name your method whatever you'd like in IL:

.module Mane.exe
.subsystem 3
.corflags 9

.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89)
  .ver 2:0:0:0
}

.assembly Mane
{
    .custom instance void [mscorlib]System.Runtime.InteropServices.ComVisibleAttribute::.ctor(bool) = ( 01 00 00 00 00 ) 
    .custom instance void [mscorlib]System.CLSCompliantAttribute::.ctor(bool) = ( 01 00 01 00 00 ) 
    .custom instance void [mscorlib]System.Reflection.AssemblyFileVersionAttribute::.ctor(string) = ( 01 00 07 31 2E 30 2E 30 2E 30 00 00 )
    .custom instance void [mscorlib]System.Resources.NeutralResourcesLanguageAttribute::.ctor(string) = ( 01 00 05 65 6E 2D 55 53 00 00 )

    .permissionset reqmin
               = {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'Execution' = bool(true)}}
    .hash algorithm 0x00008004
    .ver 1:0:0:0
}

.namespace Mane
{
    .class private abstract auto ansi sealed beforefieldinit Program extends [mscorlib]System.Object
    {
        .method private hidebysig static void Mane() cil managed
        {
            .entrypoint
            .maxstack 1
            ldstr "Hello, World!"
            call void [mscorlib]System.Console::WriteLine(class System.String)
            ret
        }
    }
}
like image 155
Jesse C. Slicer Avatar answered Oct 19 '22 23:10

Jesse C. Slicer


You can call your main method something else, but it won't be called as the first method in your application unless it is called Main. There are a few other requirements and things to note too. From MSDN:

  • The Main method is the entry point of your program, where the program control starts and ends.
  • It is declared inside a class or struct. It must be static and it should not be public.
  • It can either have a void or int return type.
  • The Main method can be declared with or without parameters.
  • Parameters can be read as zero-indexed command line arguments.
  • Unlike C and C++, the name of the program is not treated as the first command line argument.
like image 30
Mark Byers Avatar answered Oct 20 '22 00:10

Mark Byers