Home > OS >  Dotnet run command always runs the same code
Dotnet run command always runs the same code

Time:05-28

I apologize in advance if my question seems confusing, I've just started learning C# so I don't know much about it.

so I'm learning to code In C# with Visual studio Code as my IDE. I Followed this tutorial to help in setting up C# on visual studio code.

but I've encountered a problem, normally when I used to want to make a python program, I just make a new file with the python extension and it runs just fine, but this isn't the case in C#. when I followed the tutorial I got a simple hello world program.

Console.WriteLine("Hello, World!");

but then If I want to make a new program I created a new file with the .cs extension in the same folder and wrote a different code in it. but when I wanted to run it using the Dotnet run command in the terminal as I've been taught. it always runs the previous program and gives me that as an output, rather than the newer one. I tried changing the words after the namespace and tried changing the class and also the main function, but it always seems to run the older code. this might just be due to my lack of knowledge but I tried looking it up and found no answer. how could I run the newer program rather than the previous one? if that's even possible?

edit: i've been informed to Add more details and screenshots

This is how my folder looks like

the program named "Program.cs" is the the first one that runs a hello world program and the one named "Thinking with strings.cs" has this code

namespace Code
{
    class Program
    {
        static void main(string[] args)
        {
            Console.WriteLine("hello\nworld");
        }
    }
}

the output like said is Hello, World! rather than,

hello world

I also tried deleting the program.cs file but what would happen is it tries to run the code.csproj file which contains this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

and of course this is HTML and therefore it would fail so no luck there.

CodePudding user response:

if you go to the Properties of the project (project -> right click -> properties), you'll see the details of the project including the main entry point. Observe it and you probably get your answer.

there's another thing as well, see the two drop down right before the PLAY benter image description hereutton. you'll know what i mean. enter image description here

CodePudding user response:

Turns out I was just thinking of it in a wrong way, and making a new file with the .cs extension just doesn't work. a good way for me is just to make a new folder and run the "dotnet new console" command on there which works like normal. but thinking of each file on its own is the wrong perspective when it comes to C#. thanks for the help "user7313094"

  • Related