Describes how to create and use a reference variable type. (about_Ref)

   
# TOPIC
about_Ref

# SHORT DESCRIPTION
Describes how to create and use a reference variable type.

# LONG DESCRIPTION
You can use the reference variable type to permit a method to change the
value of a variable that is passed to it.

When the [ref] type is associated with an object, it returns a reference
to that object. If the reference is used with a method, the method can
refer to the object that was passed to it. If the object is changed within
the method, the change appears as a change in the value of the variable
when control returns to the calling method.

To use referencing, the parameter must be a reference variable. If it is
not, an InvalidArgument exception is thrown.

The parameters used in method invocations must match the type required
by the methods.

Examples:

PS> function swap([ref]$a,[ref]$b)
>> {
>> $a.value,$b.value = $b.value,$a.value
>> }

PS> $a = 1
PS> $b = 10
PS> $a,$b
1
10
PS> swap ([ref]$a) ([ref]$b)
PS> $a,$b
10
1

PS C:\ps-test> function double
>> {
>> param ([ref]$x) $x.value = $x.value * 2
>> }

PS C:> $number = 8
PS C:> $number
8
PS C> double ([ref]$number)
PS C> $number
16

The variable must be a reference variable.

PS C:\ps-test> double $number
double : Reference type is expected in argument.
At line:1 char:7
+ double <<<< $number

SEE ALSO
about_Variables
about_Environment_Variables
about_Functions
about_Script_Blocks

C:\Windows>powershell get-help about_regular_expressions -full

ColorConsole [Version 1.7.1000] PowerShell 2.0-Export
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2014 Microsoft Corporation.

OS: Windows 10, 8.1, 8, 7, Vista / Windows Server 2016, 2012, 2008
»»»» ColorConsole









... Windows-10




Windows 10 How To

... Windows 10 FAQ
... Windows 10 How To




PowerShell: Describes how to create and use a reference variable type.

HTTP: ... PS_Windows/en/about_Ref.htm
0.187
11599

Wie wähle ich nicht benachbarte Dateien in der Explorer-Listenansicht aus?

 /

File Explorer view column and view settings are not saved, Why no?

 /

Windows run dialog on all MS Server OS!

 /

Wie kann man Apps aus dem Windows-10-Startmenü entfernen?

 /

Overview of all installed and not installed fonts on MS Windows 11, 10, ... OS!

 /

Activate Windows Defender Application Guard!

 /

How can I export the Explorer file list for Excel (XLS, CSV, HTML)?

 /

How long does it take to install Microsoft Office 365!

 /

Don't open automatically after Save to PDF?

 /

Wie kann ich das Windows 10 herunterfahren?

 /

Unter Windows 10/11 die Geräte Suche aktivieren und deaktivieren, aber wie!

 /

File Explorer Folder Options in Windows 10/11, can I find, open?

 /