#767 – A struct Is Implicitly Sealed
January 28, 2013 1 Comment
Every struct in C#, whether it is user-defined or defined in the .NET Framework, is sealed–meaning that you can’t inherit from it. A struct is sealed because it is a value type and all value types are sealed.
A struct can implement an interface, so it’s possible to see another type name following a colon, after the name of the struct.
In the example below, we get a compile-time error when we try to define a new struct that inherits from the one defined above.
public struct PersonName
{
public PersonName(string first, string last)
{
First = first;
Last = last;
}
public string First;
public string Last;
}
// Error at compile time: Type 'PersonName' in interface list is not an interface
public struct AngryPersonName : PersonName
{
public string AngryNickname;
}
Pingback: Dew Drop – January 28, 2013 (#1,488) | Alvin Ashcraft's Morning Dew