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

Advertisement

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: