Hiding baseclass members

K

Kimmo Laine

Hi,

is there a way to hide baseclass members:

class BaseClass {
public BaseClass() {}
public string Get() { return "base"; }
}

class MyClass: BaseClass {
public MyClass() :base() {}
new public string Get() { return "my"; }

}

void Foo() {
MyClass m = new MyClass();
Debug.WriteLine( m.Get() );

BaseClass b = m;
Debug.WriteLine( b.Get() );
}

This code will print "my" and then "base". Is there as way to hide the
BaseClass.Get completely?


thx

Kimmo Laine
 
J

Jon Skeet [C# MVP]

Kimmo Laine said:
is there a way to hide baseclass members:

Well, you can override them instead of hiding them, if they're virtual.
That's the only way though.
 
J

John Wood

Not that this is a solution... but I've generally found that when you start
wanting to hide base class methods, you probably need to reconsider the
design. Perhaps you need to use the decorator pattern and delegate the
exposed functions to a member variable?
 
M

mdb

Hi,

is there a way to hide baseclass members:

class BaseClass {
public BaseClass() {}
public string Get() { return "base"; }
}

Make your BaseClass.Get() function virtual...

public virtual string Get() { ... }
class MyClass: BaseClass {
public MyClass() :base() {}
new public string Get() { return "my"; }

}

And make your MyClass.Get override it:

public override string Get() { ... }

-mdb
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top