#591 – How Optional Parameters Look with Intellisense

When you’re working in Visual Studio and you start typing in the name of a method, Intellisense will show you any optional parameters that the method has.

In the example below, we’ve started entering code to create a new instance of a Dog object.  Intellisense shows us a list of the optional parameters that are defined for the constructor.

#590 – Optional Parameters in Constructors

You’ll typically see optional parameters in methods.  But you can also define an optional parameter in a constructor:

        public string Name { get; set; }
        public int Age { get; set; }
        public string FavoriteToy { get; set; }
        public Dog Father { get; set; }
        public Dog Mother { get; set; }

        public Dog(string name, int age = 1, string favToy = "Bone",
            Dog father = null, Dog mother = null)
        {
            Name = name;
            Age = age;
            FavoriteToy = favToy;
            Father = father;
            Mother = mother;
        }

 

            Dog kirby = new Dog("Kirby", 15, "Ball");
            Dog sonOfKirby = new Dog("Ferbie", 2, "Frisbee", kirby);


Optional parameters in constructors avoid the need to chain constructors.

 

#589 – Optional Parameters Must Be Input Parameters

An optional parameter must be an input parameter.  It can’t therefore be modified by the out or ref keywords.  An optional parameter also cannot be modified by the params keyword.

        // OK: numTimesToRead is an input parameter (passed by value)
        static void ReadBook(string title, int numTimesToRead = 1)
        {
        }

        // NO: out parameter can't have default value
        static void ReadBook2(string title, out int yourRating = 3)
        {
            yourRating = 4;
        }

        // NO: ref parameter can't have default value
        static void ReadBook3(string title, ref string yourOpinion = "Unknown")
        {
        }

        // NO: Parameter array can't have default value
        static void ReadBook4(string title, params string[] characters = null)
        {
        }


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

#587 – If Provided, Optional Arguments Must Be in Correct Order

When you choose to include arguments for optional parameters on a method, you must specify the arguments in the proper order (just like required parameters).

In the example below, we define a method with one required parameter and three optional parameters.  When we call it, we must provide a value for the yourName parameter.  Then we can provide values for one, two or all three of the optional parameters, in the following combinations:

  • book
  • book, play
  • book, play, poem

        static void Favorites(
            string yourName,
            string book = "Moby Dick",
            string play = "Henry V",
            string poem = "The Road Not Taken")
        {
            Console.WriteLine("{0}'s favorites:", yourName);
            Console.WriteLine("  Book: {0}", book);
            Console.WriteLine("  Play: {0}", play);
            Console.WriteLine("  Poem: {0}", poem);
        }

        static void Main()
        {
            Favorites("Sean");
            Favorites("Sergei", "Anna Karenina");
            Favorites("Pablo", "Don Quixote", "Canción de cuna");
            Favorites("Nigel", "David Copperfield", "Hamlet", "Ode to Duty");
        }

#586 – Default Values for Optional Parameters Must Be Constants

When you specify a default value for an optional parameter, that value must be a constant, of the same type as the parameter.  The expression must be able to be evaluted at compile-time.  The parameter’s default value can be one of the following:

  • A constant expression (see below)
  • new S(),  where S is a value-type (parameters not allowed on constructor)

A constant expression is one that can be fully determined at compile-time.  In other words, you can use any expression that you’d use in the initialization of a constant.

For reference types, the expression must be null, with the exception of the string type.

        private const int OFFSET = 1;

        static void Optional1(int x = 5 + OFFSET, double y = 1.0/3.0) { }
        static void Optional2(Point3D fln = new Point3D()) { }
        static void Optional3(Dog d = null) { }
        static void Optional4(string s = "DEFAULT") { }

        static void Main()
        {
            Optional1();
            Optional2();
            Optional3();
        }

#585 – Optional Parameters Must Come Last

If you define a method that includes optional parameters, they must come after any required parameters.  This means that your options for required vs. optional parameters are:

  • No parameters at all
  • 1 or more required parameters (no default values)
  • 1 or more optional parameters (with default values)
  • 1 or more required parameters, followed by 1 or more optional parameters

        static void NoParams() { }
        static void RequiredOnly(int x, int y) { }
        static void OptionalOnly(int x = 5, int y = 10) { }
        static void RequiredAndOptional(int x, int y, int a = 1, int b = 2) { }

        static void Main()
        {
            NoParams();
            RequiredOnly(5, 10);

            OptionalOnly();
            OptionalOnly(1);
            OptionalOnly(1, 2);

            RequiredAndOptional(1, 1);
            RequiredAndOptional(1, 2, 3);
            RequiredAndOptional(1, 2, 3, 4);
        }

#584 – Defining an Optional Parameter

When you define a method in C#, you can define one or more of the method’s parameters as optional.  An optional parameter is one that has a default value, which allows a calling function to choose whether or not it wants to pass in a value for that parameter.  Any parameters that are omitted by the caller will take on the specified default value.

Here’s the Bark method of a Dog object, which supplies a default value for the numTimesToBark parameter, making it optional.

        public void Bark(string barkSound, int numTimesToBark = 1)
        {
            for (int i = 0; i < numTimesToBark; i++)
                Console.WriteLine(barkSound);
        }

When calling Dog.Bark, the caller can pass in a value for numTimesToBark, or leave off this argument so that the parameter uses the default value (1).

// Pass in both barkSound and numTimesToBark
myDog.Bark("Bow-wow", 4);

// Pass in only barkSound
myDog.Bark("Woof");

#583 – You Can’t Modify the Iterator Variable Within a foreach Loop

The iterator variable within a foreach loop is the variable that takes on the value of each item within the corresponding collection, each time through the loop.

            string[] puppets =
                {"Crow T. Robot", "Howdy Doody", "Kermit",
                 "King Friday XIII", "Lamb Chop"};

            // nextPuppet is the iterator variable
            foreach (string nextPuppet in puppets)
            {
                Console.WriteLine("Puppet: {0}", nextPuppet);
            }

If you try to modify this iterator variable within the loop, however, you’ll get a compile-time error.

foreach (string nextPuppet in puppets)
{
    nextPuppet = "(" + nextPuppet + ")";
    Console.WriteLine("Puppet: {0}", nextPuppet);
}

#582 – Use the as Operator to Unbox to a Nullable Type

You can box regular value types or their equivalent nullable types (e.g. int and int?) and the boxed values will either be null or be of the underlying value type.

You can unbox these values to a nullable type, or use the as operator to do the unboxing.  The example below shows the result of unboxing several different values to a nullable int (int?) using the as operator.

int? i1 = null;   // Nullable<int> w/no value
int? i2 = 42;     // Nullable<int> with a value
int i3 = 12;      // Plain old int

// Boxing nullable types
object o1 = i1;
object o2 = i2;
object o3 = i3;
object o4 = new Dog("I'm not an int", 12);

// Unboxing to nullable types
int? ia1 = o1 as int?;    // null
int? ia2 = o2 as int?;    // 42
int? ia3 = o3 as int?;    // 12
int? ia4 = o4 as int?;    // null

bool bHasVal = ia1.HasValue;  // false
bHasVal = ia2.HasValue;       // true
bHasVal = ia3.HasValue;       // true
bHasVal = ia4.HasValue;       // false

Follow

Get every new post delivered to your Inbox.

Join 43 other followers