#274 – Can’t Overload if Methods Differ Only by ref and out Modifiers

You can overload a method in a class, i.e. define two methods with the same name, if the methods have different lists of parameters.  The parameter lists must differ in the number of parameters or in their types.

For example, we can overload the Dog.Bark method if the parameters differ only in type:

        public void Bark(int numBarks)
        {
            // implementation here
        }

        public void Bark(short numBarks)
        {
            // implementation here
        }

We cannot overload a method if the parameters differ only in a ref vs out modifiers:

        public void FindDog(ref Dog aDog)
        {
            // implementation here
        }

        // Compiler will disallow this overload
        public void FindDog(out Dog aDog)
        {
            // implementation here
        }

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #274 – Can’t Overload if Methods Differ Only by ref and out Modifiers

  1. Pingback: #595 – Intellisense Shows Method Overloads « 2,000 Things You Should Know About C#

Leave a comment