-5
class Test {
    public Test(string name, string age) {
    }
}

interface ITest
        ????????

class Example: ITest {
    public Example(/* ... */) {
    }
}

So what should I use in my interface to use the same parameters in my example as in test?

What I want to do:

  • I have 3 classes: a, b and c.

  • I want my c to inherit from both a and b.

But C# doesn't let you do that ... So I want to use an interface.

EDIT My classes are:

  • Student(name, age, studies)
  • Teacher(name, age, classes)
  • Working Student/Teacher(name, age, studied/classes, payment)

So i want to use methods in my working class from the other classes.

user1765216
  • 111
  • 2
  • 2
  • 8

4 Answers4

1

Interfaces are used as a contract for classes that inherit it. This means any public methods/properties you want the interface to expose, must also be exposed in the inherited class.

In the case above, you don't have any public methods/properties exposed in the interface. Let me show you an example.

Interface ITest
{
   void AddPerson(string name, string age);
}

Interface IPerson
{
    string ReturnPersonsAge(string name);
}

/// This must expose the AddPerson method
/// This must also expose the ReturnPersonByAge method
class Example : ITest, IPerson
{
   Dictionary<string, string> people = new Dictionary<string, string>();

   void AddPerson(string name, string age)
   {
      people.Add(name, age);
   }

   string ReturnPersonsAge(string name)
   {
      return people[name];
   }
}

I hope that helps, but feel free to ask more questions so I can help narrow down your question.

Kyle C
  • 1,627
  • 12
  • 25
0

Interfaces can't define constructors. There certainly is other way to do it, depends on what you want to achive.

Zbigniew
  • 27,184
  • 6
  • 59
  • 66
0
Interface IAClass
{
  void Add(string s);
}
Interface IBClass
{
  void Delete(string m);
}


Class A: IAClass
{
  void Add(string name)
{
  // Your Logic
}
}

Class B: IBClass
{
  void Delete(string name)
{
  // Your Logic
}
}

Class C:IAClass,IBClass
{
     void Add(string name)
{
  // Your Logic
}

 void Delete(string name)
{
  // Your Logic
}
}
Akshay Joy
  • 1,765
  • 1
  • 14
  • 23
-1

You can expose them as properties in the interface

Interface ITest
{
string Name{get;set;}
string Age{get;set;}
}
AD.Net
  • 13,352
  • 2
  • 28
  • 47