C # 7.0 new feature 1 based on tuple "multiple" return value method _c# Tutorial

Source: Internet
Author: User

Original link: http://www.cnblogs.com/ylvict/p/5573094.html

Review
First, ask a question, in C #, how do you make a method return "multiple" return values?
Let's take a look at the c#6.0 and earlier versions of the procedure.
In C #, there are usually 4 ways in which a method returns more than one piece of data.
• Use keyvalue combination


static void Main (string[] args)
{
 int int1 =;
 int int2 =;
 var result = Add_multiply (int1, int2);
 Console.WriteLine (Result. Key);
 Console.WriteLine (Result. Value);
}

private static Keyvaluepair<int, int> add_multiply (int int1, int int2)
{
 var keyvaluepair = new Keyvaluep Air<int, int> (int1 + int2, int1 * int2);
 return keyvaluepair;
}

• Use ref/out parameters

ref

static void Main (string[] args)
{
 int int1 =;
 int int2 =;
 int add = 0;
 int multiply = 0;
 Add_multiply (Int1, Int2, ref add, ref Multiply);
 Console.WriteLine (add);
 Console.WriteLine (multiply);
}

private static void add_multiply (int int1, int int2, ref int ADD, ref int Multiply)
{
 add = int1 + Int2;
 Multiply = int1 * INT2;
}

out

static void Main (string[] args)
{
 int int1 =;
 int int2 =;
 int add = 0;
 int multiply = 0;
 Add_multiply (Int1, Int2, out Add, out Multiply);
 Console.WriteLine (add);
 Console.WriteLine (multiply);
}

private static void add_multiply (int int1, int int2, out int Add, out int Multiply)
{
 add = int1 + Int2;
 Multiply = int1 * INT2;
}

• Use struct or class struct

struct result
{public
 int add;
 public int multiply;
}
static void Main (string[] args)
{
 int int1 =;
 int int2 =;
 var result = Add_multiply (int1, int2);
 Console.WriteLine (Result.add);
 Console.WriteLine (result.multiply);
}

private static result add_multiply (int int1, int int2)
{
 var result = new result
 {
  Add = int1 + int2,< c18/>multiply = Int1 * Int2
 };
 return result;
}

class

Class result
{public
 int add;
 public int multiply;
}
static void Main (string[] args)
{
 int int1 =;
 int int2 =;
 var result = Add_multiply (int1, int2);
 Console.WriteLine (Result.add);
 Console.WriteLine (result.multiply);
}

private static result add_multiply (int int1, int int2)
{
 var result = new result
 {
  Add = int1 + int2,< c41/>multiply = Int1 * Int2
 };
 return result;
}

Dynamic

static void Main (string[] args)
{
 int int1 =;
 int int2 =;
 var result = Add_multiply (int1, int2);
 Console.WriteLine (Result.add);
 Console.WriteLine (result.multiply);
}

private static dynamic add_multiply (int int1, int int2)
{
 var result = new
 {
  Add = int1 + int2,
  mu ltiply = Int1 * Int2
 };
 return result;
}



• Use Tuple

static void Main (string[] args)
{
 int int1 = =;
 int int2 =;
 var result = Add_multiply (int1, int2);
 Console.WriteLine (Result. ITEM1);
 Console.WriteLine (Result. ITEM2);
}

private static Tuple<int, int> add_multiply (int int1, int int2)
{
 var Tuple = new Tuple<int, int> (in T1 + int2, int1 * int2);
 return tuple;
}

Okay, there's a lot of crap back there. Let's take a look at the wording in c#7.0.
new Feature (c#7.0)
The old rules, first code.

static void Main (string[] args)
{
 int int1 = =;
 int int2 =;
 var result = Add_multiply (int1, int2);
 Console.WriteLine ($ "ADD: {result.add}, Multiply: {result.multiply}");
 (var add, var multiply) = Add_multiply (Int1, int2);
 Console.WriteLine ($ "Add: {add}, Multiply: {Multiply}");
}
public (int add, int multiply) add_multiply (int int1, int int2) 
 => (int1 + int2, int1 * int2);

What do you think? Compared to 6.0 and previous C #, there is no a very refreshing feeling.
In fact, just based on tuple to do the grammar of Simplified grammar, just give people a number of return values illusion.

Summary:
This feature, though not so inspiring, has solved some of the itch points of many yards of farmers.
1. Look at the keyvalue on the way, it would have been a very simple operation, the written code will appear very clumsy, when the value of the key to obtain. And, most importantly, if it's not running, the code outside calls is unaware of the key.
2. Besides ref/out, this approach should be the most popular writing in the traditional sense. Even the characteristic of c#7.0 can not eliminate the nature of ref in a certain situation. But at least in the case where ref is used to return a value, the style shown in the code is clearly inconsistent with the actual logic, but it is not reasonable to return the value in the form of a parameter.
3. The way of struct and class is not much said, if you are dealing with an entity, but also can make sense, but if the purpose is to return a number of related data is not, specifically for the transfer between the method and add a useless model class or structure, can only be said that the solution under the helpless. Dynamic Although there is no such problem in the form of representation, the problem is that the external calling code simply does not know what is coming out of the method unless it is running.
4. When it comes to traditional tuple, it's actually the closest thing to the feature, but look at the call's *. item1,*. Item2.. God knows what they are. Even in the implementation method, it is also the face of this kind of see the actual meaning of the value represents confused.
Finally, explain, currently (June 2016) c#7.0 has not officially released, if you want to experience some features, you can go to download VS15 preview, the final release of the grammar may be mentioned in this article is different, the latest dynamic please pay attention to the Roslyn project.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.