Null Conditional Operator in C# 6

During my code session in the weekend, I discovered the Null Conditional operator in C# 6. In terms of CRM custom development, this operator reduces the risk of null reference exception. Let me give you an example.

Before

                var contactFetchQuery = @"
                <fetch version=""1.0"" output-format=""xml-platform"" mapping=""logical"" distinct=""true"">
                  <entity name=""contact"">
                    <attribute name=""contactid""/>
                    <attribute name=""birthdate""/>
                    <attribute name=""fullname""/>
                      <filter type=""and"">
		                <condition attribute=""statecode"" operator=""eq"" value=""0"" />
                      </filter>
                  </entity>
                </fetch>";
                var contactRecords = crmSvc.OrganizationServiceProxy.RetrieveMultiple(new FetchExpression(contactFetchQuery)).Entities;
                foreach (var contactRecord in contactRecords)
                {
                    var birthDate = contactRecord.GetAttributeValue<DateTime?>("birthdate");
                    if (birthDate.HasValue)
                    {
                        if (birthDate.Value.ToLocalTime().Day == DateTime.Today.Day && birthDate.Value.ToLocalTime().Month == DateTime.Today.Month)
                        {
                            Console.WriteLine("{0} was born today..", contactRecord.GetAttributeValue<string>("fullname"));
                        }
                    }
                }

After

                var contactFetchQuery = @"
                <fetch version=""1.0"" output-format=""xml-platform"" mapping=""logical"" distinct=""true"">
                  <entity name=""contact"">
                    <attribute name=""contactid""/>
                    <attribute name=""birthdate""/>
                    <attribute name=""fullname""/>
                      <filter type=""and"">
		                <condition attribute=""statecode"" operator=""eq"" value=""0"" />
                      </filter>
                  </entity>
                </fetch>";
                var contactRecords = crmSvc.OrganizationServiceProxy.RetrieveMultiple(new FetchExpression(contactFetchQuery)).Entities;
                foreach (var contactRecord in contactRecords)
                {
                    var birthDate = contactRecord.GetAttributeValue<DateTime?>("birthdate");
                    if (birthDate?.ToLocalTime().Day == DateTime.Today.Day && birthDate?.ToLocalTime().Month == DateTime.Today.Month)
                    {
                        Console.WriteLine("{0} was born today..", contactRecord.GetAttributeValue<string>("fullname"));
                    }
                }

This makes null checks almost redundant, as the part after the “?.” would not be even evaluated, if the part before “?.” is null. I compiled a project using .net 4.5.2 and VS2015 for the XrmToolBox project and I like using this new functionality. I will definitely use this in future applications.

Refer https://msdn.microsoft.com/en-us/library/dn986595.aspx for more information.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s