Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
if a property accepts value of type int which is a readonly property then modifying the original private values is not possible from another class


for example:
C#
class cl1
{
 private int _num;
 public int Num //property without set accessor
 {
    get
    {
     return _num;
    }
 }
 public cl1() //Default constructor
 {
   _num=10;
 }
}
here if i try to modify _num variable from Num property
example:
C#
cl1 c = new cl1();
c.Num=10; //will generate error
this is not possible since the Num property of the cl1 class is a readonly property

whereas instead this if i have an int array
for example like this:
C#
class cl1
{
 private int[] _num;
 public int[] Num
 {
   get
   {
    return _num;
   }
 }
 public cl() //Default constructor
 {
   _num=new int{1,2,3,4};
 }
}
class cl2
{
 static void main()
 {
  cl1 c = new cl1();
  c.Num[0]=30; //here the modification in original list ie.in _num is valid
  int[] val= c.Num;
  foreach(int x in val)
  {
    //looping through each value and printing the values
  }
 } 
}
the output is now 30 2 3 4 instead of 1 2 3 4 even when the property Num in class cl1 is not allowed to make modification in the original private int[] arr _num
or does not has a "set" accessor in the Num propery.

how the modification in the original int[] array _num becomes possible.think iv'e hardly understood the concept,also i'm pretty confused with what's happening behind the scenes.
Posted
Updated 22-Jul-13 16:22pm
v2

1 solution

Alright this is a complicated topic so hold on...

Properties provide a reference to an object. Readonly properties (or properties with only Get accessors) means that something can access and manipulate the object, but not change where the object is pointing to (aka, create a new object and assign it to the property).

Since you've already declared your array, you are only changing the data inside of it, not where the array is pointing to. This works exactly the same with any other reference type, you can change the values of the object, but not create and assign the object to a new one.

In order to understand this, you really need to read up on value types versus reference types in the .NET framework. Do some searching on google and come back here if you have more questions. MSDN has some good references and even some CodeProject articles go into the topic pretty deeply.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Jul-13 22:26pm    
All correct, a 5. But I would need only one phrase:

"All correct, but, in second case, your property type is a reference type, and the property is read-only, so, indeed, you cannot modify the reference, but can modify the referenced object through the reference."

—SA
rtz87 25-Jul-13 21:02pm    
right.
Ron Beyer 22-Jul-13 22:32pm    
True, a lot of people overlook that an array in .NET is always a reference type, even if its an array of value types. Its part of the reason that reference types vs. value types can be a complicated subject for novice programmers.
Sergey Alexandrovich Kryukov 22-Jul-13 23:53pm    
Yes; and I remember a question where the inquirer asked how such a "value type" as string could be a class. I hope you understand how string intern pool works and how System.String mimic value semantic...
—SA
rtz87 23-Jul-13 0:25am    
if i'm not going wrong then basically what's happening is that the property in example two is of reference type since we are using integer array as a return type and arrays are of reference type. so this property is basically holding the actual address of the memory location where the object is located and since it's a read only property it does not allows modification to the reference where it's actually pointing to ie. we cannot modify the original reference and this is why something like c.Num= new int[]{2,5,7,9}; in an invalid modification in the code whereas c.Num=10 is valid since we are not actually modifying the reference which is not allowed since the property is read only instead we are modifying the actual data in the memory location to where the property in actually pointing to.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900