|
-
August 8th, 2012, 02:34 PM
#4
Re: Problem related to the use reference
That's right. But, a word of caution. As I already hinted, C# has two kinds of types - reference types (classes), and value types (structs). Reference types are kind of like smart pointers, so if you want to create a copy, you need to explicitly call a method that creates one (defined by you for your own types, or an existing one for 3rd party types). You can find out the type of each variable by simply hovering your mouse pointer over it in the IDE; it will also say if it's a class or a struct. And of course, you can always check the MSDN documentation for the type.
Now, arrays, lists and such, reference types themselves, can hold any other kind of type. Elements can be structs or classes. If you were using the generic List<int>, you would get the appropriate pass-by value behavior for methods like AddRange - since int is a value type, a copy would be passed and stored in the array. If you were using List<object1> (pass-by-reference semantics), and passed in a bunch of elements also contained in another list or an array, or some other collection, the two collections would end up pointing to the same elements (they would share elements). Clearing one (or inserting/removing elements of one) would not affect the other, but if you changed a property of an element in a list, both lists would reflect this change (since they share the element).
Code:
listA: listB:
[0]---------------> [object0] <---------------[0]
[1]---------------> [object1] <---------------[1]
[2]---------------> [object2] <---------------[2]
[3]---------------> [object3] <---------------[3]
[4]---------------> [object4] <---------------[4]
// operations on the list affect only one list
listA: listB.Remove(object3):
[0]---------------> [object0] <---------------[0]
[1]---------------> [object1] <---------------[1]
[2]---------------> [object2] <---------------[2]
[3]---------------> [object3] x
[4]---------------> [object4] <---------------[3]
// Operations on elements affect both:
listA[4].Message = "hello";
string msg = listB[3].Message; // msg is "hello"
Sometimes, this is what you want, sometimes it's not. If not, than you need to create a copy of each element before you pass them in, either one by one, or, if the collection supports it, by copying the entire input collection.
Since you're using the non-generic ArrayList class, which stores everything as object references, value types get "boxed" into an object instance (object is a reference type), so I suspect that this means that ArrayList must use pass-by-reference semantics, so the elements are shared, but I didn't check. This wouldn't be a problem if the object1 constructor accepted, say, an array of integers (int[]) or a List<int>, since in that case, each element would be retrieved by value before it would be stored in mChiffres. But since it's a collection of object-s, then I think the elements will be shared.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|