What is the Triplet class used for? Is it related

2020-04-05 07:58发布

问题:

So I just learnt about the Triplet class. I have no experience with ASP.NET, only the core .NET Framework.

Can someone explain to me where/why the Triplet class exists? Is it like a Tuple?

回答1:

Yes, it's pretty much like Tuple from .NET 4.0, but dates back to .NET 1.0 and ASP.NET 1.0 in particular. It's primarily used in ViewState serialization:

The Page class contains a SavePageViewState(), which is invoked during the page life cycle's save view state stage. The SavePageViewState() method starts by creating a Triplet that contains the following three items:

  1. The page's hash code. This hash code is used to ensure that the view state hasn't been #tampered with between postbacks. We'll talk more about view state hashing in the "View State and Security Implications" section.
  2. The collective view state of the Page's control hierarchy.
  3. An ArrayList of controls in the control hierarchy that need to be explicitly invoked by the page class during the raise postback event stage of the life cycle.

There's also its' younger brother called Pair.

There's absolutely no reason you should even bother about these classes or else an unholy untyped mess will ensue.



回答2:

Sounds like it's got one more object than a Pair. You use it when you need to return exactly three items.

C# and Java are different from Python, which will convert multiple return values into a tuple.

A Triple does sound like a tuple to me - one that has exactly three items.