Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

c# array of objects

  • 12-02-2014 3:32pm
    #1
    Registered Users, Registered Users 2 Posts: 1,723 ✭✭✭


    I asked recently about using c# or c++ for a windows application. http://www.boards.ie/vbulletin/showthread.php?t=2057140055

    have gone with c# and I now have a question about heap memory and reference variables.
    If I have 2 collections of object and I populate the second collection from the first ( where some details match) is another copy of the object created or do the two reference variables point to the same object. My understanding is that the latter is the case, i.e. only one copy of the object exists.

    I will have a total of about 400 objects on the collection and I am concerned about duplicating this amount of data unnecessarily. But the answer to this will have design implications so any clarification would be much appreciated .

    thanks


Comments

  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    Depends how you write the code. You can create new objects and copy the data if you want, or you can just store references in both containers.


  • Registered Users, Registered Users 2 Posts: 1,723 ✭✭✭rock22


    Thanks Srsly78,
    I should have explained better

    If I have code like this
    for (int I =0 ;I<myCollection.length:I++)
    {
    if( (myObject) myCollection.getImportantValue()== whatIwant)
    {
    myOtherCollection.add(myObject) myCollection);
    }
    }

    I assume I am only adding a reference to the same object. i.e. myCollection and myOtherCollection both have references to the same object.

    ps. My syntax may not be correct but I will correct that when I code.

    thanks for replying


  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    Yes that is adding a reference I believe.

    If you want to add a copy then create a new object, copy the data from other object (using copy constructor maybe). Then add reference to the new object.


  • Registered Users, Registered Users 2 Posts: 1,723 ✭✭✭rock22


    Thanks again Srsly,

    srsly78 wrote: »
    Yes that is adding a reference I believe.

    If you want to add a copy then create a new object, copy the data from other object (using copy constructor maybe). Then add reference to the new object.

    No I don't want to create a copy - just adding a new reference to the same object. I can go ahead and code it now.
    regards
    gerard


  • Registered Users, Registered Users 2 Posts: 586 ✭✭✭Aswerty


    LINQPad is a nice tool for testing things in C#. It has a REPL so it is really handy for checking out how things work.

    Just in relation to your example code, C# has a really powereful querying feature called LINQ that allows you to easily and neatly query collections. You can perform the example code using LINQ as neatly as the below (syntax may not be exact).
    var copyValues = myCollection.Where(x => x.getImportantValue() == whatIwant);
    myOtherCollection = myOtherCollection.Union(copyValues);
    
    // or just use the following if myOtherCollection contains nothing
    myOtherCollection = myCollection.Where(x => x.getImportantValue() == whatIwant);
    


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,723 ✭✭✭rock22


    Thanks
    I have just heard of Link and it does seem to provide the functionality I need. This is my first c# application so I am fairly sure I could do some things more efficiently. I will certainly try to code it this way. The latter line of code would seem to do what I want. I again assume that it copies references to the new collection and not create new object?

    I have ordered a Microsoft visual c# step by step book and will read into using Link when I get it. For now I am happy that my design will not end up duplicating a whole lot of data.

    I assume that the debugger in Visual c# will give me some information but I haven't figured it out yet!

    thanks again


  • Registered Users, Registered Users 2 Posts: 11,989 ✭✭✭✭Giblet


    If you are using value types, the values will copy unless they are boxed in both arrays.


  • Registered Users, Registered Users 2 Posts: 586 ✭✭✭Aswerty


    When you're dealing with reference types unless you explicitly perform a deep copy (e.g. maybe using a copy constructor such as srsly78 referred to) you never have to worry about duplicating the in memory data the reference points to. Though I'm sure there are some edge cases where you do have to worry but I don't think I've bumped into this myself with 3 years C# development. Normally the problem is that I have to go out of my way to perform a deep copy.

    If you have dealt with pointers before (a quick Google tells me Pascal has them) your question is like asking "if I copy a bunch of pointers will this duplicate what the pointer is pointing to" and the answer is of course, no. If duplication did occur the reference types would serve the same purpose as the value types which would be redundant.

    The above applies to using LINQ as well.

    The type system in C# is very interesting and I'd definitely recommend reading up on it. Once you have value and reference types sorted you can get stuck into generic types (though probably get a bit more familiar with C# and .Net before starting on them).


  • Registered Users, Registered Users 2 Posts: 1,723 ✭✭✭rock22


    Aswerty wrote: »
    ....
    If you have dealt with pointers before (a quick Google tells me Pascal has them) your question is like asking "if I copy a bunch of pointers will this duplicate what the pointer is pointing to" and the answer is of course, no. If duplication did occur the reference types would serve the same purpose as the value types which would be redundant.

    The above applies to using LINQ as well.

    The type system in C# is very interesting and I'd definitely recommend reading up on it.....

    Thanks for that.
    Yes I had assumed no copy would be made based on my experience with Pascal but I just wanted to be sure.
    I have ordered a c# tutorial so I will read up on the data types ( and Link) when it comes
    thanks again


  • Registered Users, Registered Users 2 Posts: 2,040 ✭✭✭Colonel Panic


    A C# reference points to an object on the managed heap, a C/C++ pointer points to some memory. So with C# references you get some of the behaviour of pointers, but not stuff like pointer arithmetic. It makes the memory model slightly higher level than native languages but conceptually, they seem similar.

    Good rule of thumb is that the only time you have an actual allocation is if you create a new instance of a class.


  • Advertisement
Advertisement