Static properties

Properties require an instance of a type to be created before they can be accessed. Static properties, on the other hand, don't.

A static property is a piece of data; in some cases, this includes constant values, associated with class definitions, that can be retrieved without creating an object instance.

MSDN shows static properties using an S symbol in the leftmost column. For example, the System.Text.Encoding class has a number of static properties denoting different text encoding types, shown in the following screenshot:

PowerShell is also able to list the static properties for a type (or class) using Get-Member with the Static switch:

PS> [System.Text.Encoding] | Get-Member -MemberType Property -Static

TypeName: System.Text.Encoding

Name MemberType Definition
---- ---------- ----------
ASCII Property static System.Text.Encoding ASCII {get;}
BigEndianUnicode Property static System.Text.Encoding BigEndianUnicode {get;}
Default Property static System.Text.Encoding Default {get;}
Unicode Property static System.Text.Encoding Unicode {get;}
UTF32 Property static System.Text.Encoding UTF32 {get;}
UTF7 Property static System.Text.Encoding UTF7 {get;}
UTF8 Property static System.Text.Encoding UTF8 {get;}

These static properties are accessed using the following generalized notation:

[<TypeName>]::<PropertyName> 

In the case of System.Text.Encoding, the ASCII property is accessible using the following syntax:

[System.Text.Encoding]::ASCII 

A variable may be used to represent either the type or the property name, as follows:

$type = [System.Text.Encoding] 
$propertyName = 'ASCII' 
$type::$propertyName 

Fields are often used as part of the internal implementation of a class (or structure). Fields aren't often accessible outside of a class.

The Int32 structure exposes two static fields, holding the maximum and minimum possible values that the type can hold:

PowerShell does not distinguish between fields and properties. The following statements show the values of each static field in turn:

[Int32]::MaxValue 
[Int32]::MinValue 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.220.120