#595 – Intellisense Shows Method Overloads

When you overload a method, you’re creating multiple methods with the same name, but a different number of parameters, or parameters of different types.  At compile-time, the compiler will automatically call the correct method.

Visual Studio can help with entering parameter values.  When you enter the name of a method, Intellisense shows you a list of available methods with that name.

Let’s say that we have three different versions of a Dog.Bark method.  After typing “Bark” and the opening parenthesis “(“, Intellisense will indicate that there are three versions of the method available and provide information about the first version.

At this point, you can press the up/down arrow keys to cycle through the three different methods.

Notice that Intellisense does the following:

  • Indicates in bold the next parameter that you should enter
  • Lists a description of the method, if one is available
  • Provides a description of the next parameter
Advertisements

#594 – When You’d Want to Use Named Arguments

You can change the  order of arguments passed to a method in C# by using named arguments.  Simply changing the order of required arguments probably doesn’t make much sense.  Instead, you’ll likely make use of named arguments as a way of omitting optional parameters.

Without named arguments, when you want to include a value for an optional parameter, you normally have to include values for all arguments up to and including the one that you want to specify a value for.  But with named arguments, you can pick and choose which parameters you provide a value for.

Here’s a Dog.Bark method that has several optional parameters.

public void Bark(string barkSound, int numTimesToBark = 1, int volume = 1, string postBarkSound = null)

When calling Bark, we can choose which arguments to pass.

kirby.Bark("Woof", volume: 5);
kirby.Bark("Grrr", postBarkSound: "Arf");
kirby.Bark("Woof", 3, postBarkSound: "Growf");

#593 – Using Named Arguments

Traditionally in C#, arguments must be passed in to a method in the same order in which they are defined in the method’s declaration.

public void DoATrick(string trickName, int numTimes)
            Dog kirby = new Dog("Kirby");

            kirby.DoATrick("Fetch", 158);

You can, however, change the order of the arguments by preceding each value by the name of the parameter, with a colon (:) following each name.

            kirby.DoATrick(numTimes: 5, trickName: "Run around me");

#592 – Optional Parameters in Indexers

In addition to defining optional parameters within methods, you can also include an optional parameter in an indexer.

For example, assume that we have a Dog class that keeps track of the sounds that a dog makes when he barks and that we’ve defined an indexer for the class that retrieves a “bark sound” from that list.

        public string this[int i]
        {
            get
            {
                return BarkRecord[i];
            }
        }

Using the indexer:

            kirby.Bark("Woof");
            kirby.Bark("Bow-wow");
            kirby.Bark("Urgh");

            Console.WriteLine(kirby[2]);


We can now add an optional parameter to the indexer, to indicate that we’d like the date/time of the bark returned as well.

        public string this[int i, bool includeTime = false]
        {
            get
            {
                string barkInfo = BarkRecord[i];

                if (includeTime)
                    barkInfo = barkInfo + string.Format(" at {0}", BarkTime[i]);

                return barkInfo;
            }
        }

Using the indexer:

            Console.WriteLine("Bark at [2]={0}", kirby[2]);
            Console.WriteLine("Bark at [1]={0}", kirby[1, true]);

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