473,394 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

ArrayList and primitive types

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
Nov 15 '05 #1
3 2641
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)
Nov 15 '05 #2
http://www.csharphelp.com/archives/archive100.html

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Bear" <bd*@NOSPAM.sol.dk> wrote in message
news:3f**********************@nntp05.dk.telia.net. ..
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

Nov 15 '05 #3
Bjoern,
C# is a language where everything (value & reference types) is an object.
That is why the ArrayList.Add(object) takes int.

--------------------
From: "Bear" <bd*@NOSPAM.sol.dk>
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: <3f**********************@nntp05.dk.telia.net>
NNTP-Posting-Host: 62.107.67.208
X-Trace: 1067779002 news.stofanet.dk 7181 62.107.67.208
X-Complaints-To: Telia Stofa Abuse <ab***@stofa.dk>
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed 00.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.teli a.net!news104.dk.telia.net
!not-for-mailXref: 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 intjust 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

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ken | last post by:
Hi. Does the Java spec mandate set sizes for primitive types (e.g., int, float, char, etc.)? I know the ANSI C++ standard does not do this, which makes these sizes compiler dependent. So I guess...
9
by: Gonçalo Rodrigues | last post by:
Hi all, I have a few questions on primitive types that I will divide in two main questions. I realize that some of these questions -- especially 2. below -- are not directly related to C++ as a...
26
by: Kip | last post by:
Greetings everyone, Is there anyone here who could point me to a page or pdf that has a list of the sizes of all of the C primitive data types on various implementations such as SPARC, x86,...
14
by: Matt | last post by:
I want to know if "int" is a primitive type, or an object? For example, the following two approaches yield the same result. > int t1 = int.Parse(TextBox2.Text); //method 1 > int t2 =...
1
by: Neil Zanella | last post by:
Hello all, In C and C++ a primitive data type is represented by a minimum number of bits as defined by the corresponding standard. For instance an int is assumed to be at least 16 bits on all...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
10
by: Noah Roberts | last post by:
I understand that different integer types might have time penalties because of word boundaries. Where can I get information clarifying this topic? A quick google didn't turn up much for me...nice...
3
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: ...
4
by: jehugaleahsa | last post by:
Hello: When developing data structures for C#, there is an obvious performance hit when utilizing primitive types. For instance, a recent hash table implementation I wrote works exceedingly fast...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.