user14545937
user14545937

Reputation:

C# multiple entry points in console application

Program.cs is my main file and main method within is the entry point for my application. I have 2 other files, file1.cs and file2.cs.Both classes have its own string that I want to output using the Console.WriteLine function. When I run the program.cs file the output from the 2 other files dont show. If I run the code separately adding static void Main(string[] args) then it works.

I tried changing the name of the methods as well but no success. I also tried importing the namespaces in the program.cs file.

using File1;
using File2;

Can anyone provide some guidance or docs that could help me ?Thanks

Program.cs

using System;

namespace MyConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome User");
        }
    }
}

file1.cs


namespace File1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This is string 1");
        }
    }
}

file2.cs


namespace File2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This is string 2");
        }
    }
}

Upvotes: 2

Views: 2378

Answers (1)

Juan Medina
Juan Medina

Reputation: 559

You have to call the methods from program.cs ro be able to run those. Something like this:

using System;

namespace MyConsole
{
    class Program
    {
       static void Main(string[] args)
        {
            Console.WriteLine("Welcome User");
            File1.Program.Main(args);
            File2.Program.Main(args);
        }
    }
}

And you should make Program class as static too on File1 and File2

Upvotes: 4

Related Questions