Generics limitation on .Net

  • Thread starter Thread starter Guest
  • Start date Start date
Joanna,

I felt angry because of such remarks :

- "Tell me, what are you doing that requires such super-optimised code ? Most
business applications are only really as fast as the person typing at the
keyboard"
==> My question was 'I need speed', and your answer is "Why, nobody cares
about speed ?"

- "If speed is that much of an issue, then don't write code that invokes so
much boxing"
==> That was the purpose of my question : how to avoid so much boxing ?

- "The example that you give doesn't really seem to fit the generic
programming model"
==> You first denigrate my work and experience based on an silly example
without knowing the work I'm doing and my needs

Now, I apologize if my provious message hurts you, and I sincerly thanks you
for the time you gave me answering my messages (I really think that).

Regards
Luc
 
"Luc Vaillant" <[email protected]> a écrit dans le
message de news: (e-mail address removed)...

| Now, I apologize if my provious message hurts you, and I sincerly thanks
you
| for the time you gave me answering my messages (I really think that).

De rien :-)

Joanna
 
In the context of the OPs question: I would stay with the cast over convert,
as Convert.ChangeType "requires" the class implement IConvertible, or at the
very least Convert.ChangeType needs to know the conversion.

The cast doesn't care what the value implements...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| "Jay B. Harlow [MVP - Outlook]" <[email protected]> a écrit
dans
| le message de OQ1J9X%[email protected]...
|
|| You need to cast to object first, as Char to T is not defined. Without a
|| constraint only object to/from T is defined.
||
||| valueType = (T)(object)Char.MinValue;
|
| You can also use the Convert class :
|
| T value = (T) Convert.ChangeType(Char.MinValue, typeof(T))
|
| Joanna
|
| --
| Joanna Carter [TeamB]
| Consultant Software Engineer
|
|
 
| 1) How do I initialize a T value without doing boxing/unboxing ?

| Not possible, must use T value = (T)(Object)constant value
| even if I know that the constant value if of type T
Boxes the value! Remember (object) says to box value types!

You have two options on initializing T values without boxing:

1. default(T)
2. Parameterized constructor

NOTE: The reflection trick I showed earlier will box values.

| 2) How do I compare two T values without boxing/unboxing ?
Constrain T by IComparable<T>

http://msdn2.microsoft.com/en-us/library/4d7sx9hd.aspx

NOTE: System.IComparable<T> is different then System.IComparable!

In the case of simple equality, I would recommend constraining T by
IEquatable<T>.

http://msdn2.microsoft.com/en-us/library/ms131187(en-US,VS.80).aspx

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Is that an answer ?
|
| I'm not writing a keyboard typing application, and I DO CARE ABOUT SPEED.
|
| > If speed is that much of an issue, then don't write code that invokes so
| > much boxing
|
| That's why I wanted to use Generics...
|
| > The example that you give doesn't really seem to fit the
| > generic programming model due to the possible differences in behaviour
| > required by the different types that may be used
|
| The example I gave was just to demonstrate the initialization issue. It
was
| not a real use case...
| Same remark about the 'Generics limitation' post I sent today. The example
| is to desmonstrate the != operator issue with generics, and this is not my
| real program...
|
| Why don't you try to answer my questions instead of explaining me that
most
| programs don't need speed, and that my examples don't "really seem to fit
the
| generic programming model".
|
| My questions are simple:
|
| 1) How do I initialize a T value without doing boxing/unboxing ?
|
| The answer is:
| Not possible, must use T value = (T)(Object)constant value
| even if I know that the constant value if of type T
|
| 2) How do I compare two T values without boxing/unboxing ?
|
| The answer is:
| Not possible from the answers I got in this post
|
|
| Regards
| Luc
|
|
| "Joanna Carter [TeamB]" wrote:
|
| > "Luc Vaillant" <[email protected]> a écrit dans le
| > message de news: (e-mail address removed)...
| >
| > | This will also box Char.MinValue into Object
| >
| > I won't deny that :-)
| >
| > Tell me, what are you doing that requires such super-optimised code ?
Most
| > business applications are only really as fast as the person typing at
the
| > keyboard.
| >
| > If speed is that much of an issue, then don't write code that invokes so
| > much boxing. The example that you give doesn't really seem to fit the
| > generic programming model due to the possible differences in behaviour
| > required by the different types that may be used. In which case, I would
| > tend to look elsewhere for a solution; generics are only really useful
when
| > the behaviour for *all* anticipated types is *identical*.
| >
| > Joanna
| >
| > --
| > Joanna Carter [TeamB]
| > Consultant Software Engineer
| >
| >
| >
 

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

Similar Threads


Back
Top