Several C# questions

  • Thread starter Thread starter Kaki
  • Start date Start date
K

Kaki

1) How come const cannot be declared with static? Does const already
mean static/implemented as static in C#?

2) How do I make a public non-overridable function/property in the super
class private in the subclass? I want to completely hide that public
function without providing a subclass alternate, so the "new" accessor
doesn't really fit here...

Thanks
 
1) How come const cannot be declared with static? Does const already
mean static/implemented as static in C#?
Yes.

2) How do I make a public non-overridable function/property in the super
class private in the subclass? I want to completely hide that public
function without providing a subclass alternate, so the "new" accessor
doesn't really fit here...

You can't, as that would violate the Liskov Substitutability Principle.

Think of it this way: people would still be able to do

Base b = new DerivedClass();
b.MethodRemovedInDerived();

so it wouldn't help you much anyway.

If you don't want your derived class to do everything the base class
can, consider composition instead of inheritance.
 
About this part:

Base b = new DerivedClass();
b.MethodRemovedInDerived();

How come it's not consistent with property? Say there's a property with
get&set in the base class, the language allows the derived class to hide
it with only get/set, but doesn't prevent people from trying:

Base b = new Derived();
b.PropertyReadOnlyInDerived = "something";

Any ideas?
 
About this part:

Base b = new DerivedClass();
b.MethodRemovedInDerived();

How come it's not consistent with property? Say there's a property with
get&set in the base class, the language allows the derived class to hide
it with only get/set, but doesn't prevent people from trying:

Base b = new Derived();
b.PropertyReadOnlyInDerived = "something";

Any ideas?

I don't see that that's inconsistent. Again, you're making the compiler
only consider it as if it were the base class, not the derived class.
Why is this inconsistent?
 
Back
Top