3

Having a bit of an issue getting this error to clear when compiling with csc.exe on Win10. I am very new to C#. Doing a bit of self learning before I start attending classes in Jan.

Following a "TeamTreehouse" tutorial on doing this. They are using mono in their workspaces. I am preferring to use vscode on my machine to get familiar with using C# outside of mono.

From what I have read on CS0501 with my issue is that it is a compiler issue that shouldn't be an issue but using csc.exe is causing an issue. Adding abstract gives a whole new set of issues.

Command using to compiler from terminal is csc *.cs

I am not sure where to look next for a solution. I haven't been able to find a working one using what is called Auto-Implemented Properties.

The error is

Invader.cs(6,39): error CS0501: 'TreehouseDefense.Invader.Location.get' must declare a body because it is not marked abstract or extern

Invader.cs(6,52): error CS0501: 'TreehouseDefense.Invader.Location.set' must declare a body because it is not marked abstract or extern

The code Invader.cs

namespace TreehouseDefense {

    class Invader {

        public MapLocation Location { get; private set; }

    }
}

The Project Object_Project.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

</Project>
Community
  • 1
  • 1
Clayton Lewis
  • 394
  • 2
  • 16
  • 1
    I think, your problem is compiler that is of early version that does not support Auto-property – T.S. Dec 18 '18 at 15:25
  • I think for .NET Core projects, you should be using the [dotnet build](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-build?tabs=netcore2x) command. Or just use the Build command from within VSCode. – itsme86 Dec 18 '18 at 15:27
  • @itsme86 `msbuild` works too – T.S. Dec 18 '18 at 15:30
  • When you run just "csc" what version number does it show? – Jon Skeet Dec 18 '18 at 15:45
  • @itsme86 `dotnet build` is working to compile it looks like but it is not generating an exe. Also just found `dotnet publish` which is creating over 200 files rather than a self contained exe like `csc` does – Clayton Lewis Dec 18 '18 at 15:47
  • @JonSkeet `Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.8745 for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727 Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.` is what it shows – Clayton Lewis Dec 18 '18 at 15:48
  • `dotnet publish` shouldn't create 200 files unless you've got that many dependencies - in which case you'd need those files anyway. Not that I'd use it most of the time - just `dotnet run` is fine generally. – Jon Skeet Dec 18 '18 at 15:56

2 Answers2

4

You're using the C# 2 compiler, from 2005. C# 2 didn't support automatically implemented properties - along with all kinds of other features which are part of modern C#.

You want to be running a C# 7 compiler. I'd suggest installing Visual Studio 2017 Community Edition, and using the Visual Studio 2017 Command Prompt so that the right version of csc is on the path.

Or just download the .NET Core SDK and use the dotnet command to build and run instead of csc.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks for this. This led me to to a bunch of new articles I hadn't found. Posted a solution I was able to get to work. – Clayton Lewis Dec 18 '18 at 19:20
0

I want to thank everyone for help with this. It got me pointed in a good direction. Have been doing a bit more reading and found this as a solution. The following way is the easiest way I found that could make a self contained EXE. I was not able to get dotnet build, dotnet publish, or msbuild to work. Still have a lot to learn.

This will install Microsoft (R) Visual C# Compiler version 2.10.0.0 (b9fb1610)

  • Install NuGet v4.8.1 Link to Download
  • Opened NuGet in Command Prompt and ran nuget install Microsoft.Net.Compilers
  • Moved the package it downloaded to C:\Windows\Microsoft.NET
  • Added a new Environment Variable to PATH with C:\Windows\Microsoft.NET\Microsoft.Net.Compilers.2.10.0\tools (Also moved it above the existing one I had, could be removed as well)

csc *.cs now makes a self contained EXE

Clayton Lewis
  • 394
  • 2
  • 16