2

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");
        }
    }
}
3
  • 2
    A console program can only have 1 main entry point Commented Apr 6, 2021 at 10:31
  • The operating system can't magically know what you want ,hence one entry point .... so, what are the rules here Commented Apr 6, 2021 at 10:34
  • I understand that there can only be 1 main method / entry point. Should I then just rename the methods in the 2 files ? Commented Apr 6, 2021 at 10:37

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.