Junior — Middle
Is it possible to declare private methods inside an interface in a programming language?
sobes.tech AI
Answer from AI
Starting from version 8.0, C# introduced the ability to declare private methods within interfaces. This allows hiding auxiliary logic used in default interface methods from external access.
Example:
public interface IExample
{
void PublicMethod()
{
PrivateHelper();
}
private void PrivateHelper()
{
Console.WriteLine("Private method inside interface");
}
}
Before C# 8.0, interfaces could only contain method declarations without implementation, and private methods inside interfaces were not possible.