NuGet\Install-Package Required -Version 1.0.0
dotnet add package Required --version 1.0.0
<PackageReference Include="Required" Version="1.0.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add Required --version 1.0.0
#r "nuget: Required, 1.0.0"
// Install Required as a Cake Addin #addin nuget:?package=Required&version=1.0.0 // Install Required as a Cake Tool #tool nuget:?package=Required&version=1.0.0
Use C# 11's required member feature in in older target frameworks.
You may also want to check out Manuel Römer's Nullable project which provides support for .NET's nullable reference type attributes for older target frameworks.
C# 11 added support for the
required keyword. When using C# 11 with target frameworks
⇐ .NET 7.0, using this new feature is not possible because the compiler is missing the
CompilerFeatureRequired attributes, hence making the features
unavailable for any target framework prior to .NET 7.
Luckily, this problem can be solved by re-declaring the attributes as an
in your own project. The compiler will use these custom class definitions and thus allow
you to use the
required keyword in any project.
This repository hosts the code for the "Required" NuGet Package which, when referenced, automatically adds the attributes to the referenced project(s).
The code for the
Required class is added at compile time and gets built into the referencing project.
This means that the resulting project does not have an explicit dependency on the
package, because the code is not distributed as a standard library.
Required is currently compatible with the following target frameworks:
- .NET Standard >= 2.0
You must use a C# version >= 11.0 with the
Requiredpackage - otherwise, your project won't compile. The steps below assume that you are using the new SDK
.csprojstyle. Please find installation guides and notes for other project types (for example
Reference the package
Add the package to your project, for example via:
Install-Package Required --or-- dotnet add package Required
Ensure that the package has been added as a development dependency
.csprojfile and ensure that the new package reference looks similar to this:
<PackageReference Include="Required" Version="<YOUR_VERSION>" PrivateAssets="all" /> <PackageReference Include="Required" Version="<YOUR_VERSION>"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference>
This is especially important for libraries that are published to NuGet, because without this, the library will have an explicit dependency on the
Build the project
Ensure that the project compiles. If a build error occurs, you will most likely have to update the C# language version.
Afterwards, you can immediately start using the attributes.
The included C# file makes use of some compiler constants that can be used to enable or disable certain features.
REQUIRED_DISABLE constant is defined, the attributes needed to support the feature are excluded from the build.
This can be used to conditionally exclude code of this package from the build if it is not required.
In most cases, this should not be required, because the package automatically excludes the code from target frameworks that already support the feature.
By defining the
REQUIRED_INCLUDE_IN_CODE_COVERAGE constant, the
attributes are not applied and the attributes may therefore appear in code coverage reports.
Because the package consists of source files, building works differently than a normal .NET project.
In essence, no build has to be made at all. Instead, the
*.cs files are packaged into a NuGet package
through custom packaging targets.
Open to any contributions!
See the LICENSE file for details.
|.NET||net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows|
|.NET Core||netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1|
|.NET Standard||netstandard2.0 netstandard2.1|
|.NET Framework||net461 net462 net463 net47 net471 net472 net48|
This package has no dependencies.
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.