Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we can’t use sealed classes as generic constraints?

Tags:

Can you guess what is the reason to not allow sealed classes for type-constraints in generics? I only have one explanation is to give opportunity to use naked constraints.

like image 935
Grigory Bushuev Avatar asked Dec 22 '09 09:12

Grigory Bushuev


People also ask

Why we Cannot inherit sealed class?

You cannot derive or extend any class from it. Sealed method is implemented so that no other class can overthrow it and implement its own method. The main purpose of the sealed class is to withdraw the inheritance attribute from the user so that they can't attain a class from a sealed class.

Can generic classes be constrained?

Declaring those constraints means you can use the operations and method calls of the constraining type. If your generic class or method uses any operation on the generic members beyond simple assignment or calling any methods not supported by System. Object, you'll apply constraints to the type parameter.

Can a generic class have multiple constraints?

There can be more than one constraint associated with a type parameter. When this is the case, use a comma-separated list of constraints. In this list, the first constraint must be class or struct or the base class.

What are generic constraints?

The where clause in a generic definition specifies constraints on the types that are used as arguments for type parameters in a generic type, method, delegate, or local function. Constraints can specify interfaces, base classes, or require a generic type to be a reference, value, or unmanaged type.


2 Answers

If the class is sealed it cannot be inherited. If it cannot be inherited it'd be the only type valid for the generic type argument [assuming if allowed to be a type argument]. If it is the only generic type argument then there's no point in making it generic! You can simply code against the type in non-generic class.

Here's some code for this.

public class A {     public A() { } }  public sealed class B : A {     public B() { } }  public class C<T>         where T : B {     public C() { } } 

This will give compiler error: 'B' is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter.

In addition to this, You can also not have a static class as generic type-constraint. The reason is simple. Static classes are marked as abstract and sealed in compiled IL which can be neither instantiated nor inherited.

Here's the code for this.

public class D<T>         where T : X {     public D() { } }  public static class X { } 

This will give compiler error:'X': static classes cannot be used as constraints.

like image 66
this. __curious_geek Avatar answered Oct 24 '22 18:10

this. __curious_geek


Are you talking about something like this:

class NonSealedClass { }  class Test<T> where T : NonSealedClass { } 

Because it's perfectly legal.

like image 36
Aviad P. Avatar answered Oct 24 '22 19:10

Aviad P.