|
||
|
|
#1 (permalink) |
|
Forum Expert
Join Date: Jan 2003
Posts: 564
|
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
__________________
|
|
|
|
|
|
#2 (permalink) | |
|
Forum Newbie
|
Quote:
__________________
Ice Princess25 |
|
|
|
|
|
|
#3 (permalink) |
|
Forum Expert
Join Date: Mar 2003
Location: where I belong
Posts: 1,761
|
'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 |
|
|
|
|
|
#5 (permalink) | |
|
Forum Expert
|
Quote:
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. |
|
|
|
|
|
|
#6 (permalink) |
|
Join Date: Feb 2003
Posts: 33
|
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);
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) |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|