In the above Statement if emp?.EmpAddress is not null then it will access HomeAddress else it will Show “Nothing “ in EmpAddress.
Tuesday, 7 November 2017
Null-Conditional Operator in C# 6.0
in: asp csharp
Null-Conditional Operator is a new Concept in C# 6.0 that is useful when we want to check the null condition of an object or reference data type. We can write an in-line null-conditional with the ? and ?? operators.
Syntax:
Condition ? if true ?? if false
Example:-
namespace ConsoleApplication2
{
class Program
{
class Employee
{
public string Name { get; set; }
public Address EmpAddress { get; set; }
}
class Address
{
public string HomeAddress { get; set; }
public string OfficeAddress { get; set; }
}
static void Main(string[] args)
{
Employee emp = new Employee();
emp.Name = "Max";
emp.EmpAddress = new Address()
{
HomeAddress = "Street",
OfficeAddress = "Street1"
};
Console.WriteLine(emp?.Name);
Console.WriteLine((emp?.EmpAddress ?.HomeAddress ?? "Nothing"));
Console.ReadLine();
}
}
}
In the above example I created 2 class and which contain Some of the Properties and in the main method we assign the value to those properties. However, in this Statement,
Console.WriteLine(emp?.Name);
It will show Employee Name if it is not null.
Console.WriteLine((emp?.EmpAddress ?.HomeAddress ?? "Nothing"));
In the above Statement if emp?.EmpAddress is not null then it will access HomeAddress else it will Show “Nothing “ in EmpAddress.
In the above Statement if emp?.EmpAddress is not null then it will access HomeAddress else it will Show “Nothing “ in EmpAddress.
RELATED POSTS

string.IsNullOrEmpty in C#.
In this article, We will discuss How to use string.IsNullOrEmpty efficie ...

Why Switch not Use Float and Double Value in C#.
In this article, We will discuss about Why Switch not Use Float and Doub ...

Auto Property Initializer in C# 6.0
In this article, We will discuss about How to Use Auto Property Initial ...
Get All Location Details Using IPAddress in C#.
This article will tell you about how to retrieve User Locations by passin ...
-
This issue is Completely related to EntityFramework and this issue arises whenever you have installed EntityFramework in one project at th...
-
This is generally a Common Problem or the error could be like this, To Resolve this error, One Small Solution is Exists which is as b...
-
In this article, We will discuss What is the above Error and How to resolve this Error. Whenever, We are Restoring our database into A...
0 comments:
Post a Comment