Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can an Interface in C# have a data member with the type of interface itself, which is possible with classes?

For example:
Class SomeClass {
//Some properties defined
//Method follows as
void method1 (SomeClass var1, int var2) {
//set of statements to be executed
}}

Here in question, I am referring to the type of parameter passed in the method of class which is of same type as of class itself.

Q. So, is this possible with an Interface as well?

What I have tried:

Research over classes that can have one, does it apply to the interface as well?
Posted
Updated 20-Sep-17 22:45pm

No. An Interface cannot contain any concrete methods at all, only abstract definitions which must be implemented by inheriting classes.

Abstract classes can define concrete methods, but Interfaces cannot.
 
Share this answer
 
v2
Comments
Graeme_Grant 21-Sep-17 3:12am    
Future C#8, maybe...
Yes this applies to interfaces as well.
 
Share this answer
 
Quote:
For example:
Class SomeClass {
//Some properties defined
//Method follows as
void method1 (SomeClass var1, int var2) {
//set of statements to be executed
}}

Here in question, I am referring to the type of parameter passed in the method of class which is of same type as of class itself.

This is possible (albeit is not what you asked in question title), for instance
C#
interface IMyInterface
{
  void report(IMyInterface mi);
  int id
  {
    get;
  }
}

class MyClass : IMyInterface
{
  int _id;
  public MyClass(int i) { _id = i; }
  public void report(IMyInterface mi)
  {
    Console.WriteLine("{0}", mi.id);
  }
  public int id
  {
    get { return _id; }
  }
}

class Program
{
  static void Main()
  {
    MyClass a = new MyClass(5);
    MyClass b = new MyClass(-1);
    b.report(a);
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900