> For the complete documentation index, see [llms.txt](https://til.cazzulino.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://til.cazzulino.com/dotnet/use-c-9-records-in-non-net5.0-projects.md).

# Use C# 9 records in non-net5.0 projects

The new C# 9 records syntax is quite nice:

```csharp
[DebuggerDisplay("{Name} = {Value}")]
record ResourceValue(string Name, string Value, bool HasFormat)
{
    public bool IsIndexed { get; init; }
    public List<string> Format { get; } = new List<string>();
}
```

When using it in a non-*net5.0* project (i.e. *netstandard2.0*), [compilation fails with an error](https://github.com/dotnet/roslyn/issues/45510):

```
Predefined type 'System.Runtime.CompilerServices.IsExternalInit' is not defined or imported
```

To workaround the issue, simply declare the missing type in your project:

```csharp
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.ComponentModel;

namespace System.Runtime.CompilerServices
{
    /// <summary>
    /// Reserved to be used by the compiler for tracking metadata.
    /// This class should not be used by developers in source code.
    /// </summary>
    [EditorBrowsable(EditorBrowsableState.Never)]
    sealed class IsExternalInit
    {
    }
}
```

([as seen elsewhere too](https://github.com/dotnet/runtime/blob/master/src/libraries/System.Text.Json/tests/Serialization/IsExternalInit.cs)). Note the class doesn't even need to be public.

If you're multitargeting net5.0 and other TFMs, just add this bit of MSBuild to remove it for net5.0 since it's built-in:

```markup
<ItemGroup>
  <Compile Remove="IsExternalInit.cs" Condition="'$(TargetFramework)' == 'net5.0'" />
</ItemGroup>
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://til.cazzulino.com/dotnet/use-c-9-records-in-non-net5.0-projects.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
