Results 1 to 6 of 6

Thread: Is it valid to use Multiple main methods in C sharp?

  1. #1
    Join Date
    Dec 2009
    Posts
    32

    Is it valid to use Multiple main methods in C sharp?

    Hi friends,

    I am learning C sharp programming language from one months. I know that C sharp program must contains main method. But my question is that can we use multiple main methods in single C sharp program?. I didn't try this type of program in C# yet. Please let me know you opinion over this C sharp concept. If you know how to use multiple main method then please explain it with suitable example. I would greatly appreciate you help.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Is it valid to use Multiple main methods in C sharp?

    Yes, it is valid to use multiple main methods in the same class of C sharp program. C# includes a feature that enables to define more than one class with the Main method. When you use multiple main methods the compiler will inform you that the main method is already defined in the class. We can have multiple main methods with different arguments. Compiler throws an error if we have a method similar to public static void main(String args[]).

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Is it valid to use Multiple main methods in C sharp?

    Hi friend,

    The following C sharp code will depict you about how to implement multiple Main methods in C sharp:
    Code:
    using System;
    
    class CsA1
    
    {
         public static void Main()
         
       {
            Console.WriteLine("Class CsA1");
         }
    }
    
    class CsB1
    
    {
         public static void Main()
         
        {
            Console.WriteLine("Class CsB1");
         }
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Is it valid to use Multiple main methods in C sharp?

    Hi,

    C Sharp language includes a feature that enables to define more than one class with the Main method. we can use multiple main methods in a class by overloading and not by the overriding. Since 'Main' is the entry point for program execution, there are now more than one entry points. In fact, there should be only one entry point. This problem can be resolved by specifying which Main is to be used to the compiler at the time of compilation.

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Is it valid to use Multiple main methods in C sharp?

    In C sharp the implementation of multiple main method is achieved through "overloading". By using "overriding" it is possible only in the case of child- parent combination. See following example:
    class ASDF
    {
    public static void main(String args[]){}

    }
    class ERT extends ASDF
    {

    public static void main(String args[]){}

    }

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Is it valid to use Multiple main methods in C sharp?

    The C sharp designers included a mechanism by which you are able to define more than one class with a multiple Main method. In addition to this,attempting to compile an C sharp application consisting of multiple C sharp classes with defined Main methods and not specifying the /main switch will result in a compiler error. You can then use the /main:< className > switch with the C# compiler to specify which class's Main method is to be used.

Similar Threads

  1. Differences between int main() and int main(void)
    By BOGUMIL in forum Software Development
    Replies: 5
    Last Post: 10-06-2011, 06:55 AM
  2. Sharp 007SH Hybrid An 3D Clamshell Android Phone by Sharp
    By jackalz in forum Web News & Trends
    Replies: 3
    Last Post: 22-05-2011, 07:28 AM
  3. How to achieve multiple inheritance in C sharp?
    By Rum in forum Software Development
    Replies: 5
    Last Post: 19-01-2010, 08:51 AM
  4. Get and Set methods
    By garfield1 in forum Software Development
    Replies: 3
    Last Post: 24-11-2009, 02:20 PM
  5. Replies: 3
    Last Post: 14-05-2009, 10:25 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,983,214.77744 seconds with 16 queries