C# Generics Book

S

sloan

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.");
}
}


}
 
L

Ludwig

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-portal.com/Codingstuff/Myarticles/tabid/16942/Default.aspx).
 
S

sloan

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 said:
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-portal.com/Codingstuff/Myarticles/tabid/16
942/Default.aspx).
 
L

Ludwig

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

Ludwig

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

sloan

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/api/java/util/Hashtable.html#remove(java.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
 
B

Bill Woodruff

:
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/ApressCorporate/supplement/1/390/1590594193-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/visualcadn/chapter/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/publications/files/ECMA-ST/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/GenericSingletonPattern.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/genericoperators.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/issues/06/00/NET/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/issues/05/07/Reflection/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.
 

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

Top