Key points
1.DEFAULT METHODS ARE ACCESSIBLE THROUGH A REFERENCE VARIABLE OF THE INTERFACE TYPE.
ISports obj = new Cricket();
Console.WriteLine(obj.CentralHeadQuarter());
public interface ISports
{
public string SportsName { get; set; }
public string CentralHeadQuarter()
{
return "DELHI";
}
}
public class Cricket : ISports
{
private string _sportsName;
public string SportsName { get => _sportsName; set => _sportsName = value; }
}
class Program
{
public static void Main()
{
ISports obj = new Cricket();
//Default methods are accessible through a reference variable of the interface type.
Console.WriteLine(obj.CentralHeadQuarter());
Console.ReadKey();
}
}
2. INTERFACE MEMBERS CAN HAVE ACCESS MODIFIERS OTHER THAN PUBLIC- PRIVATE , PROTECTED, INTERNAL ,PROTECTED INTERNAL, PRIVATE PROTECTED
internal string AnnualMeet();
ISports obj = new Cricket();
Console.WriteLine(obj.AnnualMeet());
public interface ISports
{
public string SportsName { get; set; }
public string CentralHeadQuarter()
{
return "DELHI";
}
internal string AnnualMeet();
}
public class Cricket : ISports
{
private string _sportsName;
public string SportsName { get => _sportsName; set => _sportsName = value; }
string ISports.SportsName { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
string ISports.AnnualMeet()
{
return "AT MUMBAI";
}
public string AnnualMeet()
{
return "AT KOLKOTA";
}
}
class Program
{
public static void Main()
{
ISports obj = new Cricket();
Console.WriteLine(obj.AnnualMeet());//calls the first AnnualMeet
Cricket obj1= new Cricket();//calls the second AnnualMeet
Console.WriteLine(obj1.AnnualMeet());
Console.ReadKey();
}
}
output
AT MUMBAI
AT KOLKOTA
3. PRIVATE INTERFACE- SHOULD HAVE BODY + IT WILL BE CALLED INSIDE INTERFACE ITSELF
private string AppendHeadQuarter() { return "The HeadQuarter is at:: "; }
public interface ISports
{
public string SportsName { get; set; }
private string AppendHeadQuarter()
{
return "The HeadQuarter is at:: ";
}
public string CentralHeadQuarter()
{
return AppendHeadQuarter() + "DELHI";
}
}
public class Cricket : ISports
{
public string SportsName { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public string AnnualMeet()
{
return "AT KOLKOTA";
}
}
class Program
{
public static void Main()
{
ISports obj = new Cricket();
Console.WriteLine(obj.CentralHeadQuarter());
Console.ReadKey();
}
}
output
The HeadQuarter is at:: DELHI
4. STATIC INTERFACE
Console.WriteLine( obj.AppendHeadQuarter()); //you cannot do this
Console.WriteLine( ISports.AppendHeadQuarter()); //you can do this
public interface ISports
{
public string SportsName { get; set; }
internal static string AppendHeadQuarter()
{
return "The HeadQuarter is at:: ";
}
public string CentralHeadQuarter()
{
return AppendHeadQuarter() + "DELHI";
}
}
public class Cricket : ISports
{
public string SportsName { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public string AnnualMeet()
{
return "AT KOLKOTA";
}
}
class Program
{
public static void Main()
{
ISports obj = new Cricket();
Console.WriteLine(obj.CentralHeadQuarter());
//Console.WriteLine(obj.AppendHeadQuarter());//you cannot do this
Console.WriteLine(ISports.AppendHeadQuarter());//you cannot do this
Console.ReadKey();
}
}
output
The HeadQuarter is at:: DELHI
The HeadQuarter is at::