2

I'm writing C# code, to be compiled with the command line compiler csc (not the IDE), that needs an object to represent a range of integers.

Happily, such a thing was introduced in C# 8.0: https://www.geeksforgeeks.org/range-structure-in-c-sharp-8-0

Unhappily, it's not working. To make sure I'm not misunderstanding how to use it, I just copied the sample program in its entirety from the above page, and tried compiling it:

C:\t>csc a.cs
Microsoft (R) Visual C# Compiler version 4.2.0-4.22281.5 (8d3180e5)
Copyright (C) Microsoft Corporation. All rights reserved.

a.cs(18,3): error CS0246: The type or namespace name 'Range' could not be found (are you missing a using directive or an assembly reference?)
a.cs(18,14): error CS0518: Predefined type 'System.Range' is not defined or imported

The sample code does specify using System so it doesn't seem to be a missing using directive.

Range was introduced in C# 8.0. As I understand it, this version of the language was introduced in an update to VS 2019. I'm using the compiler that was installed with VS 2022, so it should support 8.0.

What am I missing?

rwallace
  • 31,405
  • 40
  • 123
  • 242
  • 7
    `csc` is the wrong compiler. Use `dotnet build` instead – Panagiotis Kanavos Oct 18 '22 at 07:45
  • @PanagiotisKanavos Okay, so which compiler would be the right one? – rwallace Oct 18 '22 at 07:46
  • 2
    Use `dotnet build` – Panagiotis Kanavos Oct 18 '22 at 07:47
  • 1
    @PanagiotisKanavos should be an answer – Jeremy Thompson Oct 18 '22 at 07:48
  • 1
    csc is for the old .Net Framework – Jimbot Oct 18 '22 at 07:51
  • @PanagiotisKanavos `dotnet build` requires a C# project file as input. This isn't a C# project; it's a multilanguage project that needs to compile one single-file C# program for a certain purpose. I selected C# in preference to Kotlin for this project (despite the JVM having better performance), specifically because C# provides, or at least has hitherto provided (when I last used C#), better support for straightforward commandline compiling. Seems like I'm not up to speed on recent development; is `csc` being deprecated, and if so, why? – rwallace Oct 18 '22 at 07:51
  • 2
    `csc` is the C# compiler for .NET *Framework*, and .NET Framework does not support every new language feature, a notable one is the `Range` type. Instead use the .NET Core (now named just .NET) compiler, i.e `dotnet build` (and the [`dotnet` CLI tool](https://learn.microsoft.com/en-us/dotnet/core/tools/) in general) – MindSwipe Oct 18 '22 at 07:51
  • @MindSwipe Aha! So what is the recommended way of doing things nowadays if you just want to compile a single-file C# program, not a project? – rwallace Oct 18 '22 at 07:52
  • 1
    `csc` has a `-langversion:?` switch that shows which versions it supports. On my machine, with csc `4.4.0-2.22430.14` I get 1-11, which is the default. What does `a.cs` contain? – Panagiotis Kanavos Oct 18 '22 at 07:53
  • @PanagiotisKanavos Ah! That confirms `csc` should be supporting language up to 10.0. a.cs contains an unchanged copy-paste of the example program in the linked webpage. – rwallace Oct 18 '22 at 07:55
  • 2
    `just want to compile a single-file C# program,` compile into what? Targeting which runtime, using which packages? In any case, what does `a.cs` contain? There may be an actual error in the code. Or the compiler is complaining that you really didn't specify where to find `System.Range`, which is in `System.Runtime` – Panagiotis Kanavos Oct 18 '22 at 07:55
  • @PanagiotisKanavos Simple commandline program whose job will involve reading some text files, doing some calculation and writing another text file, not using any third-party libraries. Runtime – which runtime is one supposed to be targeting nowadays when using C# for that kind of job? – rwallace Oct 18 '22 at 07:58
  • @rwallace there's no "nowadays" - you couldn't compile C# files that required other assemblies in any version. Those assemblies always appeared in project files. When you run the project file those assemblies are passed as referenced assemblies. In all versions, if you forget to reference the assemblies you want you get an error – Panagiotis Kanavos Oct 18 '22 at 08:00
  • 1
    The `Range` struct was introduced in .NET Core 3.0, so you'd need that or later (.NET Core 3.1, .NET 5, .NET 6 or release candidate of .NET 7) to use it. – Matthew Watson Oct 18 '22 at 08:01
  • @PanagiotisKanavos Actually, compiling and running a simple 'hello world' program with `csc` alone still works fine, just like it always did. Possibly `csc` is adding default assembly references to the resulting binary of its own accord; that's fine. The point is that the only input it required, was a single source file. – rwallace Oct 18 '22 at 08:02
  • @MatthewWatson Right. Is there a way to tell `csc` to use 'whatever is the latest version of .NET/Core currently installed'? – rwallace Oct 18 '22 at 08:03
  • @PanagiotisKanavos Sure. The point is that given `hello.cs` consisting of `Console.WriteLine("hello");`, `csc hello.cs` successfully builds `hello.exe` without requiring any other input; the compiler adds required assembly references of its own accord. – rwallace Oct 18 '22 at 08:15

0 Answers0