Why do I get compilation error when trying to use record type in C#?
Asked Answered
M

2

5

EDIT: Thank you everyone! I have never upgraded to a newer version of .NET and language version before. Thus didn't know about .csproj configuration. Even though I did a research before posting a question I was not able to find a solution myself. So, I just leave these two links for further reference, perhaps this might help someone as well.

https://learn.microsoft.com/en-us/dotnet/standard/frameworks

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version

I have upgraded to .NET 5.0.301

And finally got around to try record type in C# 9.0

I wrote a simple code but got an error during compilation.

I use Visual Studio Code as an editor.

VS Code version 1.57.0

C# extension version 1.23.12

Here is my settings.json:

"editor.semanticHighlighting.enabled": true,
"csharp.semanticHighlighting.enabled": true,
"omnisharp.path": "latest"

Set up:

dotnet new sln -n "Test"
dotnet new console -n "TestProject"
dotnet sln Test.sln add  .\TestProject\TestProject.csproj

My code:

using System;

namespace TestProject
{

    class Program
    {
        static void Main(string[] args)
        {
            var person = new Person() { Name = "Tom" };
            person.Name = "Bob";
            Console.WriteLine(person.Name);
        }
    }

    public record Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

}

Problems:

CS0246 The type or namespace name 'Person' could not be found (are you missing a using directive or an assembly reference?)
CS0246 The type or namespace name 'record' could not be found (are you missing a using directive or an assembly reference?)
CS0548 '<invalid-global-code>.Person': property or indexer must have at least one accessor
CS1513 } expected
CS0116 A namespace cannot directly contain members such as fields or methods
CS1022 Type or namespace definition, or end-of-file expected

Any help much appreciated

Metaphor answered 14/6, 2021 at 23:54 Comment(5)
I don't know enough about VS Code to answer. But the error messages make pretty clear that you haven't gotten things configured properly to use the C# version that supports record as a keyword. You need to double-check that the Code version you're using is correct, and that your project is correctly configured to use C# 9 features.Remonstrant
The provided code compiles well under VS2017 with struct instead of record: what is the Framework Target in the project properties? As asked and answered: .NET 5 or lower? But what is the language version tag in the build settings? 9 or 8 or 7 or less? Is the last major or minor used? #44699625Courbevoie
When you do get it working, you can abbreviate your record to public record Person(string Name, int Age)Jefferson
When upgrading to a different version of Net make sure you do a clean build.Errors like you have can be caused by two issues.One the code was not compiled and you have a mismatch between version of Net.The second reason is the automatic upgrade that VS does when it recognizes and different version of Net failed because a library was missing.The upgrade aborts after finding the first missing library so not all library versions get changed and you manually have to modify the version. Simple solution is to delete library from Solution Explorer Reference. Then add again which will update version.Squelch
Thank you everyone! It was indeed a problem in .csproj configuration of TargetFramework and LangVersion properties.Metaphor
T
9

Check your target framework and language version in your .csproj file. You should find something like:

<TargetFramework>net5.0</TargetFramework>
<LangVersion>9.0</LangVersion>

If you don't find these properties, add them inside the <PropertyGroup>...</PropertyGroup> tags. If they are set to older versions, change them.

If this does not solve your problem, you may have installed your .NET SDK incorrectly, or you may have installed it in a directory other than the default one and the VS Code C# extension is not able to find it.

Tarpaulin answered 15/6, 2021 at 2:23 Comment(0)
H
0

I think you can change this code:

public record Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

to this one:

public record Person(string Name, int Age )
Hagler answered 31/3, 2023 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.