Sergey Metlov
Sergey Metlov

Reputation: 26331

How to solve this inheritance difficulty?

I use class A from third-party library. Class has method M:

public class A
{
    public int M(int x)
    {
        ...
    }
}

I've written my class B with method M:

public class B : A
{
    public void M(params int[] xs)
    {

    }
}

And then in class C I want to call method M of parent class A

public class C : B
{
    public void M2()
    {
        int result = M(1);
    }
}

But compiler marks this line as error. It tries to use method M of class B, that returns void.
How can I solve this collision without rewriting class B?

Upvotes: 1

Views: 88

Answers (1)

Ivan Danilov
Ivan Danilov

Reputation: 14787

I think that ((A)this).M(1) would help you. Note that if those methods are virtual/overridden somewhere - things could change.

Thus said, I think that hiding method in such a way is a bad idea that communicating either poor design choices or some hackery. At least I can't think out a way when I really need this.

Upvotes: 6

Related Questions