DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Using Sealed in CSharp

Dave Bush user avatar by
Dave Bush
·
Dec. 08, 08 · Interview
Like (0)
Save
Tweet
Share
5.74K Views

Join the DZone community and get the full member experience.

Join For Free

Another potentially useful but not very popular keyword in CSharp is the sealed keyword.  While not a keyword that you will need to use often, it is a critical keyword for framework developers who want to have tighter control over how their classes get used.[img_assist|nid=6421|title=|desc=|link=none|align=right|width=244|height=184]

The two primary uses of the sealed keyword are as they apply to the class and as they are applied to elements of the class.

To start with the easiest first, we will apply sealed to the class.  You would applied sealed to a class when you want to create a class that no one else can inherit.  The string class is one such class.

class dmbcllc_com
{
}

sealed class blog_dmbcllc_com : dmbcllc_com
{
}

In the example above, the class dmbcllc_com can be used and inherited from anyone who can access it while blog_dmbcllc_com can only be used but can not be inherited from since we have sealed it.

Notice that the sealed keyword comes before the class keyword.

You might also apply the sealed keyword to virtual functions that you are overriding in a child class.  This has the effect of allowing you to prevent the virtual function from being overridden in the child class past the class where it has been sealed.

class dmbcllc_com
{
public virtual Foo() {}
}

class blog_dmbcllc_com : dmbcllc_com
{
sealed public override Foo() {}
}

class errorClass : blog_dmbcllc_com
{
public override Foo() {}
}

In the example above, errorClass.Foo() will cause a compile error because we are trying to override a method that has been sealed in the class above it.

You should be careful when using the sealed keyword that you do not unnecessarily seal off classes or methods simply because you can. You may end up backing yourself into a corner. It is possible to protect a class so much that it becomes completely unusable. One place I’ve seen this happen before is in the Java classes that were used to render HTML. The problem was ultimately fixed, but the initial implementation made it so that no one but the guys who wrote the classes could use them.

Seal classes and methods only when leaving them unsealed will be more dangerous than sealing them.

Reference:

Using Sealed in CSharp

 

csharp

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Using the PostgreSQL Pager With MariaDB Xpand
  • Top Authentication Trends to Watch Out for in 2023
  • Writing a Modern HTTP(S) Tunnel in Rust
  • Why Open Source Is Much More Than Just a Free Tier

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: