ArrayList and primitive types

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bear

    #1

    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



  • Thomas F.B.

    #2
    Re: ArrayList and primitive types

    On Sun, 02 Nov 2003 14:16:45 +0100, Bear wrote:[color=blue]
    >
    > 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
    >[/color]

    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.ValueTyp e 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: tarball@phreake r.foo (foo = net)

    Comment

    • Eric Gunnerson [MS]

      #3
      Re: ArrayList and primitive types



      --
      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" <bdr@NOSPAM.sol .dk> wrote in message
      news:3fa503b9$0 $7181$ba624c82@ nntp05.dk.telia .net...[color=blue]
      > 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[/color]
      int[color=blue]
      > just a "typedef" of the Int32 structure and, if so, does the runtime[/color]
      system[color=blue]
      > 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
      >
      >[/color]


      Comment

      • Rakesh Namineni[MSFT]

        #4
        RE: ArrayList and primitive types

        Bjoern,
        C# is a language where everything (value & reference types) is an object.
        That is why the ArrayList.Add(o bject) takes int.

        --------------------[color=blue]
        >From: "Bear" <bdr@NOSPAM.sol .dk>
        >Newsgroups: microsoft.publi c.dotnet.langua ges.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: <3fa503b9$0$718 1$ba624c82@nntp 05.dk.telia.net >
        >NNTP-Posting-Host: 62.107.67.208
        >X-Trace: 1067779002 news.stofanet.d k 7181 62.107.67.208
        >X-Complaints-To: Telia Stofa Abuse <abuse@stofa.dk >
        >Path:[/color]
        cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!new sfeed00.sul.t-online.de!newsf ee
        d01.sul.t-online.de!t-online.de!eusc. inter.net!news. tele.dk!news.te le.dk!sma
        ll.news.tele.dk !newsfeed101.te lia.com!nf01.dk .telia.net!news 104.dk.telia.ne t
        !not-for-mail[color=blue]
        >Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1960 52
        >X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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[/color]
        int[color=blue]
        >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
        >
        >
        >[/color]


        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

        Comment

        Working...