473,386 Members | 1,924 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,386 software developers and data experts.

Is this a reasonable way to use generics?

All:

I have an interesting problem in front of me. does this sound reasonable,
or ridiculous?

I have to build something that is sort of like a style sheet for Windows
controls.
Picture a collection of dissimilar value types: a couple of Color types,
some ints, some strings etc.

I _could_ just use a hashtable of object to contain all of these, but since
they are value types, they would be boxed.

But what if I did this: (warning the syntax probably isn't spot on)
public class Element<T>
{
public T Value;
public Element(T value)
{
this.Value = value;
}
}

And then stored the element objects in my collection:
So, here is how I would store a couple of properties:
{
HashTable styleElements = new HashTable();
styleElements.Add("BackgroundColor", new Element<Color>(Color.Blue);
styleElements.Add("FontName", new Element<string>("Arial");
// and so on

//now accessing the collection:
aControl.BackgroundColor =
((Element<Color>)styleElements["BackgroundColor"]).Value;
}

Is this a horrible, horrible idea, or is it preferable to directly storing
the value types in the hashtable (thereby boxing them)?
Nov 17 '05 #1
5 1270
It seems to me that you just made the boxing explicit (and probably less
efficient than the implicit one).

/LM

"J.Marsch" <jm*****@newsgroup.nospam> wrote in message
news:ey**************@tk2msftngp13.phx.gbl...
All:

I have an interesting problem in front of me. does this sound reasonable,
or ridiculous?

I have to build something that is sort of like a style sheet for Windows
controls.
Picture a collection of dissimilar value types: a couple of Color types,
some ints, some strings etc.

I _could_ just use a hashtable of object to contain all of these, but
since they are value types, they would be boxed.

But what if I did this: (warning the syntax probably isn't spot on)
public class Element<T>
{
public T Value;
public Element(T value)
{
this.Value = value;
}
}

And then stored the element objects in my collection:
So, here is how I would store a couple of properties:
{
HashTable styleElements = new HashTable();
styleElements.Add("BackgroundColor", new Element<Color>(Color.Blue);
styleElements.Add("FontName", new Element<string>("Arial");
// and so on

//now accessing the collection:
aControl.BackgroundColor =
((Element<Color>)styleElements["BackgroundColor"]).Value;
}

Is this a horrible, horrible idea, or is it preferable to directly storing
the value types in the hashtable (thereby boxing them)?

Nov 17 '05 #2
Hmm. Looking back, I don't know whether I am much more efficient than
boxing, as I am now manually allocating a new class (the element), so there
is still an additional heap allocation.

But I want to get back to your comments about explicitly vs. implicitely
boxing. It was my understanding that if you pass a value type as the
parameter to a generic type, then you get a "real" value type, so boxing
does not have to occur. I had thought that was supposed to be a prime
benefit of using a the generic collections with value types. Am I wrong
here?

Or, when you say that I am now explicitly boxing vs implicitly boxing, are
you refering to the instatiation of the Element class? I'm still not sure
whether this is the way to go. It seems that if I am only going to read
from the list of elements once, then I'm losing by going with generics. But
what if I am going to load the collection once, and read from it many times?
Then it seems as though I am trading multiple unbox operations for casts.

I'm still not sure which is better/worse. Comments?


"Luc E. Mistiaen" <lu**********@advalvas.be.no.spam> wrote in message
news:uO**************@TK2MSFTNGP12.phx.gbl...
It seems to me that you just made the boxing explicit (and probably less
efficient than the implicit one).

/LM

"J.Marsch" <jm*****@newsgroup.nospam> wrote in message
news:ey**************@tk2msftngp13.phx.gbl...
All:

I have an interesting problem in front of me. does this sound
reasonable, or ridiculous?

I have to build something that is sort of like a style sheet for Windows
controls.
Picture a collection of dissimilar value types: a couple of Color types,
some ints, some strings etc.

I _could_ just use a hashtable of object to contain all of these, but
since they are value types, they would be boxed.

But what if I did this: (warning the syntax probably isn't spot on)
public class Element<T>
{
public T Value;
public Element(T value)
{
this.Value = value;
}
}

And then stored the element objects in my collection:
So, here is how I would store a couple of properties:
{
HashTable styleElements = new HashTable();
styleElements.Add("BackgroundColor", new Element<Color>(Color.Blue);
styleElements.Add("FontName", new Element<string>("Arial");
// and so on

//now accessing the collection:
aControl.BackgroundColor =
((Element<Color>)styleElements["BackgroundColor"]).Value;
}

Is this a horrible, horrible idea, or is it preferable to directly
storing the value types in the hashtable (thereby boxing them)?


Nov 17 '05 #3
I was refereing to the fact that you stash your value into a class member
which is equivalent to boxing.

/LM

"J.Marsch" <jm*****@newsgroup.nospam> wrote in message
news:um**************@TK2MSFTNGP12.phx.gbl...
Hmm. Looking back, I don't know whether I am much more efficient than
boxing, as I am now manually allocating a new class (the element), so
there is still an additional heap allocation.

But I want to get back to your comments about explicitly vs. implicitely
boxing. It was my understanding that if you pass a value type as the
parameter to a generic type, then you get a "real" value type, so boxing
does not have to occur. I had thought that was supposed to be a prime
benefit of using a the generic collections with value types. Am I wrong
here?

Or, when you say that I am now explicitly boxing vs implicitly boxing, are
you refering to the instatiation of the Element class? I'm still not sure
whether this is the way to go. It seems that if I am only going to read
from the list of elements once, then I'm losing by going with generics.
But what if I am going to load the collection once, and read from it many
times? Then it seems as though I am trading multiple unbox operations for
casts.

I'm still not sure which is better/worse. Comments?


"Luc E. Mistiaen" <lu**********@advalvas.be.no.spam> wrote in message
news:uO**************@TK2MSFTNGP12.phx.gbl...
It seems to me that you just made the boxing explicit (and probably less
efficient than the implicit one).

/LM

"J.Marsch" <jm*****@newsgroup.nospam> wrote in message
news:ey**************@tk2msftngp13.phx.gbl...
All:

I have an interesting problem in front of me. does this sound
reasonable, or ridiculous?

I have to build something that is sort of like a style sheet for Windows
controls.
Picture a collection of dissimilar value types: a couple of Color
types, some ints, some strings etc.

I _could_ just use a hashtable of object to contain all of these, but
since they are value types, they would be boxed.

But what if I did this: (warning the syntax probably isn't spot on)
public class Element<T>
{
public T Value;
public Element(T value)
{
this.Value = value;
}
}

And then stored the element objects in my collection:
So, here is how I would store a couple of properties:
{
HashTable styleElements = new HashTable();
styleElements.Add("BackgroundColor", new Element<Color>(Color.Blue);
styleElements.Add("FontName", new Element<string>("Arial");
// and so on

//now accessing the collection:
aControl.BackgroundColor =
((Element<Color>)styleElements["BackgroundColor"]).Value;
}

Is this a horrible, horrible idea, or is it preferable to directly
storing the value types in the hashtable (thereby boxing them)?



Nov 17 '05 #4
> Or, when you say that I am now explicitly boxing vs implicitly
boxing, are you refering to the instatiation of the Element class?

Yes. That's exactly what he's referring to. Creating an instance of
Element that holds your value type _is_ boxing. You're just doing what
the CLR would do anyway. This should illustrate two things to you:
first, that you should just shove the value types into the Hashtable
and let the CLR box them, and second, that for the most part those who
wring their hands shouting, "Boxing! Boxing! Oh the horror!" are for
the most part blowing smoke. Boxing is just simply what you've done in
the code you posted. It's not that big a deal... ceratinly not worth
building an entire new class and adding a bunch of machinery just to
avoid it.

Just let the CLR box the value type. You won't even notice. :)

To answer your other question, generics come into their own when you
want to create an aggregate structure (for example... there are other
situations besides aggregates) that will hold _one particular_ value
type. Then you can say: Hashtable<string, DateTime> and the DateTime
types stored in the hash table will _not_ be boxed. However, IMHO, this
is the least interesting part of generics: their run-time efficiency
benefits. The most interesting thing about generics is that they allow
for much better compile-time checking of your code, since the compiler
now understands what it is you want to do. As a result, you get
type-safe collections, which to me is far more significant than saving
a few cycles here and there (although the latter is nice, too).

In your case, generics won't help you: your collection does not hold a
uniform type of thing. This is one case in which the current
(non-generic) collections are what you need. As I said, just ignore the
whole boxing thing... it's not going to have a significant impact.

Nov 17 '05 #5
That clears things up, I believe. Thank you both for posting, I appreciate
it!
"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Or, when you say that I am now explicitly boxing vs implicitly

boxing, are you refering to the instatiation of the Element class?

Yes. That's exactly what he's referring to. Creating an instance of
Element that holds your value type _is_ boxing. You're just doing what
the CLR would do anyway. This should illustrate two things to you:
first, that you should just shove the value types into the Hashtable
and let the CLR box them, and second, that for the most part those who
wring their hands shouting, "Boxing! Boxing! Oh the horror!" are for
the most part blowing smoke. Boxing is just simply what you've done in
the code you posted. It's not that big a deal... ceratinly not worth
building an entire new class and adding a bunch of machinery just to
avoid it.

Just let the CLR box the value type. You won't even notice. :)

To answer your other question, generics come into their own when you
want to create an aggregate structure (for example... there are other
situations besides aggregates) that will hold _one particular_ value
type. Then you can say: Hashtable<string, DateTime> and the DateTime
types stored in the hash table will _not_ be boxed. However, IMHO, this
is the least interesting part of generics: their run-time efficiency
benefits. The most interesting thing about generics is that they allow
for much better compile-time checking of your code, since the compiler
now understands what it is you want to do. As a result, you get
type-safe collections, which to me is far more significant than saving
a few cycles here and there (although the latter is nice, too).

In your case, generics won't help you: your collection does not hold a
uniform type of thing. This is one case in which the current
(non-generic) collections are what you need. As I said, just ignore the
whole boxing thing... it's not going to have a significant impact.

Nov 17 '05 #6

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

Similar topics

27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
2
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
23
by: Luc Vaillant | last post by:
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...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
5
by: anders.forsgren | last post by:
This is a common problem with generics, but I hope someone has found the best way of solving it. I have these classes: "Fruit" which is a baseclass, and "Apple" which is derived. Further I have...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in 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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.