Click here to Skip to main content
16,005,181 members
Home / Discussions / C#
   

C#

 
AnswerRe: a question about image list (not imagelist class) Pin
Christian Graus14-Nov-05 12:42
protectorChristian Graus14-Nov-05 12:42 
Questionquestion about arrays Pin
savage_14-Nov-05 9:01
savage_14-Nov-05 9:01 
AnswerRe: question about arrays Pin
Christian Graus14-Nov-05 12:44
protectorChristian Graus14-Nov-05 12:44 
GeneralRe: question about arrays Pin
savage_14-Nov-05 16:18
savage_14-Nov-05 16:18 
GeneralRe: question about arrays Pin
Christian Graus14-Nov-05 16:26
protectorChristian Graus14-Nov-05 16:26 
GeneralRe: question about arrays Pin
S. Senthil Kumar14-Nov-05 18:41
S. Senthil Kumar14-Nov-05 18:41 
GeneralRe: question about arrays Pin
Christian Graus15-Nov-05 10:40
protectorChristian Graus15-Nov-05 10:40 
AnswerRe: question about arrays Pin
S. Senthil Kumar14-Nov-05 18:32
S. Senthil Kumar14-Nov-05 18:32 
Depends on how the SomeArray property has been implemented. If it's like
public int[] SomeArray
{
   get { return someArray; }
}

then no, no copies of the array are created, because array is a reference type. So only the reference to the array is copied.

However, this means that the client can do something like
int []arr = obj.SomeArray;
arr[0] = arr[1] = 0;


and this will result in the original array also being updated.

In case the programmer didn't want that to happen, he/she will code the SomeArray property as
public int [] SomeArray()
{
   return (int[] )((ICloneable)someArray).Clone();
}


in which case, everytime someone does obj.SomeArray[i], a new copy of the array is created.

In fact it's not recommended to have properties that return arrays for precisely this reason. According to MSDN[^],
Use a method when:
    ...
    * The member returns an array. Properties that return arrays can be very misleading. 
      Usually it is necessary to return a copy of the internal array so that the user cannot change internal state. 
      This, coupled with the fact that a user can easily assume it is an indexed property, leads to inefficient code. 
      In the following code example, each call to the Methods property creates a copy of the array. 


Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
GeneralRe: question about arrays Pin
savage_14-Nov-05 20:06
savage_14-Nov-05 20:06 
QuestionDrag&Drop selected Text, anyone knows something? Pin
_Kalix14-Nov-05 8:01
_Kalix14-Nov-05 8:01 
AnswerRe: Drag&Drop selected Text, anyone knows something? Pin
n10sive14-Nov-05 8:15
n10sive14-Nov-05 8:15 
GeneralText becomes unselected before the MouseDown event Pin
_Kalix14-Nov-05 20:04
_Kalix14-Nov-05 20:04 
QuestionTabControl problem. Pin
chettu14-Nov-05 8:00
chettu14-Nov-05 8:00 
QuestionCopy an Open File Pin
Osman Ishaque14-Nov-05 8:00
Osman Ishaque14-Nov-05 8:00 
AnswerRe: Copy an Open File Pin
dabuskol14-Nov-05 20:03
dabuskol14-Nov-05 20:03 
GeneralRe: Copy an Open File Pin
Osman Ishaque14-Nov-05 22:18
Osman Ishaque14-Nov-05 22:18 
QuestionUML Class Digram and C# Source Code in MS-Visio Pin
Naif_Prof14-Nov-05 5:56
Naif_Prof14-Nov-05 5:56 
QuestionVisual Studio Class Designer? Pin
Naif_Prof14-Nov-05 5:28
Naif_Prof14-Nov-05 5:28 
QuestionCancel Click In Inherited User Control Pin
rich_wenger14-Nov-05 4:55
rich_wenger14-Nov-05 4:55 
AnswerRe: Cancel Click In Inherited User Control Pin
Curtis Schlak.14-Nov-05 5:26
Curtis Schlak.14-Nov-05 5:26 
GeneralRe: Cancel Click In Inherited User Control Pin
rich_wenger14-Nov-05 5:42
rich_wenger14-Nov-05 5:42 
GeneralRe: Cancel Click In Inherited User Control Pin
rich_wenger14-Nov-05 8:52
rich_wenger14-Nov-05 8:52 
GeneralRe: Cancel Click In Inherited User Control Pin
Curtis Schlak.14-Nov-05 9:04
Curtis Schlak.14-Nov-05 9:04 
GeneralRe: Cancel Click In Inherited User Control Pin
rich_wenger14-Nov-05 9:48
rich_wenger14-Nov-05 9:48 
GeneralRe: Cancel Click In Inherited User Control Pin
rich_wenger14-Nov-05 9:58
rich_wenger14-Nov-05 9:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.