Wrox Programmer Forums
|
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 4th, 2008, 07:31 PM
Registered User
 
Join Date: Mar 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Generics in 3.0

I bought the Professional C# 2008 book and while it is an excellent book some sections are not easy to understand. For example, chapter 9, that introduces "Generics" and chapter 7, about "Delegate and Events" are confusing. One question related to Generics is:

Q. If almost every thing (classes, methods, interfaces, collections ...etc) can declared as Generics then why do we need
non-Generics declarations? What is the advantage or in which situation it would make sense to still use non-Generics declarations?

Thanks,
Sikander

Thanks,
Sikander
 
Old May 5th, 2008, 08:30 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

There will be times when you can utilize generics, when you have a block of functionality that could apply to any or a large set of class types. Take List<T> for example. Anything can be a list, as a list is a very abstract construct. Therefore a list is a natural candidate for "genericizing".

However, consider a class that works with text parsing. The methods will always expect strings. So is there much sense in creating a class that uses generics when the type parameter for the generic will likely always be a string? That's just overkill.

Given that generics can be tricky to work with and understand, it is sensible to maintain functionality that applies to specific concrete types when appropriate. Doing so leads to more terse and readable code and decreased complexity.

Consider also that every data type is derived from System.Object. You could certainly declare every variable in your code as type 'object', but you would then require casting every time you needed to do something specific to the type instantiated (that is not inherited from System.Object). This is functional but not very user friendly.

Consider generics a specialized tool set that should be used where appropriate, but not as a replacement for existing tools.

-Peter
compiledthoughts.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
NET Generics FREDYCOREA .NET Framework 3.5 1 February 21st, 2008 10:17 AM
Application of generics micmit C# 2005 0 October 12th, 2006 08:10 PM
Generics Related error in my code, I think??? rodmcleay Generics 1 August 3rd, 2006 12:02 AM
Problem with generics collections demitry .NET Framework 2.0 4 November 24th, 2005 06:39 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.