Bookmark and Share

Pair Has Changed But Is Not Going Away

I wrote earlier about creating a factory method for KeyValuePair<K, V> similar to the factory method in the Tuple class. Like the KVP, a two element Tuple is known as a pair as well. But did you know there exists another Pair class in the .NET Framework?

System.Web.UI.Pair sits in System.Web.dll. It’s the dirtiest class of all as it isn’t even generically-typed. Here is the definition.

[Serializable]
[AspNetHostingPermission(SecurityAction.LinkDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class Pair
{
    public object First;
    public object Second;

    public Pair()
    {
    }

    public Pair(object x, object y)
    {
        this.First = x;
        this.Second = y;
    }
}

I decided to take a look at it in .NET 4 to see if there were any differences. I didn’t expect much as I’m sure it would break many pieces of code, but it would see that Pair should at least inherit from Tuple<object, object> then have its First and Second properties mapped to Item1 and Item2. In an ideal world, Pair would be an alias for Tuple<T1, T2>.

[Serializable]
public sealed class Pair
{
    public object First;
    public object Second;

    [TargetedPatchingOptOut(
        "Performance critical to inline this type of method across NGen image boundaries")]
    public Pair()
    {
    }

    [TargetedPatchingOptOut(
        "Performance critical to inline this type of method across NGen image boundaries")]
    public Pair(object x, object y)
    {
        this.First = x;
        this.Second = y;
    }
}

No such luck on the inheritance hierarchy, but there are a few attribute changes.

First, of all, the AspNetHostingPermission attribute was removed. This is a code access security attribute that grants the minimum permissions required for execution to the caller of this class. I compared the usage of this attribute in System.Web 2.0 and System.Web 4 (there is no inbetween release for that assembly) and it appears that it is being removed from nearly everything. The attribute is still used in a few places, but it is almost completely removed from the System.Web.UI namespace (with the exception of PageParserFilter and Page.get_LastFocusedControl()). I wouldn’t be surprised to see it further deprecated by the time .NET 4 goes live. For reference, I am comparing it with Beta 2.

The next thing you will notice is the addition of the TargetedPatchingOptOut attribute. When you generate a native assembly, this attribute informs the native assembly generator to inline the method across image boundaries. This will improve performance for your native assemblies.

I am disappointed the antiquated Pair class is still in the Framework, but I understand that to change it would cause many people headaches. It is interesting to see how such a benign class has been changed in the process of upgrading System.Web.dll since its last release in 2005.

blog comments powered by Disqus

KodefuGuru.GetInfo()

Chris Eargle
LinkedIn Twitter Technorati Facebook

Chris Eargle
Telerik Developer Evangelist, C# MVP

JustCode

Telerik .NET Ninja

 

INETA Community Speakers Program

 

MVP - Visual C#

 

Friend of RedGate

World Map

Tag cloud

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.