{C# 6.0 and VS 2015 Quick Tip} Coding Superpowers for CRM developers – Part 2

Sometimes there is a long gap between Part 1 and Part 2 of my blog series.

But its just because I want to use the concepts in my projects or POC’s before sharing them.

image

The Part 1 of this series is here: Click here

Part 2 Goes here, the new features which can benefit you!

The new null conditional operator {?.}:

You can do multiple layer null check in one condition, the below snippet checks EntityCollection is not null

and then checks Entities is not null:

  EntityCollection entityCollection = service.RetrieveMultiple(query);

 

                            if (entityCollection?.Entities != null)

                            {

                                // Processing logic

                            }

 

What’s more you can also do null check and also match conditions , the below snippet checks EntityCollection is not null,

then checks Entities is not null and also matches entity count condition:

EntityCollection entityCollection = service.RetrieveMultiple(query);

 

                            if (entityCollection?.Entities?.Count > 0)

                            {

                                // Processing logic

                            }

 

Nameof Operator:

This operator helps you to get the name of a particular type and can be used for handling in exceptions and tracing:

For example:

var entity = service.Retrieve(“account “, new Guid(“B18DFC86-4776-E511-80FC-3863BB343C90”), new ColumnSet(true));

 

                    Console.WriteLine(“Var in entity is of type: “ + nameof(entity));

                    Console.WriteLine(“Attribute collection in entity is of type: “ + nameof(entity.Attributes));

Gives the output:

clip_image001

Exception filters:

In your CRM exception handling you can filter by messages now:

  try

                    {

                        throw new Exception(“CRM Exception”);

                    }

                    catch (InvalidPluginExecutionException ex) when (ex.Message == “CRM Exception”)

                    {

                        //control will not come in this catch block

                    }

                    catch (Exception ex) when (ex.Message == “CRM Exception”)

                    {

                        //control will  come in this catch block

                    }

Hope it helps and Happy CRMing!

Advertisements

One thought on “{C# 6.0 and VS 2015 Quick Tip} Coding Superpowers for CRM developers – Part 2

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