#268 – You Must Set the Value of All out Parameters Before Returning from a Method

You can use out parameters as a way of returning multiple data values from a method to its caller.  Because the intention of an output parameter is to return data to the caller, you must assign some value to every parameter marked as out before you can return from a method.  The compiler will enforce this rule.

In the example below, we’ve failed to set a value for the barkPhrase output parameter.

        public void GetDogVitals(out string fullName, out int age, out string barkPhrase)
        {
            fullName = string.Format("{0}, {1}", Name, Title);
            age = Age;
            //barkPhrase = BarkPhrase;
        }

When we try to compile the code, we’ll get an error telling us that we must assign a value to the barkPhrase output parameter before control leaves the method.

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: