Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# generic method with integer constraint refuses to cast from integer to the generic type

Tags:

c#

generics

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.

like image 474
Phil Wright Avatar asked Aug 02 '12 04:08

Phil Wright


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


3 Answers

"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?

like image 99
Dmitry Khryukin Avatar answered Oct 22 '22 10:10

Dmitry Khryukin


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>();
like image 4
Eric J. Avatar answered Oct 22 '22 10:10

Eric J.


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.

like image 2
logicnp Avatar answered Oct 22 '22 10:10

logicnp