K
K
Kamol2016-06-17 16:09:58
User interface
Kamol, 2016-06-17 16:09:58

Minimum set of required parameters in an Interface method in C#?

Good day.
There is an interface with a mandatory method and a minimum set of required parameters.
How to use additional sets of parameters in an inherited class overriding an interface method?
Example:

interface IClass
{
// устанавливаем минимальный набор обязательных параметров
bool A(a,b,c,d);
}

// и наследуюмый класс
class MyClass : IClass
{
    // здесь нужно использовать еще и дополнительные параметры
   bool a(a,b,c,d,e)
   {
     return false
   }
}

I would like to know the best practices on this issue.
Thank you in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MonkAlex, 2016-06-17
@MonkAlex

Instead of a set of parameters, in your case, you need to use an interface \ class, then you can pass a more parameterized interface \ class to the heir and work with it.
It is impossible to add REQUIREMENT of new fields. In general, this behavior is considered undesirable, but if you really need it...:
interface IClassParam
{
string A { get; set; }
}
class ClassParam : IClassParam
{
public string A { get; set; }
public string B { get; set; }
}
interface IClass
{
bool A(IClassParam param);
}
class MyClass : IClass
{
public bool A(IClassParam param)
{
// Field A is available.
var extend = param as ClassParam;
if (extend != null)
{
// you can work with property B
}
return true;
}
}

R
Roman, 2016-06-18
@yarosroman

interface IClass
{
    bool A(a,b,c,d, params object[] args);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question