Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can the static class Tuple in .NET Framework 4.0 have new keyword to create instances?

Static Classes and Static Class Members

In this link Microsoft says,

a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.

I learned static class the same as stated above. But for the static class Tuple introduced in .NET Framework 4 can have new keyword to create a Tuple.

var population = new Tuple( "New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278);

Another example

Can anyone explain how is that possible?

like image 556
Sen Jacob Avatar asked Dec 04 '22 18:12

Sen Jacob


1 Answers

The static Tuple class is a factory class: it's job is just to provide an easy to way to construct tuples.

In reality, there are 8 tuple classes in .NET 4:

  • 7 Generic tuples you can create instances of, with up to 8 generic argument: Tuple<T1, T2>, Tuple<T1, T2, T3> and so on.
  • The static Tuple factory class which centralizes construction of the above.

So, you can't create an instance of a static class, but you can have several classes with the same name, if they have different generic arguments.

like image 187
Paul Turner Avatar answered Dec 10 '22 10:12

Paul Turner