6

If I have a generic method that is constrained to be type 'int' then surely I should be able to cast an integer to the generic T type. For example...

 public T ExampleMethod<T>(int unchanged) where T : int { return (T)unchanged; } 

...the compiler complains that Cannot convert type 'int' to 'T' but I have a constraint indicating that the target is as integer. So surely it should work?

Update:

The actual scenario is that I want to a helper method that returns an enum value. So my ideal helper method would be more like this....

public T GetAttributeAsEnum<T>(XmlReader reader, string name) where T : enum { string s = reader.GetAttribute(name); int i = int.Parse(s); return (T)i; } 

...and use it like this...

StateEnum x = GetAttributeAsEnum<StateEnum>(xmlReader, "State"); CategoryEnum y = GetAttributeAsEnum<CategoryEnum>(xmlReader, "Category"); OtherEnum z = GetAttributeAsEnum<OtherEnum>(xmlReader, "Other"); 

...but you cannot constrain by enum.

8
  • Why not just specify the parameter type as T? Commented Aug 2, 2012 at 4:06
  • My real situation is more complicated. I want a helper method tthat reads a string from a file, the string is actually a number. Then cast the number to the correct target enum type. So... Commented Aug 2, 2012 at 4:09
  • 1
    This will not compile. Error : 'int' is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter. Commented Aug 2, 2012 at 4:09
  • 4
    If T is always int then why to use generics? Commented Aug 2, 2012 at 4:10
  • public T Method<T>(XmlReader reader) where T : enum { string s = reader.GetAttribute("whatever"); int i = int.Parse(s); return (T)i; } Commented Aug 2, 2012 at 4:10

3 Answers 3

7

"Only class or interface could be specified as constraint." (c) ReSharper

int (Int32) is just a struct. You can constrain that T is a struct. but you can't use any struct as constraint.

the whole list of possible constraints you can find here - http://msdn.microsoft.com/en-us/library/d5x73970.aspx

UPD

and for Enum constraint see this question - Is there a workaround for generic type constraint of "special class" Enum in C# 3.0?

Sign up to request clarification or add additional context in comments.

3 Comments

A struct can also be used as a constraint. The following compiles: public class Generic<T> where T : struct { }. You can then instantiate the class as Generic<int> gen = new Generic<int>();.
@EricJ. you don't understand. we can constrain T to be "a struct", but we can't use any struct as constraint.
I see where you are coming from. As it stands, though, your answer is misleading. You in addition to struct you can also specify new as a constraint. Neither of those are a "class or interface".
4

int (and all other numeric types, and enums) cannot be used as a generic constraint.

See

Generic C# Code and the Plus Operator

for further details and options.

For a discussion with Anders Hejlsberg, the creator of C#, about generics and type constraints see

http://www.artima.com/intv/generics.html

One can place a type constraint of struct like this:

public class Generic<T> where T : struct { } Generic<int> gen = new Generic<int>(); 

1 Comment

Downvoter: What is wrong with this answer? Do share, so that we can all learn.
3

Are you sure its compiling?

Here, it gives following error:

error CS0701: 'int' is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.