In our Exploring the .NET Framework series, we’ve covered some terminology, creating instances of objects and calling methods. In today’s installment, we are going to look at using static members - methods and properties. Static methods are methods that a class (or type) make available without needing to create an instance of the class. Similarly, static properties are properties that you can access without needing an instance of the class. Many classes provide static members supply functionality where it doesn’t make sense to need an object.
The System.Math class contains a couple of static properties, PI and E.
PS C:\scripts\PowerShell> [math]::E 2.71828182845905 PS C:\scripts\PowerShell> [math]::PI 3.14159265358979
Constant values are not the only type of property that you’ll find. The System.AppDomain class has a static property, CurrentDomain, which is an AppDomain instance referring to the current application domain.
PS C:\scripts\PowerShell> [AppDomain]::CurrentDomain.gettype()
IsPublic IsSerial Name BaseType ——– ——– —- ——– True False AppDomain System.MarshalByRefObject
System.Math also contains a number of static methods that provide a great deal of basic mathematical function.
PS C:\scripts\PowerShell>[Math] | get-member –static –membertype method | select name
Abs Acos Asin Atan Atan2 BigMul Ceiling Cos Cosh DivRem Equals Exp Floor IEEERemainder Log Log10 Max Min Pow ReferenceEquals Round Sign Sin Sinh Sqrt Tan Tanh Truncate
(V2 CTP3 - We can use the trick of using the method name without parenthesis following to get additional information on each of the methods that we are interested in.)
One type of static method that I would like to highlight is the factory method. A factory method is a common way of controlling object creation. At its most basic level, a factory pattern is a method that creates objects of a specified type.
An example of this is the System.Net.WebRequest’s Create method.
PS C:\scripts\PowerShell> $web = [net.webrequest]::create(’http://www.google.com’) PS C:\scripts\PowerShell> $web.gettype()
IsPublic IsSerial Name BaseType ——– ——– —- ——– True True HttpWebRequest System.Net.WebReq…
The factory method Create used the argument (‘http://www.google.com’) to determine the type of WebRequest object to create. In this case, it created an HttpWebRequest object, which is a more specialized version of the WebRequest for HTTP requests.
Let’s see how it would respond if we passed the Create method a URI with FTP.
PS C:\scripts\PowerShell> $web = [net.webrequest]::create(’ftp://ftp.google.com') PS C:\scripts\PowerShell> $web.gettype()
IsPublic IsSerial Name BaseType ——– ——– —- ——– True False FtpWebRequest System.Net.WebReq…
As we can see, the WebRequest class created an FtpWebRequest object.
Factory methods provide developers a way to more flexibly support different operations by allowing the static method to determine which object to return.
UPDATE: Joel “Jaykul” Bennet added a V2 advanced function to convert static methods to functions to PoShCode.org and pointed out Oisin Grehan’s blog post about creating functions from static methods (V1 compatible).
I’ve been really happy to receive some of the feedback (positive and constructive criticism) on this series. There is a lot of the .NET Framework to cover, and I’m open to suggestions as to what you would like to know. Please post a comment on the blog or email me at Steve at UsePowerShell.Com.