Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: must declare a body because it is not marked abstract or extern

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>
like image 218
Clayton Lewis Avatar asked Feb 02 '26 10:02

Clayton Lewis


1 Answers

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.

like image 90
Jon Skeet Avatar answered Feb 04 '26 23:02

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!