C # from scratch single row King family --- tuple

From zero to single-row Series Description

The original idea was to write a blogger shed the cocoon into a butterfly series, but then I manifestation blog is difficult to achieve coherent and systems. So begins this blog bloggers will choose the more important and difficult to understand book of knowledge combined with their own practical experience to explain, is no longer scripted, bloggers hope to help everyone as much as possible doubts.

This blog will be to explain the following aspects of knowledge

  • What tuples are?
  • Why use a tuple?
  • How to use tuples?

What tuples are?

Tuple (tuple) is a fundamental concept in the relational database, the relationship is a table, each row in the table (i.e., each record in the database) is a tuple, is an attribute for each column. In the two-dimensional table, a tuple is also called a row

  • Currently divided into Tuple and ValueTuple
  • Tuple is the syntax of C # 4.0
  • ValueTuple is the syntax of C # 7.0

Why use a tuple?

Thoroughly understand what tuples, then when do we use it? The following points:

  • Usually we generally used to describe the class for the abstract things in life, there is time for some simple things abstract will be used to simulate arrays, but the meaning of the expression array is not very friendly, we can only be accessed through the index will result in Code into a "contractual" we'll convention Array [0] What does it mean, Array [1] What does it mean, over time, if you look at the code will not comment vomit fragrance ,,, and so tuple Defined
  (sting name,int age,double height)=("李羽飞",27,1.80);
  • A method can have only one return value in C #, but the actual business development is often a need for methods to return multiple return values, generally use the out keyword or a class before we return. Now we can return a tuple, neither out the need for new keyword to define variables do not need to re-write a class, very convenient.

How to use tuples?

See Code Example The following two kinds Tuple

   var studentInfoByTuple = Tuple.Create<string, int, int>("李羽飞", 27, 175);

   var studentInfoByValueTuple = (Name: "李羽飞", Age: 27, Height: 180);

   Console.WriteLine($"studentInfoByTuple: Name [{studentInfoByTuple.Item1}], Age [{studentInfoByTuple.Item2}], Height [{studentInfoByTuple.Item3}]");

   Console.WriteLine($"studentInfoByValueTuple: Name [{studentInfoByValueTuple.Name}], Age [{studentInfoByValueTuple.Age}], Height [{studentInfoByValueTuple.Height}]");

Guess you like

Origin www.cnblogs.com/liyufeia/p/11486246.html