RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

.net framework 2.0 question

brodock

Sorceror
.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
 
brodockbr said:
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
 

Khaz

Knight
'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.
 

Sep102

Page
Khaz said:
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.
 

swtrse

Wanderer
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)
 
Top