1

For learning purposes, I use csc.exe myprogram.cs shipped with Visual Studio Community (my version 17.3.4) to compile basic C# programs. It mostly works, except when using what appears to be latest language features, for example, array range shorthand array[0..] or element from end shothand array[^1].

For example, when trying to access [^1] element.

Arrays.cs(58,47): error CS0518: Predefined type 'System.Index' is not defined or imported
Arrays.cs(58,47): error CS0656: Missing compiler required member 'System.Index..ctor'

I tried passing -langversion:preview to csc but still not working. Also I was unable to find proper usings for it to work.

Can I somehow get those features to work with basic csc compilation? They worked when I created csproj file and used dotnet build, but doing so was multiple times slower than using just csc. Thanks!

user124
  • 1,632
  • 1
  • 11
  • 11
  • 3
    What .net sdk are you targeting – Daniel A. White Sep 22 '22 at 22:18
  • Why are you using `csc` and not `dotnet build`? – DavidG Sep 22 '22 at 22:21
  • I executed csc with default params from Developer PowerShell, like this: `csc .\Arrays.cs` . I am using csc to get better speed... I know that I can use dotnet build, and it works for my file. It also seems interesting to me that csc behaves like this - works with some features and not with others. – user124 Sep 22 '22 at 22:21
  • How is it better speed? Have you even tried it? – DavidG Sep 22 '22 at 22:28
  • 1
    In general you need a boatload of /ref parameters to tell the compiler about .net reference assemblies. That's was made a bit easier, the same directory that contains csc.exe has a file named csc.rsp which contains precooked parameters. But not for the new stuff, /ref:System.Runtime.dll is required here. – Hans Passant Sep 22 '22 at 22:34
  • Hans, thank you a lot. However it still is not working with this additional ref. Am i doing it wrong? Command: `csc.exe /r:System.Runtime.dll .\Arrays.cs` , same error. – user124 Sep 22 '22 at 23:11
  • 1
    The compiler is just one component in a much larger ecosystem of tools here for any new .NET version; there's absolutely no reason to be directly invoking csc by hand here if you want to use new features. – Jason Malinowski Sep 22 '22 at 23:44

1 Answers1

3

Arrays.cs(58,47): error CS0518: Predefined type 'System.Index' is not defined or imported Arrays.cs(58,47): error CS0656: Missing compiler required member 'System.Index..ctor'

The compiler uses several types to lower index / range expressions to IL. One of those types is System.Index. This error is the compiler noting that it cannot find that type which is necessary to lower that expression to IL.

This feature was added as a part of netcoreapp3.1 where csc defaults to compiling for .NET Framework applications. These types are not present in the standard set of references you get for .NET Framework hence this is why you get the error.

They worked when I created csproj file and used dotnet build ...

That worked because your project file contained something like the following:

<TargetFramework>net6.0</TargetFramework>

This targeted your application for .NET Core, the build command passed along the standard references for that and those included the System.Index type.

... but doing so was multiple times slower than using just csc.

Building a .NET application involves more than just compiling code. It is finding the correct set of references, building dependencies, compiling and deploying the resulting binaries. It's more work hence it's going to take longer to complete. Multiple times slower is not expected, particularly for repeated executions, but it will take more time that invoking csc, or any ofeth other tools used in build, directly.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454