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

Advertisements