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
12
votes
4 answers

Why singleton class should be sealed?

I want to know the why a singleton class should be sealed. If we are giving the constructor as private we can prevent the class to be derived right?.. Below i'm pasting few lines from MSDN. Please give me some color on it.. In this strategy, the…
Anish
  • 872
  • 5
  • 21
  • 42
11
votes
1 answer

Why do WinRT types have to be sealed?

In several places (e.g. "Creating Windows Runtime Components for JavaScript, in C# and Visual Basic" on MSDN), I've seen it specified that, if you write a class in .NET that you want to use from JavaScript, then you must make it a sealed class. This…
Joe White
  • 94,807
  • 60
  • 220
  • 330
10
votes
5 answers

How to forbid a class method/property to be overridden in C#?

I believe I want a some methods and properties of a class to be unoverridable and use the base's implementation in all derived classes. How to achieve this? sealed keyword doesn't seem to work and says "method can not be sealed because it is not an…
Ivan
  • 63,011
  • 101
  • 250
  • 382
9
votes
2 answers

Making sealed class hierarchy Parcelable

I have something similar like the following where I want to pass them around as intent arguments; sealed class BasketType : Parcelable { class BasketOne(val basketId: String): BasketType() { constructor(parcel: Parcel) :…
stdout
  • 2,471
  • 2
  • 31
  • 40
9
votes
4 answers

How to get rid of virtual table? Sealed class

As far as I know making class sealed gets rid of look up in VTable or am I wrong? If I make a class sealed does this mean that all virtual methods in class hierarchy are also marked sealed? For example: public class A { protected virtual void…
9
votes
2 answers

Why is the StringBuilder class sealed?

I'm wondering this, since I need to inherit from StringBuilder to implement a TextChanged event. I could always make a wrapper containing a private StringBuilder and implicit/explicit conversions, but this does not seem like a proper…
KappaG3
  • 660
  • 5
  • 14
9
votes
3 answers

Purpose of final and sealed

Why would anyone want to mark a class as final or sealed?
Chad
  • 23,658
  • 51
  • 191
  • 321
8
votes
5 answers

Sealing an interface after implementing it

I am working on a small project and I came across that problem. The project output is a library containing an interface. I would like to implement that interface and seal the functions in it like this if possible: public interface ITest { void…
christ.s
  • 181
  • 1
  • 10
8
votes
9 answers

Why does this C# class declaration compile?

This question really is kinda pointless, but I'm just curious: This: public sealed class MyClass { protected void MyMethod(){} } compiles, but gives a warning while This: public sealed class MyClass { public virtual void…
BFree
  • 102,548
  • 21
  • 159
  • 201
7
votes
1 answer

Inheriting from Sealed classes in MATLAB

In MATLAB, one of the attributes of a class (defined after classdef) is Sealed, which means that no class can use it as a superclass (or to be more precise, "to indicate that these classes have not been designed to support subclasses."1). For…
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
7
votes
0 answers

Should classes still be sealed as a recommendation?

I know this is "technically" a duplicate of this question. Should I recommend sealing classes by default? But I'm asking it again because apparently the recommendation has changed1 with the times though I can't find anything in the last few years…
Storm
  • 1,848
  • 4
  • 20
  • 39
7
votes
2 answers

Why is String class final?

Possible Duplicate: Why is String final in Java? There are various moments in my programming life that I wished the the String class had not been final/sealed/NotInheritable. What are the language architects trying to prevent me from doing that…
Blessed Geek
  • 21,058
  • 23
  • 106
  • 176
7
votes
2 answers

How to override functions from String class in C#

For example, I need to see if a string contains a substring, so I just do: String helloworld = "Hello World"; if(helloworld.Contains("ello"){ //do something } but if I have an array of items String helloworld = "Hello World"; String items = {…
Jronny
  • 2,164
  • 4
  • 30
  • 41
7
votes
2 answers

Why exposed types must be sealed for WinMD/WinRT components?

VS compiler does not allow to create sealed exposed types for WINMD type library. Why is this restriction placed ? (I know about sealed types advantages, my question is with respect to Win RT components).
Tilak
  • 30,108
  • 19
  • 83
  • 131
6
votes
4 answers

'Protected member in sealed class' warning (a singleton class)

I've implemented a singleton class and keep getting the warning that a method I'm writing is a 'new protected member declared in a seal class.' It's not affecting the build but I don't really want to ignore the warning in case it causes problems…
user1017882
1 2
3
12 13