On Sun, 02 Nov 2003 14:16:45 +0100, Bear wrote:
How come it's possible to add values of the type "int" into an ArrayList
when the Add member function only accepts values of type "object"? Is an int
System.Object is the mother of all classes in C# (i.e. the ultimate
base class). That is e.g. the reason why all classes in C# provide
a 'ToString()' or a 'GetType()' method (because: 'object' does).
Now, System.ValueType is the base class for all value types and
- because of the above - System.Object is thus the ultimate base
class for alll value types (and everything else :-)
Example: Observe that the following two lines are essentially
the same (and produce the same IL code):
int myVariable;
System.Int32 myVariable;
'System.Int32' is a struct in the .NET Class Library and 'int' is
a C# alias for that struct. (The 'real' implementation of value
types in C#/.NET is actually a little bit tricky; but at least they
'appear' as classes in the above sense.)
The only exception to the above rule (i.e. the above example) is
'System.Void' and 'void' (IIRC). The return type 'void' in C# is
*not* an alias for 'System.Void', but implemented by other means.
Conclusion: You can put everything into an 'ArrayList'; if you
want more control by the compiler, you have to define your own
MyArrayList-Class (by deriving it from ArrayList or by deriving
it from the interfaces ICollection, IList and IEnumberable or by
deriving it from the abstract class CollectionBase.)
Hope this helps. Thomas
--
mailto:
ta*****@phreaker.foo (foo = net)