Questions tagged [generic-constraints]

A generic constraint will restrict the types that can be used for a specific generic. Use this tag for questions regarding generic constraints.

allow a programmer to restrict the types, which can be used for specific . This will also allow the compiler to ensure that specific object members exist. Therefore programmers can write more flexible code.

This feature is implemented in a range of languages including , , , , and .

136 questions
1414
votes
22 answers

Create Generic method constraining T to an Enum

I'm building a function to extend the Enum.Parse concept that Allows a default value to be parsed in case that an Enum value is not found Is case insensitive So I wrote the following: public static T GetEnumFromString(string value, T…
johnc
  • 39,385
  • 37
  • 101
  • 139
208
votes
9 answers

Is there a generic constructor with parameter constraint in C#?

In C# you can put a constraint on a generic method like: public class A { public static void Method (T a) where T : new() { //...do something... } } Where you specify that T should have a constructor that requires no…
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
115
votes
7 answers

What exactly is a "Special Class"?

After failing to get something like the following to compile: public class Gen where T : System.Array { } with the error A constraint cannot be special class `System.Array' I started wondering, what exactly is a "special class"? People often…
user3079266
27
votes
4 answers

Why aren't generic type constraints inheritable/hierarchically enforced

Item class public class Item { public bool Check(int value) { ... } } Base abstract class with generic type constraint public abstract class ClassBase where TItem : Item { protected IList items; public…
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
20
votes
1 answer

Why does Nullable not match as a reference type for generic constraints

Possible Duplicate: Nullable type as a generic parameter possible? I came across a very weird thing with generic type constraints. I have a class like this: public SomeClass where T:class { } However, I've found I can't use nullable types as…
Earlz
  • 62,085
  • 98
  • 303
  • 499
18
votes
2 answers

TypeScript - How to represent an index signature as a generic type

Index signatures in TypeScript are defined thus: Dictionary [key: string]: T Array [index: number]: T These could be wrapped into some simple, reusable types: type DictionaryIndex = { [key: string]: T } type ArrayIndex = { [index:…
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
15
votes
1 answer

Why exactly are these "Special Classes"?

After reading this question asking what exactly a “Special Class” is, I am left with the question why the six classes System.Object, System.Array, System.Delegate, System.Enum and System.ValueType were chosen and hard-coded as special classes,…
Alex Essilfie
  • 12,339
  • 9
  • 70
  • 108
14
votes
2 answers

Why a generic method of an interface can be implemented as non-generic in Java?

Let's say we have a few test interfaces/classes like this: abstract class Plant { public abstract String getName(); } interface Eatable { } class Apple extends Plant implements Eatable { @Override public String getName() { …
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
14
votes
2 answers

Why can't System.Array be a type constraint?

I'm working on a small project with a few different types of arrays (e.g. double[], float[], int[]. For verification / testing / sanity purposes, I'm printing out some of these arrays to the console as I go along. So I have multiple functions that…
vlad
  • 4,748
  • 2
  • 30
  • 36
12
votes
2 answers

TypeScript - can a generic constraint provide "allowed" types?

Given the following code... type Indexable = { [index: TKey]: TValue } This produces the following error: An index signature parameter type must be 'string' or 'number'. Is there a way to constrain TKey to be 'string' or 'number'?
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
10
votes
3 answers

Wildcards in C# generic constraints

I'm aware that C# doesn't have generic wildcards, and that a similar effect can be achieved by generic methods, but I need to use a wildcard in a field and can't work out if there is any way to encode it. List> list; void…
zmthy
  • 568
  • 3
  • 13
10
votes
3 answers

Base class constraint on generic class specifying the class itself

Yesterday, I was explaining C#'s generic constraints to my friends. When demonstrating the where T : CLASSNAME constraint, I whipped up something like this: public class UnusableClass where T : UnusableClass { public static int method(T…
user3079266
9
votes
1 answer

Can I use C# 7.3 with .Net Framework 4.6.1?

I'm interested in enum generic constraints, but when I'm switching language version for project on Build → Advanced I'm still getting error «not available in C#5; please use language version 7.3 or greater» even after reopening the project. Project…
Troll the Legacy
  • 675
  • 2
  • 7
  • 22
9
votes
1 answer

How to add LanguagePrimitives.GenericZero / get_Zero to System.String?

Note: I added a lot of Of interest comments at the end. These are not mean to suggest that one should use inline and static type parameters willy nilly, they are there so that one does not have to spend hours searching lots of SO questions related…
Guy Coder
  • 24,501
  • 8
  • 71
  • 136
8
votes
2 answers

F# error in generic constraint

The following F# code let f<'T when 'T: (member Id:int)> (t:'T) = t.Id is not accepted with the following error: Error FS0670 This code is not sufficiently generic. The type variable ^T when ^T : (member get_Id : ^T -> int) could not be…
Franco Tiveron
  • 2,364
  • 18
  • 34
1
2 3
9 10