operator overload for generic methods so I can do == and ++

N

nickdu

I've got a method that I would like to turn into a generic that accepts
either an int or a long. However in the method I would like to:

a. test an instance of the generic type against 0 (or default(T))

and

b. increment an instance of the generic type.

Something like:

public T[] GetIds<T>(string[] names)
{
...
T id;
...
if (id == (T) 0)
...

...
id++;
...
}

This doesn't work. Is there a prescribed way to do this? I'm guessing I
have to create a new templated class which will take as a parameter this int
or long and provide the operator overloads on those along with the casts
operator.
--
Thanks,
Nick

(e-mail address removed)
remove "nospam" change community. to msn.com
 
Z

Zhi-Xin Ye [MSFT]

Hello Nick,

Thank you for using Microsoft Managed Newsgroup Service, I'm Zhi-Xin Ye,
it's my pleasure to work witth you on this issue.

As I understand, you want to create a generic method which can compare the
instance of the generic type to 0, and can increase the generic type.

If you just want to accept int or long type in the generic method, it would
be easier to creat two methods instead, one for the int type and the other
for the long type.

Anyway, you can define and execute a dynamic method to increase the value
of the generic type instance. I write the following quick sample for your
information:

class Class1
{
static void Main(string[] args)
{
int i = GetIds<int>();

Console.WriteLine(i);

long b = GetIds<long>();
Console.WriteLine(b);

Console.ReadLine();
}

private delegate T IncreaseDelegate<T>(T t, int seed);

public static T GetIds<T>() where T: new()
{
T id = new T();

if ((id is int) || (id is long))
{
if ((id is int) && id.Equals(0))
{
Console.WriteLine("The value is zero!");
}
if ((id is long) && id.Equals((long)0))
{
Console.WriteLine("The value is zero!");
}

Type[] args = { typeof(T), typeof(int) };

DynamicMethod incMethod = new DynamicMethod(
"Increase",
typeof(T),
args);

ILGenerator generator = incMethod.GetILGenerator();

generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldarg_1);

if (id is long)
{
//Convert the seed from int to long type
generator.Emit(OpCodes.Conv_I8);
}

generator.Emit(OpCodes.Add);
generator.Emit(OpCodes.Ret);

IncreaseDelegate<T> incDel =

(IncreaseDelegate<T>)incMethod.CreateDelegate(typeof(IncreaseDelegate<T>));


id = incDel(id, 1);
}

return id;
}
}

For more information about dynamic methods in C#, you can refer to this
document:

How to: Define and Execute Dynamic Methods
http://msdn.microsoft.com/en-us/library/exczf7b9.aspx

For more information about operator overloading with generics, I would
recommend you read this article:

Operator Overloading with Generics
http://www.codeproject.com/KB/cs/genericoperators.aspx

For more information about generic in C#, you can refer to this document:

Generics (C# Programming Guide)
http://msdn.microsoft.com/en-us/library/512aeb7t.aspx

If you have any questions or concerns, please feel free to let me know. I
will be happy to be of assistance.

Have a nice day!

Best Regards,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top