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
2
votes
1 answer

Generic class type constraint with a generic class having another constraint

Let's say I have a generic class public class G { } and I want to declare final class as public class F where T : G { } that seems to be possible, but what if I want to complicate the task and add a constraint to the class G like…
Alex
  • 655
  • 1
  • 8
  • 16
2
votes
1 answer

Generic constrain for Lists and Collections

I have a Generic method which looks something like this: public int GetCount(T collection) where T: ICollection { return collection.Count; } Now I want to be able to call this method where the collection parameter can be a List or a…
Twenty
  • 5,234
  • 4
  • 32
  • 67
2
votes
1 answer

Why doesn't generic constraint help compiler decide among polymorphic methods with optional argument?

Given 2 static overloaded methods, one generic and one not: public static T? NullIf(this T value, T equalsThis) where T : struct // value types including enums { return EqualityComparer.Default.Equals(value,…
crokusek
  • 5,345
  • 3
  • 43
  • 61
2
votes
2 answers

Creating a generic method in C# through specific where clause

I want to create some generic methods like code below: public async Task Get(string url) where T : IBaseModel, IList Obviously I want to support enumerable collection and also the single object which drived from IBaseModel…
Saman Gholami
  • 3,416
  • 7
  • 30
  • 71
2
votes
1 answer

Avoiding explicit generic type c#

Say I have a generic class that has a constraint where T must implement IWatchable, is there any way of then using the Watcher without having to explicitly declare the TKey type, considering that T would be providing that anyway? public class…
iswinky
  • 1,951
  • 3
  • 16
  • 47
2
votes
0 answers

Possible Bug? I can create generic instance ignoring constraint

While working on a node based framework including serialization and deserialization i stummbeld over a case where i can create an instance of a class ignoring the type constraint. Why can i create a ZNumericProcessor inside ZSum although…
Alex
  • 541
  • 5
  • 19
2
votes
0 answers

Why do I need 'class' constraint here

Given the following example, I get the following compiler error: Cannot apply operator '==' to type 'TEnumerable' and 'TEnumerable' class MyClass where TEnumerable : IEnumerable { public void DoSomething(TEnumerable…
Flat Eric
  • 7,971
  • 9
  • 36
  • 45
2
votes
1 answer

C# generic constraint: Array of Structs

I would like to create a generic constraint that contains the type to be an array of value types (structs), something like: public class X where T : struct[] or maybe public class X where U : struct where T : U[] but this doesn't…
Wilbert
  • 7,251
  • 6
  • 51
  • 91
2
votes
3 answers

Java factory pattern using generic constraints

I have public class MyFactory() { public static T getItem(Element element, Class clazz) { T item = null; if (clazz == IFoo.class) { item = (T) new Foo(); } else if (clazz ==…
Professor Chaos
  • 8,850
  • 8
  • 38
  • 54
2
votes
3 answers

Simple Generic misunderstanding

Could you help me to understand the error in those case ? public interface IGeneralInterface { } public class A : IGeneralInterface { } public class B : IGeneralInterface { } public class SomeClass where TGenericType :…
Alex F
  • 3,180
  • 2
  • 28
  • 40
2
votes
1 answer

Type inference infers in the scope of the class but not methods though they are just the same?

Ok I must have got the title terribly wrong. More code, less words: public class Manager where T : Control, new() { void Manage() { Do(t => IsVisible(t)); } bool IsVisible(T t) { return t.Visible; } S…
nawfal
  • 70,104
  • 56
  • 326
  • 368
2
votes
2 answers

Generic constraint inference in class signature

I am frustrated by certain requirements when working with C# generic constraints, and I'd like to know if there is a way around my issues. If not, I'd like an explanation for why things don't work the way I'd like them to. This example shows what I…
mattbob
  • 109
  • 4
1
vote
1 answer

Return type in TypeScript function with Generic Constraints has concrete values instead of types

function createGenericCoordinates( x: Type, y: Type ) { return { x, y }; } const genericCoordinates = createGenericCoordinates(1, 2); // Error (TS2322) // Type 3 is not assignable to type 1 |…
makkabi
  • 619
  • 6
  • 10
1
vote
2 answers

Defining the type NumMax

Is it possible to define a type NumMax in Typescript that takes the maximum of two types, such that NumMax is assignable to z? The following definition does not work: export type NumMax = [x, y]…
Carucel
  • 435
  • 5
  • 11
1
vote
0 answers

Typescript's instanceof ignores initial type

When i want to check if a function parameter is a derived class, the instanceof operator allows me to compare it against virtually any class, ignoring the fact that class Random is completely incompatible with other classes. class Tag { public…
BMFreed
  • 11
  • 1