type-constraints

How to define generic type limit to primitive types?

I have the following method with generic type: T GetValue<T>(); I would like to limit T to primitive types such as int, string, float but not class type. I know I can define generic for class type like this: C GetObject<C>() where C: class; I am not sure if it is possible for primitive types and how if so. ...

When should or shouldn't I be using generic type constraints?

I've got a base class: public abstract class StuffBase { public abstract void DoSomething(); } And two derived classes public class Stuff1 : StuffBase { public void DoSomething() { Console.WriteLine("Stuff 1 did something cool!"); } public Stuff1() { Console.WriteLine("New stuff 1 reporting for...

C#: How to find and create instances which fullfills multiple type constraints

Ok, maybe that title didn't make much sense, but here is the deal. Say I have a generic method with multiple type constraints, this this: public static void DoSomethingAwesome<T>(T thing) where T : IThing, IAwesome, IComparable<T> { ... } Now.... how can I, using reflection, create something I can send in there? If it was on...

Extension methods for specific generic types

I'm attempting to create various extension method for a generic type bound to specific generic type parameters in F#, but the language does not seem to be allowing me: What I want to do is something like the following: type IEnumerable<int> with member this.foo = this.ToString() Yet it gives me the compiler error (underli...

C# generic "where constraint" with "any generic type" definition?

Let me give example: I have some generic class/interface definition: interface IGenericCar< T > {...} I have another class/interface that I want to relate with class above, for example: interface IGarrage< TCar > : where TCar: IGenericCar< (any type here) > {...} Basically, I want my generic IGarrage to be dependent on IGenericCar,...

Non-strict multiple interface type parameter constraints?

Excuse me if this is a dupe, but I couldn't seem to get the right combo of keywords to filter down the various type constraint and generics questions out there (as there are a lot). I have two interfaces--let's call them IOnline and IOffline. They're closely related in that they describe nearly identical contracts, but one of the key...

Why does Haskell stop short of inferring the datatype's typeclasses in the function signatures?

Firstly, this question isn't 100% specific to Haskell, feel free to comment on the general design of typeclasses, interfaces and types. I'm reading LYAH - creating types and typeclasses The following is the passage that I'm looking for more information on: Data (Ord k) => Map k v = ... However, it's a very strong convention in...

When is it important to have a public parameterless constructor in C#?

I'm trying to understand the constraints on generic type parameters in C#. What is the purpose of the where T : new() constraint? Why would you need to insist that the type argument have a public parameterless constructor? Edit: I must be missing something. The highest rated answer says the public parameterless constructor is necessa...

How to Work Around Limitations in Generic Type Constraints in C#?

Okay I'm looking for some input, I'm pretty sure this is not currently supported in .NET 3.5 but here goes. I want to require a generic type passed into my class to have a constructor like this: new(IDictionary<string,object>) so the class would look like this public MyClass<T> where T : new(IDictionary<string,object>) { T Creat...

How to make safe cast using generics in C#?

I want to implement a generic method on a generic class which would allow to cast safely, see example: public class Foo<T> : IEnumerable<T> { ... public IEnumerable<R> SafeCast<R>() where T : R { return this.Select(item => (R)item); } } However, the compiler tells me that Foo<T>.SafeCast<R>() does not d...

Attribute constraint on a generic type in .net?

in .net, if I have a generic class SomeClass<T>, is it possible to use the where keyword to require that T is a class with a certain attribute? something like: [SomeAttribute] class MyClass { ... } class AnotherClass<T> where T : Attribute(SomeAttribute) { ... } ...

Trying to understand Haskell's => vs defining the types

In Haskell, why would you define a function with a type constraint: ghci> :t (==) (==) :: (Eq a) => a -> a -> Bool Rather than defining it so it's type was: ghci> :t (==) (==) :: Eq -> Eq -> Bool ...

Writing an extension method for type T; how can I add a type constraint for a field of T?

Initial situation: I am working with a proprietary framework (ESRI's ArcGIS Engine) which I want to extend with some new functionality. I've chosen to use extension methods in C# for this. Shown below are the parts of the framework API that are relevant to this question: +------------------------+ IGeometry |...

Specifying constructor constraint for Generic Parameter

I have a collection of objects which I pass as parameter to create objects of another type (one for one). I am doing this in many places (basically converting from data objects to business objects). I want to write a generic extension method to accomplish this. But I am stuck because I don't know how I can specify constraint that busines...

Static extension methods supporting member constraints

I need to implement a static extension method supporting member constraints on some basic primitive types like integers, floats, etc. Here's my code for signed integers: module MyOperators = let inline foo (x : ^T) = (^T : (static member Foo : ^T -> int) (x)) type System.Int32 with static member Foo(x : Int32) = 7 // ...

C# Generic Constraints on function

I want to write a generic function that has a constraint on the type. Specifically I want something like this: bool IsInList<T>(T value, params T[] args) { bool found = false; foreach(var arg in args) { if(arg == value) { found = true; break; } } return found; } The ...

How to specify a type parameter which does NOT implement a particular interface?

I have developed some extension methods for objects, which I don't want to be used/shown in intellisense for objects which implements IEnumerable. Conceptually I want something like as follows public static T SomeMethod<T>(this object value) where T != IEnumerable { } Is it possible to impose this kind of constraint a...

Can I dictate that a C# type parameter must only be an interface type?

I would like to implement a generic C# class which looks roughly as follows: abstract class Foobar<T> : AbstractBase, T { ... } This fails because C# will only allow types after the base class to be interfaces, so next I try this: abstract class Foobar<T> : AbstractBase, T where T : interface { ... } But then I find that C# does no...

Is there a technique to differentiate class behavior on generic types?

I'd like to do something like the following, but because T is essentially just a System.Object this won't work. I know T can be constrained by an interface, but that isn't an option. public class Vborr<T> where T : struct { public Vborr() { public T Next() { if ( typeof( T ) == typeof( Double ) ) { ...