Not sure I'm able to formulate this question in a way someone would simply understand, so lets have a cool
marketing example:
public class Part
{
public MemberType member;
...
}
public class Product
{
public Part part1;
...
}
...
Product product = new Product();
I need to modify the public product
's part1
. So, the natural method is to write something like:
product.part1 = new Part();
Now, an algorithm (let's say a sort of search one) would go through the product
object and identify the part1
as an interesting part and returns reference to it:
Part Search(Product product)
{
Part part = null;
...
part = product.part1;
...
return part;
}
...
interesting_part = Search(product);
We can alter the product
object via the interesting_part
like
interesting_part.member = whatever;
Now, the question: in c/c++ if the Product.part1
is pointer to Part
and Search returns address of this pointer, we could replace the part1
just by assigning new value to this address. AFAIK this is not possible for c# reference:
interesting_part = new Part();
Just creates new object and copies its reference to the interresting_part
, but without knowing the member parent (product
object), we are not able to modify the (product.part1
) reference, just its content. We would need second level of the reference.
Is there something like "ref reference" type which would accept reference addresses? In such hypothetical case the search would return ref Part
and assigning to such value would replace the referenced object with the new one.
Thanks.