I am currently working on a function where an object (of self written type Catalog
) is passed and shall not be mutated / changed in the called function. Here a quick example.
public override bool Migrate(in Catalog fromCatalog)
{
fromCatalog.SupplierId = 1; //works, but why
}
I want to strictly undermine this behaviour and disallow mutating any properties of the passed object.
Because the in modifier applies only to the variable (the parameter in this case). It prevents the parameter from having its value changed to refer to a different object.
and create a shallow copy of the object so that you don't change anything in the object you passed (within the class)
example:
utilization: