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?

17
  • 7
    csc is the wrong compiler. Use dotnet build instead Commented Oct 18, 2022 at 7:45
  • 2
    Use dotnet build Commented Oct 18, 2022 at 7:47
  • 1
    @PanagiotisKanavos should be an answer Commented Oct 18, 2022 at 7:48
  • 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 in general) Commented Oct 18, 2022 at 7:51
  • 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 Commented Oct 18, 2022 at 7:55

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.