Questions tagged [sealed]

The sealed modifier keyword prevents a C# class from being inherited. Similar to the final keyword in Java.

The sealed modifier keyword prevents a C# class from being inherited. Similar to the final keyword in Java.

183 questions
5
votes
3 answers

Is there an alternative to inheriting from sealed classes?

The reason why I'm asking this is because I want to create a class that has all the functionality of the FileInfo class (derives from FileInfo), and allows me to add my own properties to it. I Think an example will collaborate more. What I…
DerpyNerd
  • 4,743
  • 7
  • 41
  • 92
5
votes
4 answers

In what way is a static class implicitly abstract?

Jon Skeet, in his book C# in Depth, says about a static class: It can't be declared as abstract or sealed, although it's implicitly both. An abstract class is meant to be a base class for derived types. We can instantiate an abstract class only…
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
5
votes
2 answers

Are private classes being sealed at compilation?

Assume the following: we have class B, which is a private class nested inside class A. There isn't any class inheriting from class B. The question is: will the compiler automatically mark class B as Sealed? (NonInheritable in VB). Is there any good…
M.A. Hanin
  • 8,044
  • 33
  • 51
5
votes
4 answers

Sealed must be used with override?

From msdn sealed (C# Reference) "When applied to a method or property, the sealed modifier must always be used with override." Why must it always be used with override?
José D.
  • 4,175
  • 7
  • 28
  • 47
5
votes
5 answers

confusion regarding overriding rules C#

I have a litte confusion regarding method overriding and the validity of OOP priciples. I know everything regarding sealing, shadowing, overriding, virtual etc. but I came across a scenario, that just confused me. Suppose I have: class classA { …
Zeeshan
  • 2,884
  • 3
  • 28
  • 47
5
votes
1 answer

Is it best practice to mark all classes sealed unless you intend them explicitly to be inherited from?

It seems like there are some real benefits to be gained by marking classes as sealed: Performance in some cases Compile-time type safety in other special cases Explicit conversion fails for sealed class Preventing unintended inheritance of your…
richard
  • 12,263
  • 23
  • 95
  • 151
5
votes
3 answers

Why does 'sealed' affect the implementation of IDisposable?

After reading the answer here, I decided to mark my class as sealed in order to simplify the IDisposable implementation. Why does sealed affect the implementation of IDisposable (e.g. GC.SuppressFinalize(this); does not need to be called)? Please…
O.O
  • 11,077
  • 18
  • 94
  • 182
4
votes
2 answers

Why PasswordBox is Sealed in Silverlight?

A simple question, but google has no answer on that! I'm hitting a wall today, because the PasswordBox in Silverlight is Sealed. I have no idea why they do that. Is somebody have an idea on that?
Cyril Gandon
  • 16,830
  • 14
  • 78
  • 122
4
votes
2 answers

Overriding a single interface method when the implementing class is sealed

This is probably easiest to explain with code (this is of course not the actual code but it has the same properties): I have an interface that looks something like this: public interface ISomeProvider { object GetFoo1(); //<-- This…
alun
  • 3,393
  • 21
  • 26
4
votes
2 answers

Composite pattern of sealed class that has no interface

Let's say that I am using a library that I have no control over whatsoever. This library exposes service that requires argument of certain class. Class is marked as sealed and has no interface. tl;dr: How can I reimplement sealed class as…
Jacek Lipiec
  • 412
  • 3
  • 10
4
votes
4 answers

How is this virtual method call faster than the sealed method call?

I am doing some tinkering on the performance of virtual vs sealed members. Below is my test code. The output is virtual total 3166ms per call virtual 3.166ns sealed total 3931ms per call sealed 3.931ns I must be doing something wrong because…
Simon
  • 33,714
  • 21
  • 133
  • 202
4
votes
1 answer

Virtual Inheritance in C#

Quite confused with the rules of virtual inheritance when i have a method which prevents a derived class from overriding the virtual method defined in the base class. Here is a bit of code to explain my problem better : using System; namespace…
mrdoubtful
  • 429
  • 2
  • 7
  • 17
4
votes
3 answers

C++ virtual (sealed) function

I am using classes from a dll in my C++ project. All is working fine, until... When trying to call a certain method (listed in the object browser), I am getting an error that this method is not a member of the namespace. Upon investigation, I…
user228058
  • 465
  • 1
  • 7
  • 22
4
votes
3 answers

sealed method in android

I Am a newbie and I am reding about sealed keyword and referred sealed object. I googled but could not find a simple example of using sealed method in android.I tried something like class A { //declarations } class B extends A { //here i would…
AndroidOptimist
  • 1,419
  • 3
  • 23
  • 38
4
votes
2 answers

How to workaround impossible inheritance from sealed class?

Today im working in WPF. I tried to inherit from System.Windows.Shapes.Line class, like this: class MyLine : System.Windows.Shapes.Line { public Ln() { this.Stroke = Brushes.Black; } } I just realized, that Line class is…
Kamil
  • 13,363
  • 24
  • 88
  • 183