-4

I've a dictionary object in my C# code:dictEmployees;--->

It has a list of all Employees.Employee class has two properties...Id and Salary.Lets say, Initially, this collection has a salary of 50K for employee Id 1.

Employee employee = new Employee();
dictEmployees.TryGetValue(1, out employee);

Now,if I modify employee' salary in other some method, I see that the salary is also being reflected in the employee object in the dictionary. Is this expected please?

Thanks.

Vineet v
  • 175
  • 2
  • 13

1 Answers1

1

Yes, Because Employee is reference type and when you pass reference type only reference gets passed so when your method performs some modifications on the object it gets reflected in the original one. But on other hand when you pass value type copy of value gets passed and modification on the value does not reflect on the original one.

Saket Choubey
  • 916
  • 6
  • 11