Plus (+) in .NET Class Names

Plus (+) in .NET Class Names

  • Comments 2

On one of our internal discussion aliases, someone asked why do we need a plus (+) the class name for FTP?

Typically, you would expect this to work [Net.WebRequestMethods.Ftp], but it doesn’t. The solution is [Net.WebRequestMethods+Ftp] and it has something to do with nested classes

In C#, a nested class looks something like

public class List
{
                public class Node // Node is inside List
                {
                                // Node stuff
                }
                // List stuff...
}

Usually, nested classes are used only by its container. In fact, its a design guideline to keep the nested class private

From - .NET Framework Developer's Guide, Nested Types (http://msdn.microsoft.com/en-us/library/ms229027.aspx)
• Avoid publicly exposed nested types. The only exception to this is when variables of the nested type need to be declared in rare scenarios such as subclassing or other advanced customization scenarios.
• Do not use nested types if the type is likely to be referenced outside of the declaring type.

So public/useful nested classes are rare, but some people will bump into them. Where does the plus sign come from?

The plus sign comes from .NET Reflection. If you do [Net.WebRequestMethods+Ftp].FullName you will see that fullname is "System.Net.WebRequestMethods+Ftp"

MSDN Documentation on Type.GetMethod http://msdn.microsoft.com/en-us/library/aa332510(VS.71).aspx has a fairly indepth discussion on the plus and other symbols

It says,

To Get : A parent class and a nested class
Use : Type.GetType("MyParentClass+MyNestedClass")

PowerShell is using .NET under the covers, .NET Reflection to be exact. We don’t want to redocument the .NET Type system, but in times where it causes confusion for PowerShell users, we'll try to clarify things

.Net speed bumps,
Ibrahim Abdul Rahim [MSFT]

Leave a Comment
  • Please add 5 and 2 and type the answer here:
  • Post
Page 1 of 1 (2 items)