{"id":84953,"date":"2023-04-01T08:00:59","date_gmt":"2023-04-01T06:00:59","guid":{"rendered":"https:\/\/code-maze.com\/?p=84953"},"modified":"2023-04-01T10:15:20","modified_gmt":"2023-04-01T08:15:20","slug":"csharp-null-conditional-operators","status":"publish","type":"post","link":"https:\/\/code-maze.com\/csharp-null-conditional-operators\/","title":{"rendered":"Null-Conditional Operators in C#"},"content":{"rendered":"<p>In this article, we will discuss a feature of C# known as null-conditional operators and how it can help prevent the occurrence of null-reference exceptions, which are commonly encountered by developers.<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/csharp-advanced-topics\/NullConditionalOperators\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p>Let&#8217;s dive in!<\/p>\n<h2>What Are Null-Conditional Operators in C# and Why Are They Important?<\/h2>\n<p>Null-conditional operators were introduced in C# 6.0 to provide a way to simplify null-checking code and avoid <a href=\"https:\/\/code-maze.com\/csharp-nullreferenceexception\/\" target=\"_blank\" rel=\"noopener\">null-reference exceptions<\/a>. They also offer a safe way to access members and elements of an <code>object<\/code>\u00a0or collection that may be <code>null<\/code>.<\/p>\n<p>The way they work is straightforward &#8211; if the <code>object<\/code>, or collection, is <code>null<\/code>, the operator returns <code>null<\/code> instead of throwing a <code>NullReferenceException<\/code> otherwise, they return the value.<\/p>\n<p>Let&#8217;s set up our example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class Student\r\n{\r\n    public string? Name { get; set; }\r\n    public List&lt;string&gt; Courses { get; set; }\r\n\r\n    public Student(string name, List&lt;string&gt; courses)\r\n    {\r\n        Name = name;\r\n        Courses = courses;\r\n    }\r\n\r\n    public void Enroll(string course)\r\n    {\r\n        Courses.Add(course);\r\n    }\r\n}<\/pre>\n<p>In a new file, we create a simple <code>Student<\/code> class. It has two properties &#8211; <code>Name<\/code> and <code>Courses<\/code> as well as an <code>Enroll<\/code> method.<\/p>\n<p>We have things ready, let&#8217;s dive in!<\/p>\n<h2>Safe Navigation Operator (?.) And (?[])<\/h2>\n<p>In our application, if objects, or their members, can be <code>null<\/code> we have to appropriately handle them:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">Student? student = null;\r\n\r\nif (student != null)\r\n{\r\n    Console.WriteLine($\"Student name is: {student.Name}\");\r\n\r\n    if (student.Courses != null)\r\n    {\r\n        Console.WriteLine($\"First enrolled course is: {student.Courses[2]}\");\r\n    }\r\n}<\/pre>\n<p>Here, we first check if the <code>student<\/code> variable is <code>null<\/code> before printing its <code>Name<\/code>. Then we check if the <code>Courses<\/code> property is <code>null<\/code> before printing all courses to the console. We do this to guard our application against <code>NullReferenceExpection<\/code>. For simple code, this might be fine but for complex software, this becomes very tedious and lowers code readability.<\/p>\n<p>The null-conditional operator for safe member access, <code>?.<\/code>, and safe element access, <code>?[]<\/code>, were introduced to tackle this problem:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">Console.WriteLine($\"Student name is: {student?.Name}\");\r\nConsole.WriteLine($\"First enrolled course is: {student?.Courses?[2]}\");<\/pre>\n<p>Here, we don&#8217;t need to do <code>if<\/code> checks as the <code>?.<\/code> and <code>?[]<\/code> operators will return <code>null<\/code> if the object, or its members, are <code>null<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">Student name is:\r\nFirst enrolled course is:<\/pre>\n<p>We don&#8217;t get the student&#8217;s name or first course but our code compiles and runs without throwing exceptions.<\/p>\n<p>The first thing to remember is that <strong>null-conditional operators are short-circuiting<\/strong>. This means that, if one operation in a series of member or element access operations returns <code>null<\/code>, the rest do not execute. In our example, we have <code>student?.Courses?[2]<\/code>, here if the <code>student<\/code> is <code>null<\/code>, we directly return <code>null<\/code>, without evaluating the rest of the expression.<\/p>\n<p>The second thing we must remember is that <strong>null-conditional operators save us only from null-reference exceptions<\/strong>. A null-conditional operator cannot save us from a situation where <code>student.Name<\/code> or <code>student.Courses<\/code> are not <code>null<\/code> but throw an <code>Exception<\/code>. In practice, this means that if <code>Courses<\/code> is not <code>null<\/code> but index<em> 2<\/em> is outside its bounds, an <code>IndexOutOfRangeException<\/code> will be thrown.<\/p>\n<h2>Null-Coalescing Operator (??) And (??=)<\/h2>\n<p>In some cases, not getting an <code>Exception<\/code> might do the trick. But we also have those cases where we need a default value to be returned:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var name = string.Empty;\r\n\r\nif (!string.IsNullOrWhiteSpace(student.Name))\r\n{\r\n    name = student.Name;\r\n}\r\nelse\r\n{\r\n    name = \"John\";\r\n}<\/pre>\n<p>We declare a <code>name<\/code> variable as an empty string. Then we check if the <code>student<\/code>&#8216;s <code>Name<\/code> is <code>null<\/code>, empty, or white space. If it is not we assign it to our name variable, else we assign <em>John<\/em> to it.<\/p>\n<p>Here comes the null-coalescing operator, <code>??<\/code>:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var name = student?.Name ?? \"John\";<\/code><\/p>\n<p>The initial ten lines of code are shortened to just one. The <code>??<\/code> operator will return the value on its left if it&#8217;s not <code>null<\/code>, otherwise, it returns the value on its right.<\/p>\n<p>In other cases, we might want to assign a value to a variable, or a property of an object, if it&#8217;s <code>null<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">Student namelessStudent = new Student(null, null);\r\n\r\nif (string.IsNullOrWhiteSpace(namelessStudent .Name))\r\n{\r\n    namelessStudent\u00a0.Name = \"John\";\r\n}<\/pre>\n<p>We check if the <code>namelessStudent<\/code>&#8216;s <code>Name<\/code> is <code>null<\/code>, empty, or white space. If it is we assign <em>John<\/em> to it.<\/p>\n<p>The null-coalescing assignment operator, <code>??=<\/code>, can improve our code:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">namelessStudent.Name ??= \"John\";<\/code><\/p>\n<p>Here, the <code>??=<\/code> operator will assign the value <em>John<\/em>, to <code>namelessStudent.Name<\/code> if it\u00a0is <code>null.<\/code><\/p>\n<h2>Null-Conditional Invocation Operator (?.())<\/h2>\n<p>Often our reference-type objects have methods that we need to invoke safely:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">if (student != null)\r\n{\r\n    student.Enroll(\"Math\");\r\n}<\/pre>\n<p>Here, before we <code>Enroll<\/code> our <code>student<\/code> in <em>Math<\/em> class, we make sure that <code>student<\/code> is not <code>null<\/code>.<\/p>\n<p>This type of check further bloats our code, but we can do something about it:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">student?.Enroll(\"Math\");<\/code><\/p>\n<p>Like with the safe member access operator, the null-conditional invocation operator, <code>?.()<\/code>, will only invoke the method if the object it belongs to is not <code>null<\/code>.<\/p>\n<h2>Thread Safety and Null-Conditional Operators<\/h2>\n<p>An essential concept in asynchronous programming is thread safety. Before we take a look at how null-conditional operators help with that, let&#8217;s expand our <code>Student<\/code> class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"3, 8, 20, 21, 22, 23, 24, 25, 26\">public class Student\r\n{\r\n    public const int MaxNumberOfCourses = 10;\r\n\r\n    public string? Name { get; set; }\r\n    public List&lt;string&gt; Courses { get; set; }\r\n\r\n    public event EventHandler MaxNumberOfCoursesReached;\r\n\r\n    public Student(string name, List&lt;string&gt; courses)\r\n    {\r\n        Name = name;\r\n        Courses = courses;\r\n    }\r\n\r\n    public void Enroll(string course)\r\n    {\r\n        Courses.Add(course);\r\n\r\n        if (Courses.Count &gt;= MaxNumberOfCourses)\r\n        {\r\n            if (MaxNumberOfCoursesReached != null)\r\n            {\r\n                MaxNumberOfCoursesReached.Invoke(this, EventArgs.Empty);\r\n            } \r\n        }\r\n    }       \r\n}<\/pre>\n<p>We add a <code>MaxNumberOfCourses<\/code> constant and set it to 10 and an <code>EventHandler<\/code> called <code>MaxNumberOfCoursesReached<\/code>. Then we expand the <code>Enroll<\/code> method to invoke the event if the number of <code>Courses<\/code> reaches the limit and the <code>MaxNumberOfCoursesReached<\/code> is not <code>null<\/code>.<\/p>\n<p>This is considered a thread-safe way of invocation due to the fact that if a different thread unsubscribes from our event and <code>MaxNumberOfCoursesReached<\/code> becomes <code>null<\/code>, it won&#8217;t be invoked and our object will remain unaffected.<\/p>\n<p>Now, let&#8217;s utilize the null-conditional member access operator:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"7\">public void Enroll(string course)\r\n{\r\n    Courses.Add(course);\r\n\r\n    if (Courses.Count &gt;= MaxNumberOfCourses)\r\n    {\r\n        MaxNumberOfCoursesReached?.Invoke(this, EventArgs.Empty);\r\n    }\r\n}<\/pre>\n<p>Here, we remove the <code>if<\/code> statement and use <code>?.<\/code> before invoking <code>MaxNumberOfCoursesReached<\/code> &#8211; this reduces the code length but has the same result.<\/p>\n<h2>Advantages of Using Null-Conditional Operators<\/h2>\n<p>Every new feature in C# is there for a good reason, and the null-conditional operators make no difference. They make our code more concise and less exception-prone, especially when we deal with complex object structures that may contain <code>null<\/code> values.<\/p>\n<h3>Avoiding Null-Reference Exceptions<\/h3>\n<p>The most valuable advantage of using the null-conditional operators is making our application less prone to throwing null-reference exceptions. The return of <code>null<\/code> values may still be a nuisance but it is better than having our program crash. Moreover, if <code>null<\/code> values are handled appropriately, the end-user won&#8217;t even know that something unexpected has happened.<\/p>\n<h3>Simplifying Code and Better Code Readability<\/h3>\n<p>Simplifying our code has the advantage of improving its readability but also makes it easier for others to understand. This is very important as we often work in a team environment and understanding each other&#8217;s code is vital.<\/p>\n<h2>Disadvantages of Using Null-Conditional Operators<\/h2>\n<p>In this section, we will explore the potential disadvantages of null-conditional operators. By understanding the limitations and risks associated with them, we can decide when to leverage null-conditional operators and when to use alternative approaches for handling <code>null<\/code> values.<\/p>\n<h3>Decreasing Readability<\/h3>\n<p>Using null-conditional operators can help us reduce the amount of boilerplate code we use to handle <code>null<\/code> values. On the other hand, overusing them can make our code harder to read and understand, especially for developers who are not familiar with this feature of C#. This can lead to more time spent deciphering code and an increased probability of introducing errors.<\/p>\n<h3>Harder Debugging<\/h3>\n<p>Debugging code that heavily relies on null-conditional operators can be more challenging. It can be hard to determine which operator, or expression, is causing problems. Also, if a null-conditional operator is used improperly, it can lead to unexpected behavior that may be difficult to find and fix.<\/p>\n<h3>Not Present in Older Versions of C#<\/h3>\n<p>Null-conditional operators were introduced in C# 6.0, which means they are not an option for developers working with legacy code, or targeting older versions of the .NET Framework. This removes the possibility for some developers and teams to take advantage of those operators.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, we learned about the different null-conditional operators in C#. Now, we know how and when to use them to prevent null-reference exceptions and improve our code readability. We have also learned about the potential disadvantages and how to avoid them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will discuss a feature of C# known as null-conditional operators and how it can help prevent the occurrence of null-reference exceptions, which are commonly encountered by developers. Let&#8217;s dive in! What Are Null-Conditional Operators in C# and Why Are They Important? Null-conditional operators were introduced in C# 6.0 to provide a [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":62189,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[12],"tags":[10,1489,1490,341,220],"class_list":["post-84953","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","tag-net","tag-null-coalescence","tag-null-coallescing-operator","tag-nullable-types","tag-operators","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Null-Conditional Operators in C# - Code Maze<\/title>\n<meta name=\"description\" content=\"In this article, we will look at a feature of C# called null-conditional operators and how they can save us from exceptions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/csharp-null-conditional-operators\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Null-Conditional Operators in C# - Code Maze\" \/>\n<meta property=\"og:description\" content=\"In this article, we will look at a feature of C# called null-conditional operators and how they can save us from exceptions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/csharp-null-conditional-operators\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-01T06:00:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-01T08:15:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Code Maze\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Code Maze\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/csharp-null-conditional-operators\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/csharp-null-conditional-operators\/\"},\"author\":{\"name\":\"Code Maze\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\"},\"headline\":\"Null-Conditional Operators in C#\",\"datePublished\":\"2023-04-01T06:00:59+00:00\",\"dateModified\":\"2023-04-01T08:15:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-null-conditional-operators\/\"},\"wordCount\":1135,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-null-conditional-operators\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"keywords\":[\".NET\",\"null-coalescence\",\"null-coallescing operator\",\"nullable types\",\"Operators\"],\"articleSection\":[\"C#\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/csharp-null-conditional-operators\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/csharp-null-conditional-operators\/\",\"url\":\"https:\/\/code-maze.com\/csharp-null-conditional-operators\/\",\"name\":\"Null-Conditional Operators in C# - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-null-conditional-operators\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-null-conditional-operators\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"datePublished\":\"2023-04-01T06:00:59+00:00\",\"dateModified\":\"2023-04-01T08:15:20+00:00\",\"description\":\"In this article, we will look at a feature of C# called null-conditional operators and how they can save us from exceptions.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/csharp-null-conditional-operators\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/csharp-null-conditional-operators\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/csharp-null-conditional-operators\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"width\":1100,\"height\":620,\"caption\":\"C# Development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/csharp-null-conditional-operators\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Null-Conditional Operators in C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\",\"name\":\"Code Maze\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"caption\":\"Code Maze\"},\"description\":\"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/company\/codemaze\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog\"],\"url\":\"https:\/\/code-maze.com\/author\/codemazecontributor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Null-Conditional Operators in C# - Code Maze","description":"In this article, we will look at a feature of C# called null-conditional operators and how they can save us from exceptions.","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:\/\/code-maze.com\/csharp-null-conditional-operators\/","og_locale":"en_US","og_type":"article","og_title":"Null-Conditional Operators in C# - Code Maze","og_description":"In this article, we will look at a feature of C# called null-conditional operators and how they can save us from exceptions.","og_url":"https:\/\/code-maze.com\/csharp-null-conditional-operators\/","og_site_name":"Code Maze","article_published_time":"2023-04-01T06:00:59+00:00","article_modified_time":"2023-04-01T08:15:20+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","type":"image\/png"}],"author":"Code Maze","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Code Maze","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/csharp-null-conditional-operators\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/csharp-null-conditional-operators\/"},"author":{"name":"Code Maze","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04"},"headline":"Null-Conditional Operators in C#","datePublished":"2023-04-01T06:00:59+00:00","dateModified":"2023-04-01T08:15:20+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/csharp-null-conditional-operators\/"},"wordCount":1135,"commentCount":0,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/csharp-null-conditional-operators\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","keywords":[".NET","null-coalescence","null-coallescing operator","nullable types","Operators"],"articleSection":["C#"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/csharp-null-conditional-operators\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/csharp-null-conditional-operators\/","url":"https:\/\/code-maze.com\/csharp-null-conditional-operators\/","name":"Null-Conditional Operators in C# - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/csharp-null-conditional-operators\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/csharp-null-conditional-operators\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","datePublished":"2023-04-01T06:00:59+00:00","dateModified":"2023-04-01T08:15:20+00:00","description":"In this article, we will look at a feature of C# called null-conditional operators and how they can save us from exceptions.","breadcrumb":{"@id":"https:\/\/code-maze.com\/csharp-null-conditional-operators\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/csharp-null-conditional-operators\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/csharp-null-conditional-operators\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","width":1100,"height":620,"caption":"C# Development"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/csharp-null-conditional-operators\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"Null-Conditional Operators in C#"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04","name":"Code Maze","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","caption":"Code Maze"},"description":"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.","sameAs":["https:\/\/www.linkedin.com\/company\/codemaze\/","https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog"],"url":"https:\/\/code-maze.com\/author\/codemazecontributor\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/84953","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=84953"}],"version-history":[{"count":1,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/84953\/revisions"}],"predecessor-version":[{"id":84954,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/84953\/revisions\/84954"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62189"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=84953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=84953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=84953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}