ArrayList and primitive types

  • Thread starter Thread starter Bear
  • Start date Start date
B

Bear

Hello there

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
just a "typedef" of the Int32 structure and, if so, does the runtime system
load a value type onto the stack even though it only needs to load a
primitive type?
 
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
 
Bjoern,
C# is a language where everything (value & reference types) is an object.
That is why the ArrayList.Add(object) takes int.

--------------------
From: "Bear" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Subject: ArrayList and primitive types
Date: Sun, 2 Nov 2003 14:16:45 +0100
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Lines: 15
Message-ID: <[email protected]>
NNTP-Posting-Host: 62.107.67.208
X-Trace: 1067779002 news.stofanet.dk 7181 62.107.67.208
X-Complaints-To: Telia Stofa Abuse <[email protected]>
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!newsfee
d01.sul.t-online.de!t-online.de!eusc.inter.net!news.tele.dk!news.tele.dk!sma
ll.news.tele.dk!newsfeed101.telia.com!nf01.dk.telia.net!news104.dk.telia.net
!not-for-mail
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:196052
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

Hello there

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
just a "typedef" of the Int32 structure and, if so, does the runtime system
load a value type onto the stack even though it only needs to load a
primitive type?

--
Best regards

Bjoern D. Rasmussen
www.bearware.dk


Rakesh, EFT.

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
Back
Top