Why can the static Tuple class in the .NET Framework 4.0 have a new instance keyword?

Static classes and members of a static class

In this regard, Microsoft says:

static class could not be created. In other words, you cannot use the new keyword to create a class type variable. Since there is no instance variable, you are accessing members of a static class using the class name itself.

I studied the static class as above. But for the static Tuple class introduced in the .NET Framework 4, a new keyword can be created to create a Tuple.

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

Another example

Can anyone explain how this is possible?

+4
source share
5 answers

The Tuple static class is a factory class: this task is just for simple tuple creation.

There are actually 8 tuple classes in .NET 4:

  • 7 Generic tuples, you can create instances with up to 8 common arguments: Tuple<T1, T2> , Tuple<T1, T2, T3> , etc.
  • The static Tuple factory class, which centralizes the construction above.

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

+5
source

The Tuple Class is static and cannot be created.

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

There is another class, Tuple <T1, T2, T3, T4, T5, T6, T7> Class . This class is not static and can be created.

 // compiles var population = new Tuple<string, int, int, int, int, int, int>("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278); 
+4
source

The reason this looks weird is because your example is incorrect:

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

Are you right in thinking that it looks weird because a: Tuple is static and b: how does it know which args constructor accepts? This is because the correct line is:

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

where Tuple.Create is a group of methods several generalized overloaded methods, i.e. Tuple.Create<T1>(T1 arg) , Tuple.Create<T1, T2>(T1 arg1, T2 arg2) , etc. The compiler uses type type inference to automatically select the correct 7 generic types, so your string is actually compiled as:

 Tuple<string,int,int,int,int,int,int> population = Tuple.Create<string,int,int,int,int,int,int>("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278); 

So: it uses fairly standard language features:

  • type-based type-based inference for correct overload Create
  • implicit output type ( var ) based on the return type of the method
+3
source

This is actually pretty simple:

There is a static Tuple class, which is a factory for non-static Tuple<T> , Tuple<T1, T2> , Tuple<T1, T2, ...> classes. Pay attention to the difference in the general parameters.

+2
source

You are talking about two different types: static, which cannot create an instance, and the second with constructors.

+1
source

Source: https://habr.com/ru/post/1396979/


All Articles