When defining optional parameters and providing a default value for the parameter, you can use a value of null for a reference-typed parameter. null is actually the only valid default value that you can use for a reference-typed parameter.
static void LogDogInfo(Dog myDog, Dog anotherDog = null) { Console.WriteLine("My dog is {0}", myDog.Name); if (anotherDog != null) Console.WriteLine(" And there is also {0}", anotherDog.Name); } static void Main() { Dog dog1 = new Dog("Kirby", 15); Dog dog2 = new Dog("Jack", 17); LogDogInfo(dog1); LogDogInfo(dog2, dog1); }
Filed under: Methods Tagged: C#, Methods, null, Optional Parameters
