2

I've recently been messing with scripting in C# for making simulations in unity, and I've found it a little strange how in C# we can reference classes from different files without importing them.

Ex. in python, if we have to files in the same dir, we would do something like this

from OtherFile import className
instance = className()

Or if everything is a package, we can import from parent dirs like this

from ..utils.mathFunctions import NoiseFunction

In C#, I have multiple files in different locations, all under a single parent directory, and in any file, I'm able to use any other (public) class in any other file without importing, or ensuring they are in the same directory.

How does this happen?

Shrey Joshi
  • 1,066
  • 8
  • 25
  • 1
    I believe the answer that you are looking for is the "namespace". Each class / method is generally tied to a namespace and when you use `using namespace`, it will make all the methods in that namespace available for use. – Jawad Dec 28 '20 at 17:50
  • All cs files in an assembly (in a csproj) are compiled together, and to access separate assemblies (normally separate dll) you need a reference (that is included in the csproj). Some reference (to mscorlib for example) are implicit and automatic. – xanatos Dec 28 '20 at 17:50
  • Your various c# files are compiled together into an *assembly*. See: [Assembly definitions (Unity)](https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html) and [What exactly is an Assembly in C# or .NET?](https://stackoverflow.com/q/1362242/3744182). Both public and internal classes in the same assembly can reference each other, see [Accessibility Levels (C# Reference)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/accessibility-levels). – dbc Dec 28 '20 at 17:50
  • If you're using .NET Framework, check the `.csproj` file and you'll find several `` entries in it. For .NET Core and .NET 5.0, the structure is different. Check [this link](https://learn.microsoft.com/en-us/dotnet/core/tools/csproj#default-compilation-includes-in-net-core-projects) for more information about that. – 41686d6564 stands w. Palestine Dec 28 '20 at 17:53
  • The real magical part of the compiler is that it can do multiple passes (at least two) to collect the new symbols you introduce... So you can use a method and the method is written in the same file 200 lines below. The compiler needs at least two passes, one to collect all the classes/methods you define, and one to compile the code that uses those methods/classes. – xanatos Dec 28 '20 at 18:25

2 Answers2

4

When calling the compiler, the multiple source files (or the directory containing them) are provided as command line arguments. You can do this manually or by providing a project file containing the names of the source files.

So the source files are known to the compiler. When talking about "importing" files, we talk about referencing namespaces in C#. But namespaces and even classes can span across multiple source files, there is no 1:1 relation between what you reference in your code and the files containing more code.

If you want to include libraries (3rd party code) in your project, you must reference them in your project file, before you can work with them. Additionally, you have then to reference their namespace(s) in your code. Both applies also to some assemblies provided by Microsoft, even in the well-known System[...] namespaces.

Referencing a namespace is often done by placing a using statement at the beginning of the code file. Another way is to reference the namespace right where you are using it.

// Option 1, using the using statement:
using System;
namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
// Option 2, without using statement:
namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello World!");
        }
    }
}
Cellcon
  • 1,245
  • 2
  • 11
  • 27
  • _If you want to include libraries (3rd party code)_ Even many "System" assemblies must be added to the reference list manually – xanatos Dec 28 '20 at 17:56
  • @xanatos: Well, that is right, but Microsofts assemblies are also 3rd party code from a programmers view, so I included them in my statement, but i will add a note anyway. – Cellcon Dec 28 '20 at 17:58
0

The files that you are wondering about are probably in the same namespace. Many object-oriented languages use namespaces to control the scope of class declarations.

It is important to note that the folder structure is not really why you can "just use them." They just happen to have the same namespace. In C#, there is really no requirement that classes need to be namespaced like their folder structure.

Nechemia Hoffmann
  • 2,118
  • 2
  • 12
  • 21