How can I make a method private in an interface?

Viewed 77932

I have this interface:

public interface IValidationCRUD
{
    public ICRUDValidation IsValid(object obj);
    private void AddError(ICRUDError error);
}

But when I use it (Implement Interface, automatic generation of code), I get this:

public class LanguageVAL : IValidationCRUD
{   
    public ICRUDValidation IsValid(object obj)
    {
        throw new System.NotImplementedException();
    }

    public void AddError(ICRUDError error)
    {
        throw new System.NotImplementedException();
    }   
}

The method AddError is public and not private as I wanted.

How can I change this?

7 Answers

Since c# 8.0 you can define private methods in interfaces. Here is the link to the docs

Since private interface members won't be accessible in an implementing class you'll have to provide a default implementation for such member, otherwise such code won't compile.

A real use case for such method may be a refactoring of another default method in an interface.

Related