{"id":802,"date":"2020-04-28T20:49:20","date_gmt":"2020-04-28T20:49:20","guid":{"rendered":"https:\/\/kalkus.dev\/?p=802"},"modified":"2023-08-24T23:32:20","modified_gmt":"2023-08-24T23:32:20","slug":"what-does-question-mark-and-dot-operator-mean-in-c-6-0","status":"publish","type":"post","link":"https:\/\/kalkus.dev\/blog\/2020\/04\/28\/what-does-question-mark-and-dot-operator-mean-in-c-6-0\/","title":{"rendered":"What does question mark and dot operator ?. mean in C# 6.0?"},"content":{"rendered":"\n<p>C# 6.0 introduced new operator \u2013 null conditional operator ?. <\/p>\n\n\n\n<p>The way it works is very simple. It checks if the left part of the expression is null and if it\u2019s not it returns the right part. Otherwise, it returns null.<\/p>\n\n\n\n<h4>Simple example:<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var thisWillBeNulll = someObject.NullProperty?.NestedProperty;\nvar thisWilllBeNestedProperty = someObject.NotNullProperty?.NestedProperty;\n<\/pre>\n\n\n\n<h4>Executable example:<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/Rextester.Program.Main is the entry point for your code. Don't change it.\n\/\/Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5\n\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text.RegularExpressions;\n\nnamespace Rextester\n{\n    public class Person\n    {\n        public string Name { get; set; }\n    }\n\t\n    public class Dog \n    {\n        public Person Owner { get; set; }\n    } \n    \n    public class Program\n    {\n        public static void Main(string[] args)\n        {\n            Dog dog = new Dog();\n            Console.WriteLine(dog.Owner?.Name == null);\n            \/\/ this will print True\n            dog.Owner = new Person() { Name = \"Fred\" };\n            Console.WriteLine(dog.Owner?.Name); \n            \/\/ this will print Fred\n        }\n    }\n}<\/pre>\n\n\n\n<p>The above example can be executed here: <a href=\"https:\/\/rextester.com\/TBC19437\">https:\/\/rextester.com\/TBC19437<\/a><\/p>\n\n\n\n<p>Documentation: <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/language-reference\/operators\/member-access-operators#null-conditional-operators--and-\">https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/language-reference\/operators\/member-access-operators#null-conditional-operators&#8211;and-<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>C# 6.0 introduced new operator \u2013 null conditional operator ?. The way it works is very simple. It checks if the left part of the expression is null and if it\u2019s not it returns the right part. Otherwise, it returns null. Simple example: Executable example: The above example can be executed here: https:\/\/rextester.com\/TBC19437 Documentation: https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/language-reference\/operators\/member-access-operators#null-conditional-operators&#8211;and-<\/p>\n","protected":false},"author":1,"featured_media":823,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[202,76,1],"tags":[123,124,125],"_links":{"self":[{"href":"https:\/\/kalkus.dev\/blog\/wp-json\/wp\/v2\/posts\/802"}],"collection":[{"href":"https:\/\/kalkus.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kalkus.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kalkus.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kalkus.dev\/blog\/wp-json\/wp\/v2\/comments?post=802"}],"version-history":[{"count":7,"href":"https:\/\/kalkus.dev\/blog\/wp-json\/wp\/v2\/posts\/802\/revisions"}],"predecessor-version":[{"id":820,"href":"https:\/\/kalkus.dev\/blog\/wp-json\/wp\/v2\/posts\/802\/revisions\/820"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kalkus.dev\/blog\/wp-json\/wp\/v2\/media\/823"}],"wp:attachment":[{"href":"https:\/\/kalkus.dev\/blog\/wp-json\/wp\/v2\/media?parent=802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kalkus.dev\/blog\/wp-json\/wp\/v2\/categories?post=802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kalkus.dev\/blog\/wp-json\/wp\/v2\/tags?post=802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}