interface ICommon
{
private void Print()
{
Console.WriteLine("C#8 onward access modifier allowed.");
}
}
class Test : ICommon { }
class Program
{
static void Main(string[] args)
{
Test test = new Test();
}
}ExampleWe can create private method inside an interface as a helper method.
interface ILogger
{
void LogInfo(string msg)
{
Write("INFO", msg);
}
void LogError(string msg)
{
Write("ERROR", msg);
}
// shared helper
private void Write(string type, string msg)
{
Console.WriteLine($"[{type}] {msg}");
}
}
class MyLogger : ILogger { }
class Program
{
static void Main()
{
ILogger logger = new MyLogger();
logger.LogInfo("Started");
logger.LogError("Failed");
}
}
OUTPUT
[INFO] Started
[ERROR] Failed
No comments:
Post a Comment