How can I make a method private in an interface?

C#Interface

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

An interface can only have public methods. You might consider using an abstract base class with a protected abstract method AddError for this. The base class can then implement the IValidationCRUD interface, but only after you have removed the private method.

like this:

public interface IValidationCRUD
{
    ICRUDValidation IsValid(object obj);
}

public abstract class ValidationCRUDBase: IValidationCRUD {
    public abstract ICRUDValidation IsValid(object obj);
    protected abstract void AddError(ICRUDError error);
}

Solution 2 - C#

A private member makes no sense as part of an interface. An interface is there to define a set of methods, a role, an object must always implement. Private methods, on the other hand, are implementation details, not intended for public consumption.

Solution 3 - C#

An interface cannot contain private fields. However, the closest behavior to "private fields" you can achieve is by using explicit implementation (explained here: http://msdn.microsoft.com/en-us/library/ms173157.aspx)

Solution 4 - C#

Its not a valid scenario in real case, but still you can achieve through explicit implementation of interface

 interface Itest
 {
      void functiona();
      void functionb();
 }
class child : Itest
{
     public void functiona()
     {
     }
     void Itest.functionb()
     {
     }
}

here functionb() is explicitly implimented so if you created the object of class child you wont get functionb()

Solution 5 - C#

The rule of an interface:

>The CLR also allows an interface to contain static methods, static fields, constants, and static constructors. However, a CLS-compliant interface must not have any of these static members because some programming languages aren’t able to define or access them. In fact, C# prevents an interface from defining any static members. In addition, the CLR doesn’t allow an interface to contain any instance fields or instance constructors.

These are the rules of an interface and you can't do anything on that :)

These are not allowed

interface IIValidationCRUD
{
    static ICRUDValidation IsValid(object obj); //error
}

interface IIValidationCRUD
{
    public ICRUDValidation IsValid(object obj); //error
}

Solution 6 - C#

Have you considered using :

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

Instead of

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

Sorry to join in too late.

Solution 7 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSmartStartView Question on Stackoverflow
Solution 1 - C#Botz3000View Answer on Stackoverflow
Solution 2 - C#Kim GräsmanView Answer on Stackoverflow
Solution 3 - C#unknownView Answer on Stackoverflow
Solution 4 - C#AjiView Answer on Stackoverflow
Solution 5 - C#anishMarokeyView Answer on Stackoverflow
Solution 6 - C#LetsKickSomeAss in .netView Answer on Stackoverflow
Solution 7 - C#Monsieur MersoView Answer on Stackoverflow