473,321 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Generic Generics Problem 2

Hello everybody,

Here is another problem with generics I come across.

Let's say I want to implement a structure performing some operations on some
numeric type.
I can not than just return this expected type since the compiler is not sure
what this type would be, and it does not know that the type I am trying to
returned is the correct type, or either that the conversion is possible. Ok,
I understand - it is just the limitation of generics.

The solution I found was converting my type value to an object and then
downcasting from an object. That is I have to box and unbox my value type to
make the compiler happy paying significant performance price.

My question is: am I missing something? What sense does it make all
together, I mean placing restriction on conversion from my known type, that
is, assuming I may not know what I am doing, but allowing conversion from an
object. I just do not see any sense here.

Tomasz
Disclaimer: of course the attached example is just an example. This
particular functionality can be achieved in a different way.

sample usage:

int maxVal = MyGenericStruct<Int32>.GetMaxValue();

struct MyGenericStruct<T> where T : struct, IConvertible
{
public static T GetMaxValue()
{
TypeCode tc = default(T).GetTypeCode();

switch (tc) {
case TypeCode.Int32:
return (T)Int32.MaxValue; // causes compile error
// but this works:
object o = Int32.MaxValue;
return (T)o;
// [support for other types]
default:
throw new InvalidEnumArgumentException();
}
}
}
Nov 17 '05 #1
3 1203
I don't know of a way to solve your specific problem. However, all
discussions I've seen regarding generics imply that are basically
unusable for numerics. Sorry... you'll have to use traditional
overloading and/or multiple classes for each type.
--
http://www.kynosarges.de
Nov 17 '05 #2
Chris,

I do not think this problem is by any means limited to only numeric types.

Tomasz

"Christoph Nahr" <ch************@kynosarges.de> wrote in message
news:ur********************************@4ax.com...
I don't know of a way to solve your specific problem. However, all
discussions I've seen regarding generics imply that are basically
unusable for numerics. Sorry... you'll have to use traditional
overloading and/or multiple classes for each type.
--
http://www.kynosarges.de

Nov 17 '05 #3
Well, numerics is the only case I can think of where you have a bunch
of value types and frequently want to convert from one to another.
Structs aren't (or shouldn't be) used very often otherwise.

On Thu, 15 Sep 2005 15:04:00 GMT, "Tom Jastrzebski" <to*@tom.com>
wrote:
Chris,

I do not think this problem is by any means limited to only numeric types.

Tomasz

--
http://www.kynosarges.de
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
10
by: phancey | last post by:
I'm quite new to generics. I have 2 generic classes: MyClass<Tand MyOtherClass<T>. MyClass<Thas 2 public Add methods Add(MyOtherClass<T>); Add(MyOtherClass<Wrapper<T>>); (Wrapper<Tis another...
8
by: MMAS | last post by:
Hey everyone -- Curious about some strange behaviour I'm seeing that seems to be related to my lack of understanding on how generics work in C#. Here's some simplified code (sorry for strange...
11
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.