473,695 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generics + collections

Am I correct in thinking that C#/.NET 2.0 removes the need for a
collection for each business object you have, and allows you to create
just one collection?

Or is this just the equivalent of using Object for your collection?

Jan 31 '06 #1
4 2484
Chris,

..NET eliminates the need of creating strongly typed collection for each
business object. You can use generic collection and benefit from the stronlg
typness.

Using generics is not equivalent of having collection of Object.
Using ArrayList for example, which is collection of Object one can create
heterogeneous collection, which is not possible with generic collection
unless the type is not sepcified as Object.
--
HTH
Stoitcho Goutsev (100)

"Chris S." <sl********@gma il.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
Am I correct in thinking that C#/.NET 2.0 removes the need for a
collection for each business object you have, and allows you to create
just one collection?

Or is this just the equivalent of using Object for your collection?

Jan 31 '06 #2
I can see mountains of source code being removed in business layers
thanks to this. I realise that compile time errors occur if you create
a collection with a type and then try to use another type, e.g.

MyCollection<My Object> coll = new MyCollection<My Object>();
coll.Add(new MyObject());
coll.Add("test" );

but what I meant was internally, there can't be a huge difference
between an Object being passed around?

Jan 31 '06 #3

"Chris S." <sl********@gma il.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com...
I can see mountains of source code being removed in business layers
thanks to this. I realise that compile time errors occur if you create
a collection with a type and then try to use another type, e.g.

MyCollection<My Object> coll = new MyCollection<My Object>();
coll.Add(new MyObject());
coll.Add("test" );

but what I meant was internally, there can't be a huge difference
between an Object being passed around?


Most people wouldn't have bothered creating typesafe collections for
internal use only - that's usually reserved for where you have collection
valued public properties.

Before you go and automatically change all your code watch out for the fact
that generic collections are not totally backward compatible - eg. IList<T>
does not derive from IList.

Note also that List<any struct> is more efficient than ArrayList of the
struct because of boxing
Jan 31 '06 #4
There is; when you put something into an ArrayList, its up cast to
object when its added, and has to be down cast when you pull it out.
That takes time. Worse, value types have to be boxed and unboxed,
adding even more time. A generic collection won't have these problems
(unless as someone else mentioned, you have a generic collection of
objects).

Jan 31 '06 #5

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

Similar topics

0
5565
by: mark | last post by:
I've been getting odd compile errors when trying to sort while using generics. I have the following code: static final List<Class> levelList; static { Vector<Class> v = new Vector<Class>(); v.add(myClass1.class);
1
420
by: Helium | last post by:
Is there any need for "System.Collections.Queue" any more? OK, .Net made the mistake to start without generics, but that is fixed now. Languages without support for generics could use "System.Collections.Generics.Queue<Object>" instead of "System.Collections.Queue". So everytime those languages use a generic, they just use them with <object>. I don't think having two different sets of collections is a good idea.
10
2601
by: Ruediger Klaehn | last post by:
Sorry about the harsh language, but I have to vent my anger to somebody who actually understands what I am talking about. Complaining to my girlfriend is not going to produce any meaningful results other than straining our relationship... I just downloaded Visual C# Express Edition to mess with .NET 2.0 generics. Being a numerically inclined developer, the first thing I wanted to write was a generic complex number class. I also have some...
2
2874
by: Marc | last post by:
Given a class 'Invoice' with a property 'public IMyColl<IInvoiceLine> InvoiceLines' where 'IMyColl<T> : IList<T>' i would like to detect by reflection that 'InvoiceLines' is a 'System.Collection.Generic.IList'. When performing something like: 'if (typeof(IList<>).IsAssignableFrom(propertyInfo.Type))' where 'propertyInfo' obviously refers to the 'InvoiceLines', the result is always 'false' because indeed 'IList<object>' and...
6
245
by: nick_nw | last post by:
Hi, What significant advantages do generics give me over passing objects around as 'object' and casting when needed? I was asked this recently and started off by saying, "well of course generics are better because, er.... hang on lets ask the ng!" So hence the q.
3
2600
by: Showjumper | last post by:
Back in asp.net 1.1 i made custom collection classes per Karl Seguin's article On the Way to Mastering ASP.NET: Introducing Custom Entity Classes to take advantage of strongly typed data. Now with generics i understand that one doesnt have to used these custom collections. So i want to move this 1.1 project to 2.0. I have been reading about generics but i geuss i dont really get hpw to implement them. For example i have 2 classees in the...
4
10117
by: SHEBERT | last post by:
Here is an example of a SortedList that works as a datasource to the ComboBox and a generic SortedList<that does not works as a datasource to the ComboBox. Why? If I use List and generic List<>, both works. private void Form1_Load(object sender, EventArgs e) { System.Collections.SortedList QA1 = new System.Collections.SortedList();
6
4082
by: Mark Rae | last post by:
Hi, I'm in the process of updating an ASP.NET v1.1 web app to v2. The app uses ActiveDirectory a great deal, and I'm trying to use the new System.Collections.Generic namespace where possible, having been advised by several luminaries that that is a "good thing to do"... :-) However, I'm experiencing a problem with the IEnumerable interface. (N.B. I understand fully that I should be using the LDAP provider instead of the WinNT provider...
7
3253
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 System.Collections.Generic namespace. Literature I have read on this ties generics in with collections, hence articulate their examples as such. That's fine, I understand what is being said. My question is more towards the application and implementation...
7
5745
by: =?Utf-8?B?Q29kZVJhem9y?= | last post by:
Can someone explain a few things about collections to me. In C# 2 generics, you can create a collection class by inheriting from System.Collections.ObjectModel.Collection. Using this you can iterate through the collection and use "foreach" on the collection. public class Cars : System.Collections.ObjectModel.Collection<Car{ } So, could someone explain to me why you would want to create a GENERIC custom collection class.
0
9140
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9004
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4351
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3025
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2290
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1987
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.