Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typing generic values (C#)

Tags:

c#

generics

When I try this with a generic class where this.value is T:

if (this.value.GetType() == typeof(int))
{
    ((int)this.value)++;
}
else
{
    throw new InvalidOperationException
            ("T must be an int to perform this operation");
}

I get a compile-time error: "Cannot convert type 'T' to 'int'"

What should I do to perform an integral operation on this.value when it's an int?

Note that this is just an example. The code does type conversions with generics, and "int" is just an example of one type for T.

like image 316
core Avatar asked Oct 05 '08 20:10

core


People also ask

What is generic data type in C?

Unlike C++ and Java, C doesn't support generics. How to create a linked list in C that can be used for any data type? In C, we can use a void pointer and a function pointer to implement the same functionality. The great thing about void pointer is it can be used to point to any data type.

Can you declare a variable with a generic type?

Yes it is possible, but only for Functions, not any arbitrary variable. As you can see, it's the type itself, where you define generics and then you can make a variable of that type, which allows it to set the generic.

What is generic datatype?

Data types from the source and target systems you use are mapped to and mapped from a core set of generic data types in Oracle Cloud Infrastructure Data Integration.

When to use generics C sharp?

Generics allow you to define the specification of the data type of programming elements in a class or a method, until it is actually used in the program. In other words, generics allow you to write a class or method that can work with any data type.


1 Answers

Unfortunately, it is very hard to convince the compiler about specific T implementations. One (nasty) approach is to cast to object in the middle (note this will box and unbox value-types):

int i = (int)(object)this.value;
i++;
this.value = (T)(object)i;

Ugly but it works. In .NET 3.5 I have some better wrappers for generic arithmetic, here. The Operator class is part of MiscUtil; at the simplest level, I suspect AddAlternative would work very well:

this.value = Operator.AddAlternative(this.value, 1);

This should infer the implicit <T,int> automatically, or you can add them yourself:

this.value = Operator.AddAlternative<T,int>(this.value, 1);

Benefit: This is preferable to the original code as it doesn't actually care about the original T - it will work for any type (even your own) that supports "T +(T,int)".

I think there is also a ChangeType hiding around somewhere in there...

[edit] Collin K and others make a valid remark about the architectural implications - but being pragmatic there are times when the T really does matter that much... but I'd agree with avoiding this type of specialization unless really necessary. That said (as per my comment on Collin's post), the ability to perform things like basic arithmetic (increment, Int32 division, etc) on (for example) a Matrix<T> [for T in decimal/float/int/double/etc] is often highly valuable.

like image 173
Marc Gravell Avatar answered Sep 30 '22 12:09

Marc Gravell