Skip to main content

Null Conditional Operators (?.) in C# 6.0 (New Feature)

Here I am discussing about the new feature called “null-conditional operators” introduced in C# 6.0 (VS 2015 and .Net 4.6).

First we can see the old way,

InvoiceDetails = Invoice != null ?  Invoice.InvoiceDetails : null;

If(Invoice != null && Invoice.InvoiceDetails != null)
{
     Amount = Invoice.InvoiceDetails.Amount.;
}
else
{
    Amount = 0;
}

Or 

Amount = Invoice != null ? (Invoice.InvoiceDetails != null ? Invoice.InvoiceDetails.Amount : 0) : 0;
    
In the above code used null conditional operator, nested if statements etc. In C# 6.0 provides the new way to check for Null. We can use Null-conditional operator (“?.”) for reduce number of lines of codes.

InvoiceDetails = Invoice?.InvoiceDetails;

If all of the conditions are met, this will return the actual value; otherwise, it will return null. It checks the InvoiceDetails object and returns a value if Invoice is not equal to null; else, it returns null.

Amount = Invoice?.InvoiceDetails?.Amount;

In this case either Invoice or Invoice details is null then returns null value.

Amount = Invoice?.InvoiceDetails?.Amount ?? 0;

Here we can return if Amount is null then 0 using "??" operator.

Advantages
By lowering the number of lines of code, developers may work more efficiently. Additionally, by keeping the code clean, it may help lower the potential number of problems in the code.


Comments

Popular posts from this blog

OOP Concept with Real Time Examples

A design philosophy is OOP. Object Oriented Programming is what it stands for. In contrast to outdated procedural programming languages, object-oriented programming (OOP) employs a separate set of programming languages. In OOP, everything is categorised as self-sustaining "objects". As a result, you achieve re-usability using the four core concepts of object-oriented programming. Programmes are organised around objects and data rather than action and logic in the object-oriented programming (OOP) paradigm.    Let's use your "Leg" as an example to grasp the object orientation clearly. The class "Leg" is one. Left and right legs are objects of type Leg on your body. A series of electrical impulses supplied through your body parts (through an interface) are what manage or control their primary functions. As a result, the body part serves as an interface between your body and your legs. The Leg is a well-designed class. The attributes of the Leg are m...

Windows Application Development - Dotnet Environment Basic understandings

The development life cycle for creating Windows desktop applications using the .NET framework typically involves several stages. Here's an overview of the typical life cycle: Requirement Analysis: Gather and analyze the requirements for the Windows application. Understand the business needs, user expectations, features, and functionalities that the application should have. Design: Create a design for your application's user interface (UI) and overall architecture. Decide on the layout, controls, navigation flow, and other visual aspects. Plan the data storage mechanisms, database schema, and integration with other systems if necessary. Development: Begin coding the application using the .NET framework. Use programming languages like C# or VB.NET. Create classes, forms, controls, and implement the business logic. You'll work on creating the UI, handling user interactions, data processing, and any required integrations. Testing: Thoroughly test the applicatio...

Accepting both viewpoints is necessary for a meaningful and satisfied existence

Life is precious and brimming with chances for development, adoration, and joy. A journey should be embraced and treasured. In addition to being difficult and unpredictable, life may also bring about trials and tragedies that help us learn important lessons and develop as people.  This tale depicts a character who has little regard for either his own or other people's lives. Call him Sandy if you must. He is wealthy and runs several family businesses. son who is single and has two sisters. married, from a stable family, with two children. They remain with their parents. Sandy managed to complete his degree and depart with buddies. He developed a drug and alcohol addiction at a young age. He makes the most of the affluence in his family to enjoy life. His father is well-known in society and is recognized for being very humble. But Sandy is really illogical to society. His sisters have advanced degrees; one is married and the other is pursuing an MBBS. His wife began her professional...