0

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://i.stack.imgur.com/PGmSq.jpg 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)

Yan Ulya
  • 21
  • 4

1 Answers1

2

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!

A Bad Pun
  • 36
  • 4
  • I have .net 6 and c# 10 installed. The terminal shows that C# Compiler version is 4.4.0-6.22608.27 for some reason. This isn't the problem because "csc Program.cs Supportive.cs" compiled the whole thing. – Yan Ulya Feb 01 '23 at 21:54
  • By the way, "csc /out:Program.exe *.cs" works well. Thank you. – Yan Ulya Feb 02 '23 at 00:19