I am having a strange problem with VS Code. In my current workspace, I have two C# files. One is 'Program.cs' the default file that comes with .NET Framework. The other is a file I created called 'helloworld.cs'.
However, when I run the 'helloworld.cs' file, the output in the terminal is the output of the 'Program.cs' file. Am I just being stupid or is there some quick easy fix?
Program.cs helloworld.cs (Terminal shows outputs)
edit0: Nothing present in output tab. Have changed settings to execute code in terminal.
edit1: Changed 'void Main()' method in 'helloworld.cs' to 'static void Main()', but "error CS0017: Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point." is returned.
CodePudding user response:
A few things :
- You are breaking some usual best-practices of keeping your directory clean and understandable. In this case, if it is difficult for you to explain to someone why you would have 2 Main() methods, then it is probably best to avoid having them in the first place. This is not to say that you cannot have a method named Main()...however, do recognize that that is the default entry-point given to methods in C# projects, and therefore makes it difficult for you to understand what is going on, and inherently, it may be difficult for others trying to help to also understand your code - especially so when you add not 1 but 2 Main() methods in your code.
- As per MSDN (Main() and command-line arguments), your Test class is violating the principle of classes with Main() methods, in that it is not declared as being a static method, hence C# compiler does not recognize it as the 'usual' Program.Main() method that we are used to seeing in boilerplate auto-generated C# projects, but rather 'just another' method that 'just happens' to be named 'Main' :
Main is declared inside a class or struct. Main must be static and it need not be public. (In the earlier example, it receives the default access of private.) The enclosing class or struct is not required to be static.
Let's go ahead and change 'Test.Main()' from 'void Main()' to 'public static void Main()', to see what happens if you did follow the convention :
PS C:> dotnet run Test C:\helloworld.cs(5,24): error CS0017: Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point. [C:\VSCodeIssue.csproj]
You now have an error where the compiler is letting you know that if it were to try to follow the convention outlined in the MSDN documentation above, it would be confused about what you want it to actually do. Your instructions to the compiler are confusing and ambiguous as they are.
To fix this if you absolutely must have 2 Main() methods (see #1 and #2 above), you could change your .csproj file to include the 'StartupObject' option :
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<StartupObject>Test</StartupObject>
</PropertyGroup>
</Project>
Now, if you run 'dotnet run', the C# compiler is specifically instructed in the .csproj file of how to resolve the ambiguity that you have provided it in your code, by way of the explicit instruction in the 'StartupObject' XML element.
PS C:> dotnet run Hello
For reference, I'd suggest reformatting your code as follows, for clarity. This code is much more logically easy to follow, and does not require non-intuitive changes to .csproj file to make work:
public partial class Program
{
static void Main(string[] args)
{
if(args.Length>0 && args[0].ToLowerInvariant() == "bongo")
{
PrintBongo();
}
else
{
PrintWordAndSum();
}
}
static void PrintBongo()
{
string bongo = "Hello";
Console.WriteLine(bongo);
}
static void PrintWordAndSum()
{
int myint = 5;
int youint = 7;
int total = myint youint;
string word = "word";
System.Console.WriteLine(total);
System.Console.WriteLine(word);
}
}
Now, you invoke your program as follows to do what was originally in your 'Test' class :
PS C:> dotnet run bongo Hello
And you invoke your program as follows to do what was originally in Program.Main() :
PS C:> dotnet run 12 word
CodePudding user response:
dotnet run runs the project and not the specific source file you selected,
If you want the entry point to be the helloWorld file then you can use the following dotnet run -main:Test
You can look at the following docs for other options to get it working and education
