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
1
vote
2 answers

C# derivative generic type arguments

I am making a web application using ASP.NET MVC 4, Entity Framework and C# and I am writing abstract superclasses to encapsulate entity models and view models. The details aren't that important though, my problem is that I want these abstract…
sara
  • 3,521
  • 14
  • 34
1
vote
4 answers

Creating an extension method against a generic interface or as a generic constraint?

I'm not really sure if there is any real difference here in the two signatures: public static class MyCustomExtensions { public static bool IsFoo(this IComparable value, T other) where T : IComparable { // ... } …
michael
  • 14,844
  • 28
  • 89
  • 177
1
vote
2 answers

c# templates how can i apply constraints to a class that derives from something

stoopid question time again. I have this class that pulls in some code via a base class like so: class TVIRoot : OURTreeNodeImpl { } I now want to add some template functionality class TVIRoot : OURTreeNodeImpl { } But I can't work…
push 22
  • 1,172
  • 3
  • 15
  • 34
1
vote
2 answers

Where constraint for simple types and string

I have a generic method that I would like to put a constraint on. public T MyMethod(object obj) where T : ??? The constraint is all simple types int, bool etc but I also need to allow string. I there a way of constraining this group?
dotnetnoob
  • 10,783
  • 20
  • 57
  • 103
1
vote
2 answers

Why cannot I return something of type specified in the generic type constraint but can return the same type without constraint?

I have this: public IClub GetTeam() { return new Arsenal(); } //compiles since Arsenal is an IClub public T GetTeam() where T : IClub, new() { return new Arsenal(); } //wouldn't compile saying "cannot convert Arsenal to T" But these…
nawfal
  • 70,104
  • 56
  • 326
  • 368
1
vote
1 answer

How to add 2 new() constraint for to type parameters?

I want something like this: public static TTo JumpTo(this TFrom from_page) where TTo : new() TFrom : new() { ... } And I want to enforce that TFrom and TTo are both derived from a base type. And I want to make this method as…
smwikipedia
  • 61,609
  • 92
  • 309
  • 482
0
votes
1 answer

Apparent non-coherent behaviour between F# and C# when using generics constraints

In C#, the following ASP.NET code is valid: class A { public void WithService(IServiceCollection serviceCollection) where IService : class where Service : class, IService { serviceCollection.AddTransient
Franco Tiveron
  • 2,364
  • 18
  • 34
0
votes
1 answer

"Cannot convert from out T to Component", even though T is restrained to be a Component

I'm creating a TryGetComponent(Type type, out Component component) method, and then adding a TryGetComponent(out T component) overload for convenience: public bool TryGetComponent(Type type, out Component component) { component = null; if…
Thomas Slade
  • 129
  • 9
0
votes
0 answers

how can i use multiple interface constraints with OR not AND

I want to write a extension method to use with 2 interface and wrote this, public static void Update(this List entities, int index, T newT) where T : class, IDto, IEntity, new() { } it's looks perfect but I have to implement both interface...…
0
votes
1 answer

How to use "myFunc(Func) where T : class" as well as "myFunc(Func)" implementing the same interface?

Background: To get rid I of redundant code I am using strategy pattern. So I placed this (former) redundant code into context class of strategy pattern, where method IsSuccess ProcessApiMethod() will be called. Context class ist not where my…
0
votes
1 answer

Constraining a generic param with Either

I am using MediatR. Requests are decorated like so public record GetUserInfoQuery(Guid id) : IRequest; public record GetUserInfoResponse(Guid id, string Name); I have decided to try to use functional programming in my app…
Peter Morris
  • 20,174
  • 9
  • 81
  • 146
0
votes
1 answer

How to constrain a generic collection to both a class name and an interface

Given public class BaseClass that has derived classes, where a number of those follow the form public class DerivedClass : BaseClass, ISpecificInterface is there a way of specifying a collection that applies to just the derived classes that…
0
votes
2 answers

How to use Generic interface with two type in return of method

I need to create an interface with 2 type and use it as a method return value. public interface StringLong where T1 : string where T2 : long {} StringLong method StringLong()
0
votes
0 answers

Why was EventArgs constraint dropped from EventHandler?

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 constraint on its implementations. But somewhen in .NET…
Palec
  • 12,743
  • 8
  • 69
  • 138
0
votes
1 answer

C# - Check at compilation level arguments in the generic type constraints | ... where T : new( Foo a )

The very-well known constraint in generic types is new(), it is possible to add parameters like (args,args,...) to force the compiler to check that the class contains a particular constructor? The block shows you the case. public class FooType { …