Home c# Cortiers in C # (their pros and cons)

Cortiers in C # (their pros and cons)

Author

Date

Category

In C # there is such a collection as a cortem and I have a question. What is the difference between tuples and arrays and where each type is dominated and in C # the motorcade is meant how the data is immutable or somehow different?


Answer 1, Authority 100%

so in the documentation Written . But if you briefly – this is an easy way to group several variables (often differently different types, and generally connected to each other) in the lightweight object, for example, to return it from the function and then get easy access to its elements (in the latest versions of C # is available on the task The names of the fields, and not only by title-index).

  • System.Tuple – traditional, non-changing cortices and work with them is not very convenient
  • system.valuetuple – changeable cortices from the modern version of C #, more convenient in circulation

An area is a collection of elements of the same type with access to an array elements by numerical index.


Answer 2

Corgers are not arrays and not at all collection.
By your device, they resemble the structures more.

There are 2 types: System.Tuple and System.Valuetuple

Plus: The cortices are compact, not necessarily a separate description as in the case of structures and classes (but it is possible) are ideal for returning a group of values ​​from the method.
For example: Return (1, “A”);

Minus: It is difficult to implement the internal logic of the object (methods through the expansion methods, the absence of private fields), which will be very limited.

Cortiers on the example of 2 fields: int and string (maybe others)

(int, string) tuple1 = (1, "a"); // Access to items through Item1 and Item2
(int index, String CHARS) TUPLE2 = (1, "A"); // Access to items through Index and Chars (only such a motorcade may contain named fields)
using mytuple = System.Tuple & lt; int, string & gt ;; // Class-based motorcade (maybe NULL)
MyTuple MT = New Tuple & LT; INT, STRING & GT; (1, "A"); // Access to items through Item1 and Item2
using mytuple = System.Valuetuple & lt; int, String & GT ;; // Meat Struct (can not be NULL)
MYTUPLE MT = (1, "A"); // Access to items through Item1 and Item2
using mytuple = System.Valuetuple & lt; int, String & GT ;;
MyTuple? mt = null; // Substitute LIKE NULLABLE STRUCT (maybe NULL)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions