You have to remember that all types in Roslyn are immutable. That means that even operations like Add()
don't actually modify the object they're called on, they return the modified object.
This means that after Add()
, you have to work with the returned value, not the original object.
If you modify your code to the following, it will work:
declarator = declarator.Add(SyntaxFactory.VariableDeclarator(identifier, null, equalsClause));