By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,166 Members | 1,130 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,166 IT Pros & Developers. It's quick & easy.

C# Generics Book

P: n/a

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 Experience Only, Please.

...

My 1.1 CollectionBase objects look like this most of the time.

I'm trying to determine if I can *always* get the *exact same* functionality
with generics.......

public class CustomerCollection : System.Collections.CollectionBase
{

public void Add ( BusinessObjects.Customer cust )
{
base.InnerList.Add(cust);
}

public BusinessObjects.Customer this[int index]
{
get
{
return (BusinessObjects.Customer )base.InnerList[index];
}
}
public BusinessObjects.Customer Contains( string custId )
{

foreach( BusinessObjects.Customer item in base.InnerList )
if( item.CustomerID .Equals(custId) )
{
return item;
}
return null;
}
public BusinessObjects.Customer Remove(string custId)
{
// remove the object, but also return it.

BusinessObjects.Customer returnObject = null;
returnObject = this.Contains(custId);
if (null != returnObject )
{
base.InnerList.Remove (returnObject);
}
return returnObject;
}
protected override void OnValidate(object value)
{
base.OnValidate(value);
if (!(value is BusinessObjects.Customer))
{
throw new ArgumentException("Collection only supports Customer
objects.");
}
}
}
May 24 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On Wed, 24 May 2006 16:25:29 -0400, "sloan" <sl***@ipass.net> wrote:

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 Experience Only, Please.

..

My 1.1 CollectionBase objects look like this most of the time.

I'm trying to determine if I can *always* get the *exact same* functionality
with generics.......

public class CustomerCollection : System.Collections.CollectionBase
{

public void Add ( BusinessObjects.Customer cust )
{
base.InnerList.Add(cust);
}

public BusinessObjects.Customer this[int index]
{
get
{
return (BusinessObjects.Customer )base.InnerList[index];
}
}
public BusinessObjects.Customer Contains( string custId )
{

foreach( BusinessObjects.Customer item in base.InnerList )
if( item.CustomerID .Equals(custId) )
{
return item;
}
return null;
}
public BusinessObjects.Customer Remove(string custId)
{
// remove the object, but also return it.

BusinessObjects.Customer returnObject = null;
returnObject = this.Contains(custId);
if (null != returnObject )
{
base.InnerList.Remove (returnObject);
}
return returnObject;
}
protected override void OnValidate(object value)
{
base.OnValidate(value);
if (!(value is BusinessObjects.Customer))
{
throw new ArgumentException("Collection only supports Customer
objects.");
}
}
}


I don't know any book, but in case you're interested, I wrote an
article a while ago
(http://www.coders-lab.dotnetnuke-por.../Default.aspx).
--
Ludwig Stuyck
http://www.coders-lab.be
May 24 '06 #2

P: n/a

That's a nice write up, and you talk about my exact questions.

I'm reading it now.

Thanks for the work, and the link to it.

...

(I'm still taking book recommendations, I like having a reference book on
the shelf sometimes).

"Ludwig" <no**@none.com> wrote in message
news:v8********************************@4ax.com...
On Wed, 24 May 2006 16:25:29 -0400, "sloan" <sl***@ipass.net> wrote:

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 Experience Only, Please.

..

My 1.1 CollectionBase objects look like this most of the time.

I'm trying to determine if I can *always* get the *exact same* functionalitywith generics.......

public class CustomerCollection : System.Collections.CollectionBase
{

public void Add ( BusinessObjects.Customer cust )
{
base.InnerList.Add(cust);
}

public BusinessObjects.Customer this[int index]
{
get
{
return (BusinessObjects.Customer )base.InnerList[index];
}
}
public BusinessObjects.Customer Contains( string custId )
{

foreach( BusinessObjects.Customer item in base.InnerList )
if( item.CustomerID .Equals(custId) )
{
return item;
}
return null;
}
public BusinessObjects.Customer Remove(string custId)
{
// remove the object, but also return it.

BusinessObjects.Customer returnObject = null;
returnObject = this.Contains(custId);
if (null != returnObject )
{
base.InnerList.Remove (returnObject);
}
return returnObject;
}
protected override void OnValidate(object value)
{
base.OnValidate(value);
if (!(value is BusinessObjects.Customer))
{
throw new ArgumentException("Collection only supports Customer
objects.");
}
}
}

I don't know any book, but in case you're interested, I wrote an
article a while ago

(http://www.coders-lab.dotnetnuke-por...icles/tabid/16
942/Default.aspx). --
Ludwig Stuyck
http://www.coders-lab.be

May 24 '06 #3

P: n/a
On Wed, 24 May 2006 17:17:20 -0400, "sloan" <sl***@ipass.net> wrote:

That's a nice write up, and you talk about my exact questions.

I'm reading it now.

Thanks for the work, and the link to it.

..

(I'm still taking book recommendations, I like having a reference book on
the shelf sometimes).


Always interested in feedback, grammatical errors for example, because
English is not my mother tongue. And if you think that something is
missing, please let me know.
--
Ludwig Stuyck
http://www.coders-lab.be
May 24 '06 #4

P: n/a
Why force a download Ludwig? That's not needed and will only cause many
people to go away.

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/

"Ludwig" <no**@none.com> wrote in message
news:dv********************************@4ax.com...
On Wed, 24 May 2006 17:17:20 -0400, "sloan" <sl***@ipass.net> wrote:

That's a nice write up, and you talk about my exact questions.

I'm reading it now.

Thanks for the work, and the link to it.

..

(I'm still taking book recommendations, I like having a reference book on
the shelf sometimes).


Always interested in feedback, grammatical errors for example, because
English is not my mother tongue. And if you think that something is
missing, please let me know.
--
Ludwig Stuyck
http://www.coders-lab.be

May 24 '06 #5

P: n/a
_DD
There are books that deal with getting up to speed on new C# 2.0
features, but I know of only one book that deals -only- with Generics.
That's Ted Golding's "Pro .NET 2.0 Generics". See:
http://www.amazon.com/gp/product/0764559885/
May 25 '06 #6

P: n/a
I agree. Besides, it took me couple of seconds to visualize
the button, I even almost gave up and noticed it at the very
last moment before going out of the page.

clintonG wrote:
Why force a download Ludwig? That's not needed and will only cause many
people to go away.

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/

"Ludwig" <no**@none.com> wrote in message
news:dv********************************@4ax.com...
On Wed, 24 May 2006 17:17:20 -0400, "sloan" <sl***@ipass.net> wrote:
That's a nice write up, and you talk about my exact questions.

I'm reading it now.

Thanks for the work, and the link to it.

..

(I'm still taking book recommendations, I like having a reference book on
the shelf sometimes).

Always interested in feedback, grammatical errors for example, because
English is not my mother tongue. And if you think that something is
missing, please let me know.
--
Ludwig Stuyck
http://www.coders-lab.be


May 25 '06 #7

P: n/a
On Thu, 25 May 2006 12:33:46 GMT, Sericinus hunter <se*****@flash.net>
wrote:
I agree. Besides, it took me couple of seconds to visualize
the button, I even almost gave up and noticed it at the very
last moment before going out of the page.


Thanks for the feedback. Meanwhile I've created a html version of the
article.

Other feedback I got:
- generics are not only used for collections
- add generics and databinding topic
I'll update the article in a few days with these additions.
--
Ludwig Stuyck
http://www.coders-lab.be
May 26 '06 #8

P: n/a

Lugwig,

I've been converting my 1.1 demo
(http://spaces.msn.com/sholliday/ 5/24/2006 )

I've been using your article/pdf ... and its been very easy with your
examples.

...

For feedback:

Anal Improvement #1
If there was a table of contents with clickable links to stuff like
"Filters", etc, that'd be nice. I did the conversion over a period of 3
days, and I was always text searching to find the right section.

......
Enhanced .Remove method??

Technically, there is one thing I'm trying to do.. and maybe you have
feedback.

The "built in" .Remove method takes an object as the parameter. and returns
a bool ...
Ex:
List<BusinessLayer.BusinessObjects.Customer> custCollection =
GetCollection();//something to get a collection of Customer objects

BusinessLayer.BusinessObjects.Customer custToRemove = custCollection[3]; //
assume 3 or more items in the collection for this illustration

bool itWorked = custCollection.Remove(custToRemove);

Where the .Remove returns a bool .. and the argument a Customer object.

In my 1.1 code .. my .Remove method

#1 I have on .Remove which takes the unique identifier of a Customer
(CustomerId) and then
#2 Uses it to find the Customer object ... then after if finds it... removes
it.
#3 ~~and returns the Customer object back to the caller.

On #3, you'll see that my comments point to the java way of doing a .Remove
........

Not that I'm a super-java lover, but I see the value of doing this
sometimes.

Most times , I don't care about it being removed. On occasion, I may want
to keep track of it .. after I removed it from the Collection.

//Sample Code
public BusinessObjects.Customer Contains( string custId )
{

foreach( BusinessObjects.Customer item in base.InnerList )
if( item.CustomerID .Equals(custId) )
{
return item;
}
return null;
}
public BusinessObjects.Customer Remove(string custId)
{
// see
http://java.sun.com/j2se/1.4.2/docs/...va.lang.Object)
// for java method, which returns the object you removed, in case
// you want to do something with it

BusinessObjects.Customer returnObject = null;
returnObject = this.Contains(custId);
if (null != returnObject )
{
base.InnerList.Remove (returnObject);
}
return returnObject;
}

I guess I'm wondering if there is a way to return the actual Customer object
(that I just removed) back to the caller...
in the Generics world.

I'm working on it now .. to see what I can come up with, but thought I'd
share that nugget with you.

Feel free to comment/critique my method ... you're not going to hurt my
feelings ....

...

Other than that, its been a direct portover.... and I'm becoming more
comfortable with generics, so thanks again for the well laid out article.

...

Sloan

"Ludwig" <no**@none.com> wrote in message
news:vb********************************@4ax.com...
On Thu, 25 May 2006 12:33:46 GMT, Sericinus hunter <se*****@flash.net>
wrote:
I agree. Besides, it took me couple of seconds to visualize
the button, I even almost gave up and noticed it at the very
last moment before going out of the page.


Thanks for the feedback. Meanwhile I've created a html version of the
article.

Other feedback I got:
- generics are not only used for collections
- add generics and databinding topic
I'll update the article in a few days with these additions.
--
Ludwig Stuyck
http://www.coders-lab.be

Jun 2 '06 #9

P: n/a
"sloan" wrote :
I'm looking for a good book which goes over Generics in great detail.
and to have as a reference book on my shelf.
Sloan, these are some on-line resouces for Generics shorty to be publised in
an article on Generic Dictionaries in DeVX.com's website.

Hope they are useful :

1. Books (and free on-line chapters and book) PDF links

Pro C# 2005 and the .NET 2.0 Platform
Andrew Troelsen, APress Publishing, 2005
Chapter 10 : "Understanding Generics" is excellent
available on-line as a free PDF download from Apress at :
http://www.apress.com/ApressCorporat...94193-2965.pdf

Visual C# 2005 : A Developer's Notebook
Jesse Liberty, O'Reilly Publishing,
Chapter 1 : pp. 1~22 : A great exploratory walk-through of Generics in
..NET 2.0 with very
sophisticated code examples. Strong coverage of iterators and enumerators
for Generics.
available on-line as a free PDF download from O'Reilly at :
http://www.oreilly.com/catalog/visua...apter/ch01.pdf

The "Official" ECMA Specification which includes Generics in .NET 2.0 :
Standard ECMA-334 : C# Language Specification 4th. edition (June 2006)
available on-line as a free PDF download from ECMA at :
http://www.ecma-international.org/pu...T/Ecma-334.pdf

2. Selected Links :

On CodeProject.Com : (selected to illustrate range of 'non-obvious' uses
of Generics)

Generic Singleton Pattern using Reflection, in C# by Martin Lapierre
http://www.codeproject.com/cs/design...tonPattern.asp
Uses 'constraint' feature of Generics to create Singleton with
non-accessible constructor.

A Generic Tree Collection by Nicholas Butler
http://www.codeproject.com/csharp/treecollection2.asp
Nice demonstration of how to use serialization with Generic Types, as
well as a very handy generic

Operator Overloading with Generics by Keith Farmer
http://www.codeproject.com/csharp/ge...rs.aspTreeView class
Very interesting code that shows use of Generic classes, delegates.
Mentions that Delegate calling by late-binding is optimized in .NET 2.0,
and has
a link to the MSDN article by Pobar listed below.

Generics, Serialization and NUnit by CraigD.
http://www.codeproject.com/useritems/Kelvin.asp
Using Generics to simplify serialization and de-serialization to Binary
Files. Coverage
of issues using NUnit with Generics.

On MSDN :

Introducing Generics in the CLR by Jason Clark (revised version, MSDN
Magazine, January 2006)
http://msdn.microsoft.com/msdnmag/is...T/default.aspx
A very good overview of Generics in .NET 2.0 including advanced topics.

Performance Pitfalls to Craft Speedy Applications by Joel Pobar, MSDN
Magazine, July 2005
http://msdn.microsoft.com/msdnmag/is...n/default.aspx
Interesting (and technically deep article) on reflection in .NET 2.0 and
calling speed.
Excellent section explaining 'rules and 'constraints features of
Generics.
Jul 24 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.