KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm having a hard time understanding when to use Object (boxing/unboxing) vs when to use generics. For example: public class Stack { int position; object[] data = new object[10]; public void Push (object o) { data[position++] = o; } public object Pop() { return data[--position]; } } VS. public class Stack<T> { int position; T[] data = new T[100]; public void Push(T obj) {data[position++] = obj; } public T Pop() { return data[--position]; } } Which one should I use and under what conditions? It seems like with the System.Object way I can have objects of all sorts of types currently living within my Stack. So wouldn't this be always preferable? Thanks!
Tags (comma-separated)
Save Edits
Cancel