- by gowthamk91
Introduction:
Null parameter check is one my favorite feature in C# 10. It’s seeming to be trivial but it’s a valuable language optimization One of the common bugs you can have in your code is Null reference exception. In older days we use to check the null for the object using an assignment operator like obj==null, with C#10 it was more simplified.
Parameter Null check:
Before C#10 we used to do parameter null check something like this
Employee employee = new Employee();
employee.AddEmployee(null);
public class Employee
{
public int Id { get; set; }
public string? Name { get; set; }
public void AddEmployee(Employee employee)
{
List<Employee> list = new List<Employee>();
if (employee == null)
{
throw new ArgumentNullException();
}
employee.Name = Name;
employee.Id = Id;
list.Add(employee);
}
}
With C# 10 just by adding !! at the end of the parameter will do the null check automatically
public void AddEmployee(Employee employee!!)
{
List<Employee> list = new List<Employee>();
employee.Name = Name;
employee.Id = Id;
list.Add(employee);
}
Conclusion:
From this blog you have learned now parameter null check has been simplified with C# 10. We will see more about C# 10 new features in my future blogs