//Check if an object is null before calling a function using the ? operator.
//The ? operator is syntactic suger for the if block below:
//Using the ? operator
myObject?.MyFunc();
//Using an if block.
if (myObject != null)
{
myObject.MyFunc();
}
// As of C# 10 (.NET 6)
ArgumentNullException.ThrowIfNull(name);
public static int CountNumberOfSInName(string name)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
return name.Count(c => char.ToLower(c).Equals('s'));
}
if (name is null)
{
throw new ArgumentNullException(nameof(name));
}