O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sealing and Hiding: Take Two

As previously mentioned, the sealed keyword prevents a class from being derived any further. We can also apply such specialization prevention at a more granular level of members, too. For example, although we may leave our Square type unsealed (the default), we can prevent people from overriding the virtual properties we’ve defined:

class Square : Shape{    public Square(double side)    {        Side = side;    }    public double Side { get; private set; }    public sealed override double Area    {        get { return Side * Side; }    }    public sealed override double Circumference    {        get { return 4 * Side; }    }}

Now if someone comes in and defines a subclass of Square, say ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required