tl;dr
Because the $Package.Connections
property contains an instance of a .NET reference type, namely Microsoft.SqlServer.Dts.Runtime.Connections
, $x
and $Package.Connections
reference the very same collection instance, so $x.Remove("Something")
is the same as $Package.Connections.Remove("Something")
The behavior depends on whether a given value is an instance of a .NET reference type or value type:
When reference-type instances are assigned to a (new) variable / passed as an argument, it is the reference ("pointer") to the actual data that is copied, which means that both the original value and the target variable / parameter refer to the same object.
By contrast, assigning / passing a value-type instance makes a copy of the value itself, resulting in independent copies of the data.
You can examine an object stored in a given variable $x
as follows: $true
indicates a value-type instance, $false
a reference-type instance:
$x.GetType().IsValueType
Note that collection-like types, including arrays, are reference-type instances, so $x
and $Package.Connections
in the code in your question will refer to the same collection.
By contrast, all so-called primitive types, such as numbers, are value types.
If you don't want to rely on inspection at runtime, you can examine the documentation for a given type, which (with the language set to C#) will use the following "type-kind" identifiers:
- Reference types:
- Value types: