{"id":2190,"date":"2024-10-18T10:26:18","date_gmt":"2024-10-18T10:26:18","guid":{"rendered":"https:\/\/www.w3computing.com\/articles\/?p=2190"},"modified":"2024-10-18T10:26:22","modified_gmt":"2024-10-18T10:26:22","slug":"how-to-work-with-nullable-reference-types-in-c-8-0","status":"publish","type":"post","link":"https:\/\/www.w3computing.com\/articles\/how-to-work-with-nullable-reference-types-in-c-8-0\/","title":{"rendered":"How to Work with Nullable Reference Types in C# 8.0+"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Nullable reference types are one of the significant new features introduced in C# 8.0. The goal is to help developers avoid the most common and frustrating issues in C# development: null reference exceptions, often referred to as the &#8220;billion-dollar mistake.&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before C# 8.0, all reference types were implicitly nullable, meaning that any reference type variable could hold either a reference to an object or a <code>null<\/code> value. However, this lack of clarity often led to runtime errors when <code>null<\/code> was assigned to variables or passed around, resulting in null reference exceptions. C# 8.0 introduced nullable reference types to provide a way to explicitly mark variables as nullable or non-nullable, thereby providing better compile-time checks and reducing the likelihood of runtime issues.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This tutorial will guide you through everything you need to know about nullable reference types in C# 8.0 and beyond, explaining how they work, how to enable them, and how to manage them effectively in your codebase.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Are Nullable Reference Types?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In C# 8.0 and later, nullable reference types allow developers to express whether a reference type can be <code>null<\/code> or not. The compiler enforces this with warnings and errors where appropriate, helping developers write safer, more robust code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Nullable reference types are split into two categories:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Non-nullable reference types<\/strong>: These are the default reference types, which cannot be assigned a <code>null<\/code> value.<\/li>\n\n\n\n<li><strong>Nullable reference types<\/strong>: These reference types can hold a <code>null<\/code> value.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">The idea is simple: if you declare a reference type variable as nullable, the compiler knows that it can potentially hold a <code>null<\/code> value and will issue warnings if you&#8217;re not handling the <code>null<\/code> case appropriately. If the variable is non-nullable, the compiler will ensure that you&#8217;re not assigning <code>null<\/code> to it or accessing it without initializing it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Enable Nullable Reference Types<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Nullable reference types are not enabled by default in C# 8.0+ because they would break backward compatibility with existing codebases. To enable them, you need to do so explicitly at either the project level or within specific files or scopes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Enabling Nullable Reference Types in the Project File<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can enable nullable reference types for the entire project by modifying the <code>.csproj<\/code> file. Add the following line within the <code>&lt;PropertyGroup&gt;<\/code> section:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">PropertyGroup<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Nullable<\/span>&gt;<\/span>enable<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">Nullable<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">PropertyGroup<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">This setting applies globally to all files in the project.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Enabling Nullable Reference Types in Code<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to enable nullable reference types for a specific file or section of code rather than the entire project, you can use the <code>#nullable<\/code> directive at the top of your C# file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-meta\">#nullable enable<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">You can also disable nullable reference types using:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-meta\">#nullable disable<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">If you want to enforce nullable reference types within a particular block of code, you can use <code>#nullable<\/code> for finer control:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-meta\">#nullable enable<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">MyMethod<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n{\n    <span class=\"hljs-keyword\">string<\/span>? nullableString = <span class=\"hljs-literal\">null<\/span>; <span class=\"hljs-comment\">\/\/ Allowed<\/span>\n    <span class=\"hljs-keyword\">string<\/span> nonNullableString = <span class=\"hljs-literal\">null<\/span>; <span class=\"hljs-comment\">\/\/ Warning: CS8600<\/span>\n}\n\n<span class=\"hljs-meta\">#nullable disable<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">This flexibility allows you to gradually adopt nullable reference types in a large codebase, starting from new code or specific parts of the application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Nullable vs. Non-Nullable Reference Types<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Once nullable reference types are enabled, the C# compiler starts treating reference types in a new way. Let&#8217;s break down the differences between nullable and non-nullable reference types.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Non-Nullable Reference Types<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">By default, reference types like <code>string<\/code>, <code>object<\/code>, <code>List&lt;T&gt;<\/code>, etc., are non-nullable when nullable reference types are enabled.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-meta\">#nullable enable<\/span>\n\n<span class=\"hljs-keyword\">string<\/span> nonNullableString = <span class=\"hljs-literal\">null<\/span>; <span class=\"hljs-comment\">\/\/ Warning: CS8600, null cannot be assigned to a non-nullable reference type<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, the compiler warns you that you are trying to assign <code>null<\/code> to a non-nullable <code>string<\/code>. Non-nullable reference types cannot hold a <code>null<\/code> value. This restriction makes your code safer because you can be confident that whenever you access a non-nullable reference type, it will never be <code>null<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Nullable Reference Types<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Nullable reference types are indicated by appending a <code>?<\/code> to the reference type. These types can hold a <code>null<\/code> value.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-meta\">#nullable enable<\/span>\n\n<span class=\"hljs-keyword\">string<\/span>? nullableString = <span class=\"hljs-literal\">null<\/span>; <span class=\"hljs-comment\">\/\/ No warning<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Here, the compiler understands that <code>nullableString<\/code> can be <code>null<\/code>, so it won&#8217;t issue a warning. However, this comes with a trade-off: you need to handle <code>null<\/code> values appropriately when using nullable reference types.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-meta\">#nullable enable<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Greet<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>? name<\/span>)<\/span>\n{\n    <span class=\"hljs-keyword\">if<\/span> (name != <span class=\"hljs-literal\">null<\/span>)\n    {\n        Console.WriteLine(<span class=\"hljs-string\">$\"Hello, <span class=\"hljs-subst\">{name}<\/span>\"<\/span>);\n    }\n    <span class=\"hljs-keyword\">else<\/span>\n    {\n        Console.WriteLine(<span class=\"hljs-string\">\"Hello, stranger!\"<\/span>);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this case, we check whether <code>name<\/code> is <code>null<\/code> before using it, ensuring that we don&#8217;t run into null reference exceptions at runtime.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Compiler Warnings and Nullability Annotations<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When nullable reference types are enabled, the compiler will analyze your code and provide warnings if you&#8217;re not handling potential <code>null<\/code> values correctly. Let&#8217;s explore the different kinds of warnings you might encounter.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">CS8600: Null Assigned to Non-Nullable Type<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This warning occurs when you&#8217;re trying to assign a <code>null<\/code> value to a non-nullable reference type.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-meta\">#nullable enable<\/span>\n\n<span class=\"hljs-keyword\">string<\/span> nonNullableString = <span class=\"hljs-literal\">null<\/span>; <span class=\"hljs-comment\">\/\/ Warning: CS8600<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">CS8602: Dereference of a Possibly Null Reference<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This warning occurs when you&#8217;re trying to dereference a nullable reference type without first ensuring it&#8217;s not <code>null<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-meta\">#nullable enable<\/span>\n\n<span class=\"hljs-keyword\">string<\/span>? nullableString = <span class=\"hljs-literal\">null<\/span>;\nConsole.WriteLine(nullableString.Length); <span class=\"hljs-comment\">\/\/ Warning: CS8602<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">To fix this, you need to check whether <code>nullableString<\/code> is <code>null<\/code> before accessing it:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-meta\">#nullable enable<\/span>\n\n<span class=\"hljs-keyword\">string<\/span>? nullableString = <span class=\"hljs-literal\">null<\/span>;\n\n<span class=\"hljs-keyword\">if<\/span> (nullableString != <span class=\"hljs-literal\">null<\/span>)\n{\n    Console.WriteLine(nullableString.Length); <span class=\"hljs-comment\">\/\/ No warning<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Alternatively, you can use the null-conditional operator (<code>?.<\/code>) to safely access nullable types:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-meta\">#nullable enable<\/span>\n\n<span class=\"hljs-keyword\">string<\/span>? nullableString = <span class=\"hljs-literal\">null<\/span>;\n\nConsole.WriteLine(nullableString?.Length); <span class=\"hljs-comment\">\/\/ No warning<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this case, if <code>nullableString<\/code> is <code>null<\/code>, the expression will evaluate to <code>null<\/code>, and nothing will be printed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">CS8603: Null Returned from a Non-Nullable Type<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This warning occurs when you&#8217;re returning <code>null<\/code> from a method that has a non-nullable return type.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-meta\">#nullable enable<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">string<\/span> <span class=\"hljs-title\">GetNonNullableString<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n{\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">null<\/span>; <span class=\"hljs-comment\">\/\/ Warning: CS8603<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">To fix this, you can either change the return type to be nullable or ensure you&#8217;re returning a non-null value.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-meta\">#nullable enable<\/span>\n\n<span class=\"hljs-keyword\">string<\/span>? GetNullableString()\n{\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">null<\/span>; <span class=\"hljs-comment\">\/\/ No warning<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Null Forgiving Operator (<code>!<\/code>)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes, you may know that a variable is not <code>null<\/code> but the compiler can&#8217;t determine that. In such cases, you can use the null-forgiving operator (<code>!<\/code>) to suppress the compiler warning.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-meta\">#nullable enable<\/span>\n\n<span class=\"hljs-keyword\">string<\/span>? nullableString = GetString();\n\nConsole.WriteLine(nullableString!.Length); <span class=\"hljs-comment\">\/\/ No warning<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this example, <code>nullableString!<\/code> tells the compiler, &#8220;I know this value isn&#8217;t <code>null<\/code>, so don&#8217;t warn me about it.&#8221; Be careful when using the null-forgiving operator, as it&#8217;s easy to introduce runtime errors if you mistakenly assume that a value will never be <code>null<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Adopting Nullable Reference Types in Existing Codebases<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Nullable reference types can be a fantastic tool for writing safer code, but adopting them in a large existing codebase can be a bit tricky. Fortunately, C# gives you the tools to adopt nullable reference types incrementally and with care.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Gradual Adoption<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You don&#8217;t have to enable nullable reference types for your entire project at once. You can enable them in specific files or even in particular sections of code using the <code>#nullable enable<\/code> directive, as shown earlier.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This approach allows you to gradually refactor your codebase, starting with new code or the most critical areas. Over time, you can enable nullable reference types in more areas as you improve your code&#8217;s nullability safety.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Null Annotations and Legacy Code<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When working with legacy code that doesn&#8217;t have nullability annotations, you&#8217;ll need to balance the strictness of nullable reference types with the flexibility of existing code. One option is to disable nullable reference types in legacy files using <code>#nullable disable<\/code> to prevent compiler warnings from flooding your project.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, it&#8217;s worth considering adding nullability annotations to your existing codebase over time. This process involves updating method signatures, return types, and variable declarations to explicitly mark them as nullable or non-nullable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using Annotations for External Libraries<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If you&#8217;re working with external libraries that haven&#8217;t yet adopted nullable reference types, the compiler may not have enough information to provide accurate nullability warnings. In such cases, you can use annotations from the <code>System.Diagnostics.CodeAnalysis<\/code> namespace to indicate nullability behavior explicitly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, you can use the <code>[NotNull]<\/code>, <code>[MaybeNull]<\/code>, and <code>[AllowNull]<\/code> attributes to provide hints to the compiler about the nullability of parameters, return types, or properties.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-meta\">#nullable enable<\/span>\n<span class=\"hljs-keyword\">using<\/span> System.Diagnostics.CodeAnalysis;\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Example<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Process<\/span>(<span class=\"hljs-params\">&#91;NotNull] <span class=\"hljs-keyword\">string<\/span>? input<\/span>)<\/span>\n    {\n        <span class=\"hljs-comment\">\/\/ The compiler assumes 'input' is not null in the method body<\/span>\n        Console.WriteLine(input.Length); <span class=\"hljs-comment\">\/\/ No warning<\/span>\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">These annotations help improve nullability checks when working with external code that doesn&#8217;t have full nullability annotations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Nullable Contexts and Annotations in APIs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In addition to enabling nullable reference types in your own code, you can also use nullability annotations to design APIs that clearly communicate nullability expectations. This improves the safety and clarity of your public APIs, making it easier for other developers to use them correctly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Designing APIs with Nullability Annotations<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When designing an API, you can use nullable reference types to make your intentions clear regarding which parameters or return types can be <code>null<\/code> and which cannot.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, consider a simple API for handling user information:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-meta\">#nullable enable<\/span>\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">User<\/span>\n{\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">string<\/span> Name { <span class=\"hljs-keyword\">get<\/span>; <span class=\"hljs-keyword\">set<\/span>; }\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">string<\/span>? Nickname { <span class=\"hljs-keyword\">get<\/span>; <span class=\"hljs-keyword\">set<\/span>; }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">User<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> name, <span class=\"hljs-keyword\">string<\/span>? nickname<\/span>)<\/span>\n    {\n        Name = name ?? <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> ArgumentNullException(<span class=\"hljs-keyword\">nameof<\/span>(name));\n        Nickname = nickname;\n    }\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">string<\/span>? GetNicknameOrDefault()\n    {\n        <span class=\"hljs-keyword\">return<\/span> Nickname ?? <span class=\"hljs-string\">\"No nickname\"<\/span>;\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In this API, <code>Name<\/code> is a non-nullable property, indicating that every <code>User<\/code> must have a name. However, <code>Nickname<\/code> is nullable, reflecting the fact that a user might not have a nickname.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This approach makes the API safer and easier to use, as developers can see immediately whether they need to handle potential <code>null<\/code> values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Nullable Reference Types<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Nullable reference types are a powerful tool, but they can also introduce complexity if not used carefully. Here are some best practices to follow when working with nullable reference types in your codebase:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. Enable Nullable Reference Types Gradually<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">If you&#8217;re working with an existing codebase, enable nullable reference types gradually rather than all at once. This allows you to avoid overwhelming your project with warnings and errors and gives you time to refactor your code to handle <code>null<\/code> values correctly.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. Use Nullable Reference Types to Express Intent<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">When designing new APIs or methods, use nullable reference types to clearly express your intent. If a parameter or return value can be <code>null<\/code>, mark it as nullable. If not, make it non-nullable. This clarity helps other developers (and yourself!) avoid potential null reference exceptions.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3. Always Handle Nullable Values<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Whenever you&#8217;re working with a nullable reference type, be sure to handle the <code>null<\/code> case appropriately. Use null checks, the null-conditional operator (<code>?.<\/code>), or pattern matching to handle nullable values safely.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-meta\">#nullable enable<\/span>\n\n<span class=\"hljs-keyword\">string<\/span>? nullableString = GetString();\n\n<span class=\"hljs-keyword\">if<\/span> (nullableString <span class=\"hljs-keyword\">is<\/span> not <span class=\"hljs-literal\">null<\/span>)\n{\n    Console.WriteLine(nullableString.Length); <span class=\"hljs-comment\">\/\/ Safe access<\/span>\n}\n<span class=\"hljs-keyword\">else<\/span>\n{\n    Console.WriteLine(<span class=\"hljs-string\">\"String is null\"<\/span>);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h4 class=\"wp-block-heading\">4. Avoid Overusing the Null Forgiving Operator<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The null-forgiving operator (<code>!<\/code>) can be useful in certain situations, but it should be used sparingly. Overusing <code>!<\/code> defeats the purpose of nullable reference types, as it disables the safety mechanisms designed to protect your code from null reference exceptions.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">5. Refactor Existing Code to Add Nullability Annotations<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Over time, consider refactoring your existing codebase to add nullability annotations. This process can be time-consuming, but it helps make your code more robust and easier to maintain in the long run.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Nullable reference types in C# 8.0 and beyond are a powerful feature that helps developers write safer, more robust code by reducing the risk of null reference exceptions. By enabling nullable reference types, you can make your codebase clearer and more explicit about nullability, leading to fewer runtime errors and improved code quality.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we\u2019ve covered the key concepts of nullable reference types, including how to enable them, the difference between nullable and non-nullable types, how to handle nullable values safely, and best practices for using this feature effectively.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By following these guidelines and using nullable reference types thoughtfully, you can improve your code&#8217;s null safety and create more reliable and maintainable applications in C#. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nullable reference types are one of the significant new features introduced in C# 8.0. The goal is to help developers avoid the most common and frustrating issues in C# development: null reference exceptions, often referred to as the &#8220;billion-dollar mistake.&#8221; Before C# 8.0, all reference types were implicitly nullable, meaning that any reference type variable [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[8,4],"tags":[],"class_list":["post-2190","post","type-post","status-publish","format-standard","category-csharp","category-programming-languages","entry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Work with Nullable Reference Types in C# 8.0+<\/title>\n<meta name=\"description\" content=\"Nullable reference types are one of the significant new features introduced in C# 8.0. The goal is to help developers avoid the most common\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.w3computing.com\/articles\/how-to-work-with-nullable-reference-types-in-c-8-0\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Work with Nullable Reference Types in C# 8.0+\" \/>\n<meta property=\"og:description\" content=\"Nullable reference types are one of the significant new features introduced in C# 8.0. The goal is to help developers avoid the most common\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.w3computing.com\/articles\/how-to-work-with-nullable-reference-types-in-c-8-0\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-10-18T10:26:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-18T10:26:22+00:00\" \/>\n<meta name=\"author\" content=\"w3compadmin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"w3compadmin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-work-with-nullable-reference-types-in-c-8-0\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-work-with-nullable-reference-types-in-c-8-0\\\/\"},\"author\":{\"name\":\"w3compadmin\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"headline\":\"How to Work with Nullable Reference Types in C# 8.0+\",\"datePublished\":\"2024-10-18T10:26:18+00:00\",\"dateModified\":\"2024-10-18T10:26:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-work-with-nullable-reference-types-in-c-8-0\\\/\"},\"wordCount\":1697,\"articleSection\":[\"C#\",\"Programming Languages\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-work-with-nullable-reference-types-in-c-8-0\\\/\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-work-with-nullable-reference-types-in-c-8-0\\\/\",\"name\":\"How to Work with Nullable Reference Types in C# 8.0+\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\"},\"datePublished\":\"2024-10-18T10:26:18+00:00\",\"dateModified\":\"2024-10-18T10:26:22+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"description\":\"Nullable reference types are one of the significant new features introduced in C# 8.0. The goal is to help developers avoid the most common\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-work-with-nullable-reference-types-in-c-8-0\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-work-with-nullable-reference-types-in-c-8-0\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-work-with-nullable-reference-types-in-c-8-0\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Articles Home\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Programming Languages\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/programming-languages\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to Work with Nullable Reference Types in C# 8.0+\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\",\"name\":\"Developer Articles Hub\",\"description\":\"\",\"alternateName\":\"Developer Articles\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\",\"name\":\"w3compadmin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780747165\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780747165\",\"contentUrl\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780747165\",\"caption\":\"w3compadmin\"},\"sameAs\":[\"http:\\\/\\\/w3computing.com\\\/articles\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Work with Nullable Reference Types in C# 8.0+","description":"Nullable reference types are one of the significant new features introduced in C# 8.0. The goal is to help developers avoid the most common","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.w3computing.com\/articles\/how-to-work-with-nullable-reference-types-in-c-8-0\/","og_locale":"en_US","og_type":"article","og_title":"How to Work with Nullable Reference Types in C# 8.0+","og_description":"Nullable reference types are one of the significant new features introduced in C# 8.0. The goal is to help developers avoid the most common","og_url":"https:\/\/www.w3computing.com\/articles\/how-to-work-with-nullable-reference-types-in-c-8-0\/","article_published_time":"2024-10-18T10:26:18+00:00","article_modified_time":"2024-10-18T10:26:22+00:00","author":"w3compadmin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"w3compadmin","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/www.w3computing.com\/articles\/how-to-work-with-nullable-reference-types-in-c-8-0\/#article","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/how-to-work-with-nullable-reference-types-in-c-8-0\/"},"author":{"name":"w3compadmin","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"headline":"How to Work with Nullable Reference Types in C# 8.0+","datePublished":"2024-10-18T10:26:18+00:00","dateModified":"2024-10-18T10:26:22+00:00","mainEntityOfPage":{"@id":"https:\/\/www.w3computing.com\/articles\/how-to-work-with-nullable-reference-types-in-c-8-0\/"},"wordCount":1697,"articleSection":["C#","Programming Languages"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.w3computing.com\/articles\/how-to-work-with-nullable-reference-types-in-c-8-0\/","url":"https:\/\/www.w3computing.com\/articles\/how-to-work-with-nullable-reference-types-in-c-8-0\/","name":"How to Work with Nullable Reference Types in C# 8.0+","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/#website"},"datePublished":"2024-10-18T10:26:18+00:00","dateModified":"2024-10-18T10:26:22+00:00","author":{"@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"description":"Nullable reference types are one of the significant new features introduced in C# 8.0. The goal is to help developers avoid the most common","breadcrumb":{"@id":"https:\/\/www.w3computing.com\/articles\/how-to-work-with-nullable-reference-types-in-c-8-0\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.w3computing.com\/articles\/how-to-work-with-nullable-reference-types-in-c-8-0\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.w3computing.com\/articles\/how-to-work-with-nullable-reference-types-in-c-8-0\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Articles Home","item":"https:\/\/www.w3computing.com\/articles\/"},{"@type":"ListItem","position":2,"name":"Programming Languages","item":"https:\/\/www.w3computing.com\/articles\/programming-languages\/"},{"@type":"ListItem","position":3,"name":"How to Work with Nullable Reference Types in C# 8.0+"}]},{"@type":"WebSite","@id":"https:\/\/www.w3computing.com\/articles\/#website","url":"https:\/\/www.w3computing.com\/articles\/","name":"Developer Articles Hub","description":"","alternateName":"Developer Articles","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.w3computing.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561","name":"w3compadmin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780747165","url":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780747165","contentUrl":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780747165","caption":"w3compadmin"},"sameAs":["http:\/\/w3computing.com\/articles"]}]}},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"w3compadmin","author_link":"https:\/\/www.w3computing.com\/articles\/author\/w3compadmin\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/2190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/comments?post=2190"}],"version-history":[{"count":2,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/2190\/revisions"}],"predecessor-version":[{"id":2192,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/2190\/revisions\/2192"}],"wp:attachment":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/media?parent=2190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/categories?post=2190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/tags?post=2190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}