What's the difference between .cs with top-level statements and .csx programs?

125 views Asked by At

As mentioned, I was wondering what is the difference between C# program with only top-level statements and no namespace nor main class and a csx script.

I'm using dotnet-script right now and it behaves the same for both .cs and .csx and I'm a bit confused.

1

There are 1 answers

1
StriplingWarrior On

Because .csx is intended for scripting without a whole project involved, it has support for a few directives that aren't necessary when you're working in the context of a project.

For example, there an #r directive to load a DLL, and a #load directive to load another .csx file. See the example at https://learn.microsoft.com/en-us/archive/msdn-magazine/2016/january/essential-net-csharp-scripting.

In a C# project, all the C# files included in the project are automatically loaded, and there are directives in the project file to include nuget packages, dlls, etc.