Home > database >  Does "is" type test with declaration pattern break reference to element in C#
Does "is" type test with declaration pattern break reference to element in C#

Time:10-08

Let's say I have a function that changes property on an object like this:

private void SetTagsInChildren(List<string> tags, Model model)
{
    ContentModel contentModelV1;

    if (model is ContentModel contentModel)
         contentModelV1 = contentModel;

    // Logic to set new tags in model
}

Does the declaration of new variable contentModel after the is type check (or the assignment that goes after it) break the reference to the original model parameter ? Are the adjustments made on contentModel or contentModelV1 visible on the original model that is passed to the function ?

CodePudding user response:

If model is a reference type then is performs a reference conversion (if the match is succesful). A reference conversion only converts the static type of the reference (the compile time type of the variable), the runtime objet it is "pointing" to is exactly the same.

  • Related