#588 – A Default Parameter Value Can Be Null

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);
        }

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: