ChuckT
ChuckT

Reputation: 75

C# Struct Hiding Through Inheritance

Section 3.7.1.2 Hiding Through Inheritance of the C# 4 specification discusses the ability for classes or structs to hide members through re-declaring names that were used in base classes.

This is all well and good, but I thought that one of the distinctions of structs was that they could not be inherited.

Upvotes: 2

Views: 130

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063013

struct still inherits from object, via ValueType. You can new (hide) a GetHashCode, Equals or ToString method - however, it would be incredibly stupid to do so, as that would mean you can't override it, which means it will always be a boxing call to use them (even when done as a constrained call).

So, in order:

  • is this true: yes, it is true that you cannot inherit from a struct
  • is it possible: the only thing comparable, other than above, would be explicit interface implementation
  • to glean: the specification does not disallow you from doing things that are silly

Upvotes: 6

Related Questions