For example,
class Age
{
public int Year
{
get;
set;
}
public Age(int year)
{
Year = year;
}
}
class Person
{
public Age MyAge
{
get;
set;
}
public Person(Age age)
{
age.Year = ( age.Year * 2 );
MyAge = age;
}
}
[Client]
Age a = new Age(10);
Person p = new Person( a );
When a new Age class is constructed the Year property is: 10. However, the Person class changes the Year to 20, even though there is no ref keyword…
Can someone explain why Year is not still 10?
A reference to the object is passed by value. With that reference, the object instance itself can be modified.
Consider the following example to understand what that statement means: