Generics limitation on .Net

G

Guest

I need to initialise a typed parameter depending of its type in a generic
class.
I have tried to use the C++ template form as follow, but it doesn't work.
It seems to be a limitation of generics vs C++ templates.
Does anyone knows a workaround to do this ? Thx :

public class C<T>
{
private T myValue;

private void InitializeValue(out Byte value) { value = Byte.MinValue; }
private void InitializeValue(out Char value) { value = Char.MinValue; }
private void InitializeValue(out Int16 value) { value = Int16.MinValue; }
// ... and so on...
private void InitializeValue(out String value) { value = "Initial value"; }
private void InitializeValue(out Object value) { value = this; }

public C()
{
InitializeValue(out myValue);
}

// ...
}
 
J

Joanna Carter [TeamB]

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

|I need to initialise a typed parameter depending of its type in a generic
| class.
| I have tried to use the C++ template form as follow, but it doesn't work.
| It seems to be a limitation of generics vs C++ templates.
| Does anyone knows a workaround to do this ? Thx :
|
| public class C<T>
| {
| private T myValue;
|
| private void InitializeValue(out Byte value) { value = Byte.MinValue; }
| private void InitializeValue(out Char value) { value = Char.MinValue; }
| private void InitializeValue(out Int16 value) { value =
Int16.MinValue; }
| // ... and so on...
| private void InitializeValue(out String value) { value = "Initial
value"; }
| private void InitializeValue(out Object value) { value = this; }
|
| public C()
| {
| InitializeValue(out myValue);
| }
|
| // ...
| }

You are not specifying the type that you wish to initialise, to the
constructor.

In any case, you can use the 'default' keyword to initialise any type passed
as the parameter to the generic class :Then you don't even have to declare a
constructor for this purpose.

public class C<T>
{
private T myValue = default(T);
}

Joanna
 
G

Guest

Yes, this is for the default value, but what if I want to initialize my
variable with a value that is not the default one ?
 
N

Nicholas Paldino [.NET/C# MVP]

Then you will have to do a type comparison on T and then set your value
based on the type of T.
 
J

Joanna Carter [TeamB]

"Nicholas Paldino [.NET/C# MVP]" <[email protected]> a écrit
dans le message de news: (e-mail address removed)...

| Then you will have to do a type comparison on T and then set your value
| based on the type of T.

Hehe, you beat me to that one :)

Joanna
 
G

Guest

I have already tried that, but it didn't work (for the same reasons):

public class C<T>
{
private T myValue;

private void InitializeValue(out T value)
{
if (valeurType.GetType() == typeof(Byte))
valeurType = Byte.MinValue;
else if (valeurType.GetType() == typeof(Char))
valeurType = Char.MinValue;
else if (valeurType.GetType() == typeof(Int16))
valeurType = Int16.MinValue;
// and so on...
else
valeurType = default(T);
}

public C()
{
InitializeValue(out myValue);
}

// ...
}

Error messages are :
Error 1 Cannot implicitly convert type 'byte' to 'T'
Error 2 Cannot implicitly convert type 'char' to 'T'
Error 3 Cannot implicitly convert type 'short' to 'T'

and so on...



Nicholas Paldino said:
Then you will have to do a type comparison on T and then set your value
based on the type of T.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Luc Vaillant said:
Yes, this is for the default value, but what if I want to initialize my
variable with a value that is not the default one ?
 
N

Nicholas Paldino [.NET/C# MVP]

Luc,

You have to cast the result of the call of <type>.MinValue to T, like
so:

valueType = (T) Char.MinValue;


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Luc Vaillant said:
I have already tried that, but it didn't work (for the same reasons):

public class C<T>
{
private T myValue;

private void InitializeValue(out T value)
{
if (valeurType.GetType() == typeof(Byte))
valeurType = Byte.MinValue;
else if (valeurType.GetType() == typeof(Char))
valeurType = Char.MinValue;
else if (valeurType.GetType() == typeof(Int16))
valeurType = Int16.MinValue;
// and so on...
else
valeurType = default(T);
}

public C()
{
InitializeValue(out myValue);
}

// ...
}

Error messages are :
Error 1 Cannot implicitly convert type 'byte' to 'T'
Error 2 Cannot implicitly convert type 'char' to 'T'
Error 3 Cannot implicitly convert type 'short' to 'T'

and so on...



Nicholas Paldino said:
Then you will have to do a type comparison on T and then set your
value
based on the type of T.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Luc Vaillant said:
Yes, this is for the default value, but what if I want to initialize my
variable with a value that is not the default one ?

:

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

|I need to initialise a typed parameter depending of its type in a
generic
| class.
| I have tried to use the C++ template form as follow, but it doesn't
work.
| It seems to be a limitation of generics vs C++ templates.
| Does anyone knows a workaround to do this ? Thx :
|
| public class C<T>
| {
| private T myValue;
|
| private void InitializeValue(out Byte value) { value =
Byte.MinValue; }
| private void InitializeValue(out Char value) { value =
Char.MinValue; }
| private void InitializeValue(out Int16 value) { value =
Int16.MinValue; }
| // ... and so on...
| private void InitializeValue(out String value) { value = "Initial
value"; }
| private void InitializeValue(out Object value) { value = this; }
|
| public C()
| {
| InitializeValue(out myValue);
| }
|
| // ...
| }

You are not specifying the type that you wish to initialise, to the
constructor.

In any case, you can use the 'default' keyword to initialise any type
passed
as the parameter to the generic class :Then you don't even have to
declare a
constructor for this purpose.

public class C<T>
{
private T myValue = default(T);
}

Joanna
 
J

Jay B. Harlow [MVP - Outlook]

Luc,
One way might be to cast the value to an object, then cast it to T,
something like:

| if (valeurType.GetType() == typeof(Byte))
| valeurType = (T)(object)Byte.MinValue;

If you know you want "MinValue" I would consider using Reflection to find
the "MinValue" field on the respective type, something like:

private void InitializeValue(out T value)
{
Type t = typeof(T);
System.Reflection.FieldInfo fi = t.GetField("MinValue");
if (fi == null)
{
value = default(T);
}
else
{
value = (T)fi.GetValue(null);
}
}

This helps ensure that C<T> supports "MinValue" on new types that may be
used with it, without modifying C<T> itself.

NOTE: The above routine assumes that MinValue is a static field...


Although I would define the class such that default(T) was acceptable or
used another field to indicate the special value...

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


|I have already tried that, but it didn't work (for the same reasons):
|
| public class C<T>
| {
| private T myValue;
|
| private void InitializeValue(out T value)
| {
| if (valeurType.GetType() == typeof(Byte))
| valeurType = Byte.MinValue;
| else if (valeurType.GetType() == typeof(Char))
| valeurType = Char.MinValue;
| else if (valeurType.GetType() == typeof(Int16))
| valeurType = Int16.MinValue;
| // and so on...
| else
| valeurType = default(T);
| }
|
| public C()
| {
| InitializeValue(out myValue);
| }
|
| // ...
| }
|
| Error messages are :
| Error 1 Cannot implicitly convert type 'byte' to 'T'
| Error 2 Cannot implicitly convert type 'char' to 'T'
| Error 3 Cannot implicitly convert type 'short' to 'T'
|
| and so on...
|
|
|
| "Nicholas Paldino [.NET/C# MVP]" wrote:
|
| > Then you will have to do a type comparison on T and then set your
value
| > based on the type of T.
| >
| >
| > --
| > - Nicholas Paldino [.NET/C# MVP]
| > - (e-mail address removed)
| >
| > | > > Yes, this is for the default value, but what if I want to initialize
my
| > > variable with a value that is not the default one ?
| > >
| > > "Joanna Carter [TeamB]" wrote:
| > >
| > >> "Luc Vaillant" <[email protected]> a écrit dans
le
| > >> message de (e-mail address removed)...
| > >>
| > >> |I need to initialise a typed parameter depending of its type in a
| > >> generic
| > >> | class.
| > >> | I have tried to use the C++ template form as follow, but it doesn't
| > >> work.
| > >> | It seems to be a limitation of generics vs C++ templates.
| > >> | Does anyone knows a workaround to do this ? Thx :
| > >> |
| > >> | public class C<T>
| > >> | {
| > >> | private T myValue;
| > >> |
| > >> | private void InitializeValue(out Byte value) { value =
| > >> Byte.MinValue; }
| > >> | private void InitializeValue(out Char value) { value =
| > >> Char.MinValue; }
| > >> | private void InitializeValue(out Int16 value) { value =
| > >> Int16.MinValue; }
| > >> | // ... and so on...
| > >> | private void InitializeValue(out String value) { value = "Initial
| > >> value"; }
| > >> | private void InitializeValue(out Object value) { value = this; }
| > >> |
| > >> | public C()
| > >> | {
| > >> | InitializeValue(out myValue);
| > >> | }
| > >> |
| > >> | // ...
| > >> | }
| > >>
| > >> You are not specifying the type that you wish to initialise, to the
| > >> constructor.
| > >>
| > >> In any case, you can use the 'default' keyword to initialise any type
| > >> passed
| > >> as the parameter to the generic class :Then you don't even have to
| > >> declare a
| > >> constructor for this purpose.
| > >>
| > >> public class C<T>
| > >> {
| > >> private T myValue = default(T);
| > >> }
| > >>
| > >> Joanna
| > >>
| > >> --
| > >> Joanna Carter [TeamB]
| > >> Consultant Software Engineer
| > >>
| > >>
| > >>
| >
| >
| >
 
J

Jay B. Harlow [MVP - Outlook]

Nicholas,
| valueType = (T) Char.MinValue;
Produces a compile error!

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;

Even then a runtime error could occur...

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


message | Luc,
|
| You have to cast the result of the call of <type>.MinValue to T, like
| so:
|
| valueType = (T) Char.MinValue;
|
|
| --
| - Nicholas Paldino [.NET/C# MVP]
| - (e-mail address removed)
|
| | >I have already tried that, but it didn't work (for the same reasons):
| >
| > public class C<T>
| > {
| > private T myValue;
| >
| > private void InitializeValue(out T value)
| > {
| > if (valeurType.GetType() == typeof(Byte))
| > valeurType = Byte.MinValue;
| > else if (valeurType.GetType() == typeof(Char))
| > valeurType = Char.MinValue;
| > else if (valeurType.GetType() == typeof(Int16))
| > valeurType = Int16.MinValue;
| > // and so on...
| > else
| > valeurType = default(T);
| > }
| >
| > public C()
| > {
| > InitializeValue(out myValue);
| > }
| >
| > // ...
| > }
| >
| > Error messages are :
| > Error 1 Cannot implicitly convert type 'byte' to 'T'
| > Error 2 Cannot implicitly convert type 'char' to 'T'
| > Error 3 Cannot implicitly convert type 'short' to 'T'
| >
| > and so on...
| >
| >
| >
| > "Nicholas Paldino [.NET/C# MVP]" wrote:
| >
| >> Then you will have to do a type comparison on T and then set your
| >> value
| >> based on the type of T.
| >>
| >>
| >> --
| >> - Nicholas Paldino [.NET/C# MVP]
| >> - (e-mail address removed)
| >>
| >> | >> > Yes, this is for the default value, but what if I want to initialize
my
| >> > variable with a value that is not the default one ?
| >> >
| >> > "Joanna Carter [TeamB]" wrote:
| >> >
| >> >> "Luc Vaillant" <[email protected]> a écrit dans
le
| >> >> message de (e-mail address removed)...
| >> >>
| >> >> |I need to initialise a typed parameter depending of its type in a
| >> >> generic
| >> >> | class.
| >> >> | I have tried to use the C++ template form as follow, but it
doesn't
| >> >> work.
| >> >> | It seems to be a limitation of generics vs C++ templates.
| >> >> | Does anyone knows a workaround to do this ? Thx :
| >> >> |
| >> >> | public class C<T>
| >> >> | {
| >> >> | private T myValue;
| >> >> |
| >> >> | private void InitializeValue(out Byte value) { value =
| >> >> Byte.MinValue; }
| >> >> | private void InitializeValue(out Char value) { value =
| >> >> Char.MinValue; }
| >> >> | private void InitializeValue(out Int16 value) { value =
| >> >> Int16.MinValue; }
| >> >> | // ... and so on...
| >> >> | private void InitializeValue(out String value) { value =
"Initial
| >> >> value"; }
| >> >> | private void InitializeValue(out Object value) { value = this; }
| >> >> |
| >> >> | public C()
| >> >> | {
| >> >> | InitializeValue(out myValue);
| >> >> | }
| >> >> |
| >> >> | // ...
| >> >> | }
| >> >>
| >> >> You are not specifying the type that you wish to initialise, to the
| >> >> constructor.
| >> >>
| >> >> In any case, you can use the 'default' keyword to initialise any
type
| >> >> passed
| >> >> as the parameter to the generic class :Then you don't even have to
| >> >> declare a
| >> >> constructor for this purpose.
| >> >>
| >> >> public class C<T>
| >> >> {
| >> >> private T myValue = default(T);
| >> >> }
| >> >>
| >> >> Joanna
| >> >>
| >> >> --
| >> >> Joanna Carter [TeamB]
| >> >> Consultant Software Engineer
| >> >>
| >> >>
| >> >>
| >>
| >>
| >>
|
|
 
N

Nicholas Paldino [.NET/C# MVP]

Good point! =)

You think that people would buy newsreaders with compilers built into
them? =)

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jay B. Harlow said:
Nicholas,
| valueType = (T) Char.MinValue;
Produces a compile error!

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;

Even then a runtime error could occur...

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


in
message | Luc,
|
| You have to cast the result of the call of <type>.MinValue to T, like
| so:
|
| valueType = (T) Char.MinValue;
|
|
| --
| - Nicholas Paldino [.NET/C# MVP]
| - (e-mail address removed)
|
| | >I have already tried that, but it didn't work (for the same reasons):
| >
| > public class C<T>
| > {
| > private T myValue;
| >
| > private void InitializeValue(out T value)
| > {
| > if (valeurType.GetType() == typeof(Byte))
| > valeurType = Byte.MinValue;
| > else if (valeurType.GetType() == typeof(Char))
| > valeurType = Char.MinValue;
| > else if (valeurType.GetType() == typeof(Int16))
| > valeurType = Int16.MinValue;
| > // and so on...
| > else
| > valeurType = default(T);
| > }
| >
| > public C()
| > {
| > InitializeValue(out myValue);
| > }
| >
| > // ...
| > }
| >
| > Error messages are :
| > Error 1 Cannot implicitly convert type 'byte' to 'T'
| > Error 2 Cannot implicitly convert type 'char' to 'T'
| > Error 3 Cannot implicitly convert type 'short' to 'T'
| >
| > and so on...
| >
| >
| >
| > "Nicholas Paldino [.NET/C# MVP]" wrote:
| >
| >> Then you will have to do a type comparison on T and then set your
| >> value
| >> based on the type of T.
| >>
| >>
| >> --
| >> - Nicholas Paldino [.NET/C# MVP]
| >> - (e-mail address removed)
| >>
message
| >> | >> > Yes, this is for the default value, but what if I want to
initialize
my
| >> > variable with a value that is not the default one ?
| >> >
| >> > "Joanna Carter [TeamB]" wrote:
| >> >
| >> >> "Luc Vaillant" <[email protected]> a écrit
dans
le
| >> >> message de (e-mail address removed)...
| >> >>
| >> >> |I need to initialise a typed parameter depending of its type in a
| >> >> generic
| >> >> | class.
| >> >> | I have tried to use the C++ template form as follow, but it
doesn't
| >> >> work.
| >> >> | It seems to be a limitation of generics vs C++ templates.
| >> >> | Does anyone knows a workaround to do this ? Thx :
| >> >> |
| >> >> | public class C<T>
| >> >> | {
| >> >> | private T myValue;
| >> >> |
| >> >> | private void InitializeValue(out Byte value) { value =
| >> >> Byte.MinValue; }
| >> >> | private void InitializeValue(out Char value) { value =
| >> >> Char.MinValue; }
| >> >> | private void InitializeValue(out Int16 value) { value =
| >> >> Int16.MinValue; }
| >> >> | // ... and so on...
| >> >> | private void InitializeValue(out String value) { value =
"Initial
| >> >> value"; }
| >> >> | private void InitializeValue(out Object value) { value =
this; }
| >> >> |
| >> >> | public C()
| >> >> | {
| >> >> | InitializeValue(out myValue);
| >> >> | }
| >> >> |
| >> >> | // ...
| >> >> | }
| >> >>
| >> >> You are not specifying the type that you wish to initialise, to
the
| >> >> constructor.
| >> >>
| >> >> In any case, you can use the 'default' keyword to initialise any
type
| >> >> passed
| >> >> as the parameter to the generic class :Then you don't even have to
| >> >> declare a
| >> >> constructor for this purpose.
| >> >>
| >> >> public class C<T>
| >> >> {
| >> >> private T myValue = default(T);
| >> >> }
| >> >>
| >> >> Joanna
| >> >>
| >> >> --
| >> >> Joanna Carter [TeamB]
| >> >> Consultant Software Engineer
| >> >>
| >> >>
| >> >>
| >>
| >>
| >>
|
|
 
J

Jay B. Harlow [MVP - Outlook]

| You think that people would buy newsreaders with compilers built into
| them? =)
They might! There's been days when I would find it helpful...

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


message | Good point! =)
|
| You think that people would buy newsreaders with compilers built into
| them? =)
|
| --
| - Nicholas Paldino [.NET/C# MVP]
| - (e-mail address removed)
|
<<snip>>
 
G

Guest

Thanks a lot for your answer,

cast value->Object->T works fine.

I don't need the MinValue, it was just for the example.
But Reflexion is a good idea, I will keep it in mind

Thanks again
Luc


Jay B. Harlow said:
Luc,
One way might be to cast the value to an object, then cast it to T,
something like:

| if (valeurType.GetType() == typeof(Byte))
| valeurType = (T)(object)Byte.MinValue;

If you know you want "MinValue" I would consider using Reflection to find
the "MinValue" field on the respective type, something like:

private void InitializeValue(out T value)
{
Type t = typeof(T);
System.Reflection.FieldInfo fi = t.GetField("MinValue");
if (fi == null)
{
value = default(T);
}
else
{
value = (T)fi.GetValue(null);
}
}

This helps ensure that C<T> supports "MinValue" on new types that may be
used with it, without modifying C<T> itself.

NOTE: The above routine assumes that MinValue is a static field...


Although I would define the class such that default(T) was acceptable or
used another field to indicate the special value...

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


|I have already tried that, but it didn't work (for the same reasons):
|
| public class C<T>
| {
| private T myValue;
|
| private void InitializeValue(out T value)
| {
| if (valeurType.GetType() == typeof(Byte))
| valeurType = Byte.MinValue;
| else if (valeurType.GetType() == typeof(Char))
| valeurType = Char.MinValue;
| else if (valeurType.GetType() == typeof(Int16))
| valeurType = Int16.MinValue;
| // and so on...
| else
| valeurType = default(T);
| }
|
| public C()
| {
| InitializeValue(out myValue);
| }
|
| // ...
| }
|
| Error messages are :
| Error 1 Cannot implicitly convert type 'byte' to 'T'
| Error 2 Cannot implicitly convert type 'char' to 'T'
| Error 3 Cannot implicitly convert type 'short' to 'T'
|
| and so on...
|
|
|
| "Nicholas Paldino [.NET/C# MVP]" wrote:
|
| > Then you will have to do a type comparison on T and then set your
value
| > based on the type of T.
| >
| >
| > --
| > - Nicholas Paldino [.NET/C# MVP]
| > - (e-mail address removed)
| >
| > | > > Yes, this is for the default value, but what if I want to initialize
my
| > > variable with a value that is not the default one ?
| > >
| > > "Joanna Carter [TeamB]" wrote:
| > >
| > >> "Luc Vaillant" <[email protected]> a écrit dans
le
| > >> message de (e-mail address removed)...
| > >>
| > >> |I need to initialise a typed parameter depending of its type in a
| > >> generic
| > >> | class.
| > >> | I have tried to use the C++ template form as follow, but it doesn't
| > >> work.
| > >> | It seems to be a limitation of generics vs C++ templates.
| > >> | Does anyone knows a workaround to do this ? Thx :
| > >> |
| > >> | public class C<T>
| > >> | {
| > >> | private T myValue;
| > >> |
| > >> | private void InitializeValue(out Byte value) { value =
| > >> Byte.MinValue; }
| > >> | private void InitializeValue(out Char value) { value =
| > >> Char.MinValue; }
| > >> | private void InitializeValue(out Int16 value) { value =
| > >> Int16.MinValue; }
| > >> | // ... and so on...
| > >> | private void InitializeValue(out String value) { value = "Initial
| > >> value"; }
| > >> | private void InitializeValue(out Object value) { value = this; }
| > >> |
| > >> | public C()
| > >> | {
| > >> | InitializeValue(out myValue);
| > >> | }
| > >> |
| > >> | // ...
| > >> | }
| > >>
| > >> You are not specifying the type that you wish to initialise, to the
| > >> constructor.
| > >>
| > >> In any case, you can use the 'default' keyword to initialise any type
| > >> passed
| > >> as the parameter to the generic class :Then you don't even have to
| > >> declare a
| > >> constructor for this purpose.
| > >>
| > >> public class C<T>
| > >> {
| > >> private T myValue = default(T);
| > >> }
| > >>
| > >> Joanna
| > >>
| > >> --
| > >> Joanna Carter [TeamB]
| > >> Consultant Software Engineer
| > >>
| > >>
| > >>
| >
| >
| >
 
J

Joanna Carter [TeamB]

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

Guest

You can also use the Convert class :
T value = (T) Convert.ChangeType(Char.MinValue, typeof(T))

This will also box Char.MinValue into Object


Joanna Carter said:
"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
 
J

Joanna Carter [TeamB]

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

Guest

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
 
J

Joanna Carter [TeamB]

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

| Is that an answer ?

Sometimes answers need to include further questions :)

Sometimes, a good argument can help to avoid following "les poissons rouges"

| I'm not writing a keyboard typing application, and I DO CARE ABOUT SPEED.

No need to shout, such things are not always apparent.

| That's why I wanted to use Generics...

What made you think that generics would necessarily be fast ?

| 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...

As I said, your particular requirements may indicate that generics are not
necessarily the best direction. If you wanted to set T to its true default,
then that is what the default(...) function is there for.

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

Because, from years of experience in OO, I felt that you were going down a
direction simply because it seemed right but not necessarily wise. You are,
of course, welcome to accept or discard any advice given; it comes without
malice or antagonism.

| 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

The answer is to understand that generics provides a mechanism, default(T),
which you feel is not adequate, therefore, you may have to either accept the
shortcomings of the alternatives like boxing, or choose an alternative to
generics.

If the types you are going to use are within your control, then you could
always have them implement a common interface with a method like
Initialise().

| 2) How do I compare two T values without boxing/unboxing ?
|
| The answer is:
| Not possible from the answers I got in this post

The only other alternative I could think of was to add the IComparable<T>
constraint to the generic class, allowing you to use :

{
int result = CompareTo(T other);
}

That is, assuming that your T types support IComparable<T>.

Joanna
 
G

Guest

Ok, I'm bored feeding the troll...
Because, from years of experience in OO, I felt that you were going down a
direction simply because it seemed right but not necessarily wise. You are,
of course, welcome to accept or discard any advice given; it comes without
malice or antagonism.

With your "years of experience in OO", you should felt that answer :

class MyComp : IComparer<short>, IComparer<int>, IComparer<long>
{
public int Compare(int x, int y) { return x - y; }
public int Compare(short x, short y) { return x - y; }
public int Compare(long x, long y) { return x - y; }
}

class GenClass<T, I> where I : IComparer<T>, new()
{
private IComparer<T> comparer = new I();

public GenClass(T value1, T value2)
{
// THIS IS AN EXAMPLE, NOT MY REAL PROGRAM !!!
// (I'M NOT SHOUTING..., IT'S JUST A WARNING...)
if (comparer.Compare(value1, value2) == 0)
Console.WriteLine("value1 equals value2");
}
}

class Program
{
static void Main(string[] args)
{
// THIS IS AN EXAMPLE TOO, NOT MY REAL PROGRAM !!!
GenClass<short, MyComp> A = new GenClass<short, MyComp>(123, 123);
GenClass<int, MyComp> B = new GenClass<int, MyComp>(123, 123);
GenClass<long, MyComp> B = new GenClass<long, MyComp>(123, 123);
}
}
What made you think that generics would necessarily be fast ?
Have you ever written or used generics ? Is Generics just a search/replace
tool for you ?

"In addition to type safety, generic collection types generally perform
better for storing and manipulating value types because there is no need to
box the value types." taken from
"ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxfund/html/6b90f9c3-eee3-4cd1-98d6-82019c83a1df.htm" :

Regards
Luc
 
J

Joanna Carter [TeamB]

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

| Ok, I'm bored feeding the troll...

I'm sorry you feel that way. I have never "trolled" newsgroups; had I done
so, I doubt that I would have been appointed as a newsgroup mediator for
Borland. You obviously have a problem with people who don't give you advice
or don't have opinions with which you agree.

| With your "years of experience in OO", you should felt that answer :

Hey, I am officially on holiday for Christmas. Just because I don't come up
with the right answer first time, doesn't give you the right to denegrate my
experience. If I don't understand your question fully the first ime around,
it may be that I am working on more than just solving your problems; like
writing articles for people that do appreciate the time and effort that goes
into trying to help others understand some pretty complicated stuff.

| Have you ever written or used generics ? Is Generics just a search/replace
| tool for you ?
|
| "In addition to type safety, generic collection types generally perform
| better for storing and manipulating value types because there is no need
to
| box the value types." taken from
|
"ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxfund/html/6b90f9c3-eee3-4cd1-98d6-82019c83a1df.htm"
:

It seems it takes a troll to think they have found an other troll :) I find
your attitude to be insulting.

So, does the IComparer solve your problem ?

Have a peaceful New Year.

Joanna
 

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

Generics limitation... 13
Polymorphic generics 4
Generics 5
IComparable and generics 2
Operator overload for generics 6
How to use generics? 4
Anonymous Types, LINQ and Generics 3
Generics Issue with Simple Factory 1

Top