generic-constraints Questions

10

Solved

In C# you can put a constraint on a generic method like: public class A { public static void Method<T> (T a) where T : new() { //...do something... return new T(); } } Where you spec...
Baras asked 5/12, 2009 at 17:37

3

Solved

The where T : struct constraint lets one to limit the domain of acceptable type parameters to the set of value types (as compared to the superset of types including both value and reference types) ...
Heisler asked 26/3, 2017 at 2:27

1

Solved

I want to write a single function that can add certain fields to Firebase message structs. There are two different types of message, Message and MulticastMessage, which both contain Android and APN...
Histogenesis asked 17/6, 2022 at 20:50

2

Solved

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<T> ...
Flung asked 10/2, 2017 at 9:25

22

Solved

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 stat...
Mosstrooper asked 17/9, 2008 at 1:56

0

In .NET Framework 2.0, delegate EventHandler got its generic generalization that allowed the second argument to be not only of type EventArgs, but also of a derived type, imposing a stricter constr...
Augsburg asked 31/5, 2021 at 18:44

2

Solved

Full signature of method: public static <T, U extends Comparable<? super U>> Comparator<T> comparing( Function<? super T, ? extends U> keyExtractor) I'm learning lambda e...
Semination asked 27/12, 2020 at 17:38

1

Solved

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...
Bobbybobbye asked 20/9, 2018 at 8:39

2

Solved

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 ^...
Trixie asked 20/2, 2018 at 3:45

2

Solved

Given the following code... type Indexable<TKey, TValue> = { [index: TKey]: TValue } This produces the following error: An index signature parameter type must be 'string' or 'number'. ...
Walleye asked 23/10, 2017 at 9:1

3

Solved

Is it possible to have a generic constraint which is an unbounded generic type? For example: public T DoSomething<T>(T dictionary) where T : IDictionary<,> { ... } Edit: Just to ...
Gadget asked 1/8, 2012 at 18:4

2

Solved

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 { @Over...
Hearts asked 10/8, 2016 at 2:49

1

Solved

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 ...

2

Solved

Given an object of type System.Reflection.MethodInfo how can I extract generic parameter constraints? Somehow I can not find reasonable information about this.
Frazzled asked 18/11, 2015 at 8:57

1

Solved

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 ...
Valuator asked 6/5, 2015 at 9:7

7

Solved

After failing to get something like the following to compile: public class Gen<T> where T : System.Array { } with the error A constraint cannot be special class `System.Array' I start...
Glyptograph asked 30/4, 2015 at 7:40

3

Solved

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<T> whe...
Felty asked 24/3, 2015 at 9:9

2

Solved

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 consol...
Effluent asked 10/2, 2013 at 5:29

1

Solved

I'd like to be able to extend types from other libraries with static methods to enable generic arithmetic. Take, for example, the newly minted SIMD-friendly fixed-size VectorN types from Microsoft....
Vivavivace asked 17/8, 2014 at 4:10

4

Solved

Im having some trouble getting this generic constraint to work. I have two interfaces below. I want to be able to constrain the ICommandHandlers TResult type to only use types that implement ICo...
Khaki asked 18/3, 2013 at 10:1

3

Solved

I have run across an issue with overloading methods that have different constraints that seems exclusive. That is my example: public class A { public void Do<T>() where T : class { } p...
Attitudinarian asked 12/3, 2013 at 16:39

4

Hi I would like to have a class: class Matrix <T> where T : // I don't know what to write here { T[][] elements; } I would like T to be addable and multiplyable by + and * operator...
Madelynmademoiselle asked 7/2, 2013 at 18:57

1

Solved

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<T>...
Gargantua asked 10/12, 2012 at 3:20

2

Solved

Basically I have the following: public static bool IsBetween<T>(this T value, T a, T b) where T : IComparable { ... } public static bool IsBetween<T>(this T value, T a, T b) where ...

4

Solved

Item class public class Item { public bool Check(int value) { ... } } Base abstract class with generic type constraint public abstract class ClassBase<TItem> where TItem : Item { prote...
Demetricedemetris asked 22/12, 2011 at 15:56

© 2022 - 2024 — McMap. All rights reserved.