Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 06-18-2006, 02:37 PM   #1 (permalink)
Forum Expert
 
Join Date: Jan 2003
Posts: 564
Default .net framework 2.0 question

Ok, i'm hearing people talk about "generics" in framework 2.0... i would like to someone give me an example about what a generic is and what's the goal to use it.

Links to documentations are also welcome.
thanks
__________________
UO Central: O Mistério do Oculto
(Free Shard Brasileiro)

brodock is offline   Reply With Quote
Old 06-18-2006, 02:39 PM   #2 (permalink)
Forum Newbie
 
Join Date: Feb 2006
Age: 28
Posts: 23
Send a message via AIM to iceprincess25 Send a message via MSN to iceprincess25
Default

Quote:
Originally Posted by brodockbr
Ok, i'm hearing people talk about "generics" in framework 2.0... i would like to someone give me an example about what a generic is and what's the goal to use it.

Links to documentations are also welcome.
thanks
there is no generics
__________________
Ice Princess25
iceprincess25 is offline   Reply With Quote
Old 06-18-2006, 02:56 PM   #3 (permalink)
Forum Expert
 
Khaz's Avatar
 
Join Date: Mar 2003
Location: where I belong
Posts: 1,761
Default

'cause I'm a lazy bum, here's a simple link explaining Generics: Generics Explained

The most relevant use for them that you will find with custom RunUO programming is the performance increase with ArrayLists. ArrayLists require boxing and unboxing of types (Mobile to object, and back, for example).
Generics allow list and structured storage of types without the need for casting, so list access is much more efficient. The drawback, however, is that any given list can only have its one defined type. Anyway, check out the article.
__________________
"Misfortune shows those who are not really friends." -Aristotle
"A multitude of words is no proof of a prudent mind." -Thales
Khaz is offline   Reply With Quote
Old 06-18-2006, 03:07 PM   #4 (permalink)
Forum Expert
 
Join Date: Jan 2004
Location: England
Age: 20
Posts: 442
Default

Also some interesting links in this thread, especially about Generics:
RunUO 2.0
__________________
David's Sig is my Sig. He always says it best.
tobyjug is offline   Reply With Quote
Old 06-18-2006, 03:15 PM   #5 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

Quote:
Originally Posted by Khaz
The most relevant use for them that you will find with custom RunUO programming is the performance increase with ArrayLists. ArrayLists require boxing and unboxing of types (Mobile to object, and back, for example). Generics allow list and structured storage of types without the need for casting, so list access is much more efficient. The drawback, however, is that any given list can only have its one defined type. Anyway, check out the article.
Well, not necessarily, the only thing that requires boxing and unboxing operations is to put or get an object on or off the Managed Heap that does not normally reside there (e.g. Value Type's). So, if you wanted to store some struct in an ArrayList, it would need to be boxed to be added to it and unboxed when it is removed and cast back to the original type. If you wanted to store a Mobile in an ArrayList then no boxing or unboxing would need to take place as Mobile's are a class and as such instances of it are already on the Managed Heap.

Like you said though, a big win for the new collections using Generics is the casts that used to be necessary that can now be removed. Personally, though I don't see the ability to store only a single type to be constricting, I see it as being quite a bit nicer as there is now static checking of the types of objects being added to the collection. I think this is much nicer than the old-style collections where the onus was on you to make sure the types you added to the collection and the type that you removed it as are synchronized correctly and having any mistakes show up as a run-time error.

At the very least, if it is too constricting, you could always parameterize it with an interface or base class and add any types derived from them. You just need to note that if you do use an interface to store objects that aren't on the Managed Heap, you get the same boxing and unboxing operations that happened with ArrayList's storing them as Object's.

Last edited by Sep102; 06-18-2006 at 03:34 PM.
Sep102 is offline   Reply With Quote
Old 06-19-2006, 10:36 AM   #6 (permalink)
 
Join Date: Feb 2003
Posts: 33
Default

Well just for the note.

You can store different Objects in the generic lists.
There are only two conditions that must be true
1) The object you put in the generic must be the object that is defined for the generic or has to be an child class of this object.
(For Example
Code:
List<object> test = new List<object>();
test.Add("Hallo");
test.Add(1);
works fine)

2) The object must mach the limitations that can be set in the generic class.
(Thats the thing you can define with the WHERE constraint at the generic class itself)
swtrse is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5