Public & Private Members in Interfaces

Problem
You want to know if it’s bad practice to use private members in interfaces.

Solution
Even though there are several exceptions to the rule, you should prefer public interface members over private ones.

Comments
One reason to implement private interface members is when you don’t want types to contain many seldom-used public methods. A perfect example of this is numeric types in .NET implementing the IConvertible interface as private methods. Another exception is if you want to supply a public method that does the same thing as a private one but in a strongly-typed way.

Example

public class Account : ICloneable, IComparable {
    // ICloneable is a public implementation.
    public object Clone() {
        ...
    }

    // IComparable is a private implementation.
    int IComparable.CompareTo(object obj) {
        ...
    }
}

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s