I am wondering how linking between files works behind the scenes. It's easy to add reference to another project in Visual Studio and point the namespace if it's different. But how do I do this without IDE? Sometimes I want to test something and don't want to create a new Console App for the 1000th time. I guess the notepad is better in such situation.
Here, on the screen, you can see 2 simple .cs files located in the same folder: https://imgur.com/a/Aync64w Both have the same namespace. Probably there's some way to point the path to another file to make Supportive class visible in Program.cs? (Creating .dll and linking it didn't help, maybe I did it wrong)
CodePudding user response:
Your solution is valid as of C# 10 which came out in late 2021. However it appears that you are running on an older version of C# and the .NET framework. C# 10 introduced File Scoped Namespaces where you can write
namespace Hello;
and the namespace will apply for everything within that file.
In your version of C# namespaces should serve as their own scope. You can rewrite your classes to work by doing the following:
Program.cs
using System;
namespace Hello{
public class Program{
static void Main(){
Console.WriteLine(Supportive.Addition(5, 2));
}
}
}
Supportive.cs
namespace Hello{
public static class Supportive{
public static int Addition(int a, int b) { return a b; }
}
}
You may also need to compile with csc /out:Program.exe *.cs
This tells the C# compiler to compile all .cs files in the current directory, and save the output as "Program.exe"
I don't currently have access to this version of C#, but this should hopefully work!