TransWikia.com

Why I cannot see the output of the program I wrote in C# in VS Code when I run it in the terminal

Stack Overflow Asked by Roksana on December 27, 2021

I am newbie to C# and I’m doing some easy practices in VS code.

I have an array of colors and a method that returns the corresponding index of the inputted color(Resistor Color exercise).

I want to see the result of the program when I run the program in the terminal, but nothing shows!

For example, when I choose the "brown" as an input for the method, I expect to see the 1 in the terminal.

I appreciate if anyone can check what is wrong with the code I wrote.

using System;

public class ResistorColor
{
    private void  Main(string[] args)
    {
        int a = ColorCode("brown");
        Console.WriteLine(a.ToString());
    }

    public static string[] colors = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white"};
    public static int ColorCode(string color)
    {
       return Array.IndexOf(colors, color);
    }

    public static string[] Colors()
    {
        return colors;
    }

}

3 Answers

I assume that you are working on a project downloaded from exercism.io.
Those projects are Test Projects. meaning that they are not supposed to define any Main method. Test projects already internally have some sort of Main method!
Anyway, if you want to convert that project into a console project which you can run, you should :

1. remove the references to the test libraries.

Currently your .csproj file contains lines like this:

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />

Remove these 3 lines. These are adding a dependency to your project and actually converting it into a test project.

2. Define output type of your project:

Make sure that in the '.csproj' file, output type of the project is specified. You should use Exe for OutputType

<PropertyGroup>
   <OutputType>Exe</OutputType>
   <TargetFramework>netcoreapp3.1</TargetFramework>
 </PropertyGroup>

3- Use a valid signature for Main method:

There exists a few valid signatures for Main and in all of them, it should be static

4- You're done!

now you can run : dotnet run and see the output.

Update:

You may have a different version of .net core sdk installed. So do not copy its value from snippets I've wrote, stick to the version you already have. These snippets are just for explaining.

Answered by Shahryar Saljoughi on December 27, 2021

It looks likely that you have two Main methods; the instance Main method you've shown in the question, and a static method called Main somewhere else.

A quick way to test this is to simply rename your Main method to something else (Main2 perhaps; naming is hard!); if the code still compiles, it was never the entry point.


To be the "entry point" (i.e. the thing that runs when you run your exe), a method needs to be static (and be called Main, and have a suitable signature such as void Main(string[]), although many other signatures are allowed), so the code shown in the question is just a method that happens to be called Main. The actual code that runs when you run your exe is probably in a different file, typically Program.cs - and it is probably empty.

Answered by Marc Gravell on December 27, 2021

Youu need to also add ReadLine in order to see the output:

...
Console.WriteLine(a.ToString());
Console.ReadLine();

Answered by apomene on December 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP