The deconstruction function in c#7.0---deconstruct

Source: Internet
Author: User
Deconstructing tuples

c#7.0 has added a number of features, one of which is the new tuple (ValueTuple), which allows us to return multiple values and work with deconstruction more easily, as in the following example

 static  void  Main (string  [] args) {( var  name   , var  Age) = GetUser (); Console.WriteLine ($   name:{name}\nage:{ AGE}    public  static  (string  name,int   age) GetUser () { return  ( "  Zhang San  " , 11   

You can see that the deconstructed tuple can write elegant code, and you can use type inference, but refactoring tuples here is not the point, so pay more attention, and here's an interesting feature

Deconstructing objects

The ability to deconstruct is not only a tuple, but also a more interesting function, which is to deconstruct the object. Does that sound interesting?

Static voidMain (string[] args) {    varuser =NewUser {Name="Zhang San", Age= One, Email="zhangsan@126.com", Sex="male"   }; (varNamevaremail) =user; Console.WriteLine ($"Name:{name}\nemail:{email}");}

The above code is not very surprised, anyway I first saw when I was particularly surprised, but also feel particularly interesting, then this is how to achieve, in fact, just add a destructor in the class (deconstruct) can, The destructor parameter method name must be deconstruct, the return value must be void, and the argument list must be out

 Public classuser{ Public stringName {Get;Set; }  Public stringEmail {Get;Set; }  Public intAge {Get;Set; }  Public stringSex {Get;Set; } //Deconstruction function      Public voidDeconstruct ( out stringName out stringemail) {Name=Name; Email=Email; }}

is not feeling particularly simple, haha,

Destructors also support overloading

classprogram{Static voidMain (string[] args) {      varuser =NewUser {Name="Zhang San", Age= One, Email="zhangsan@126.com", Sex="male"      }; (varNAME1,varEMAIL1) =user; Console.WriteLine ($"Name:{name1}\temail:{email1}"); (varName2,varAge2,varEMAIL2) =user; Console.WriteLine ($"Name:{name2}\tage:{age2}\temail:{email2}"); }}    Public classuser{ Public stringName {Get;Set; }  Public stringEmail {Get;Set; }  Public intAge {Get;Set; }  Public stringSex {Get;Set; } //Deconstruction function     Public voidDeconstruct ( out stringName out stringemail) {Name=Name; Email=Email; }    //structure function Overloading     Public voidDeconstruct ( out stringName out intAge out stringemail) {Name=Name; Age=Age ; Email=Email; }}

However, refactoring does not support overloading of parameter consistency

  

Even if the parameter types are inconsistent

  

  

It feels like a parameter type inference error, however,

  

So destructors are not overloaded with the same number of parameters, even if the parameter types are inconsistent

  

Related Article

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.

Tags Index: