C# 7 - Tuple return types and deconstruction
نویسنده: وحید نصیری
تاریخ: ۱۳۹۵/۱۲/۲۴ ۱۴:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class Location
{
public string City { get; set; }
public string State { get; set; }
public Location(string city, string state)
{
City = city;
State = state;
}
} var location = new Location("Lake Charles","LA"); var location = new Tuple<string,string>("Lake Charles","LA");
// Print out the address
var address = $"{location.Item1}, {location.Item2}"; static Tuple<int, string, string> GetHumanData()
{
return Tuple.Create(10, "Marcus", "Miller");
} var data = GetHumanData();
Console.WriteLine("What is this value {0} or this {1}", data.Item1, data.Item3); (int x1, string s1) = (3, "one");
Console.WriteLine($"{x1} {s1}"); error CS8179: Predefined type 'System.ValueTuple`2' is not defined or imported
PM> install-package System.ValueTuple
int x2;
string s2;
(x2, s2) = (42, "two");
Console.WriteLine($"{x2} {s2}"); static (int, int) Divide(int x, int y)
{
int result = x / y;
int reminder = x % y;
return (result, reminder);
} (int result, int reminder) = Divide(11, 3);
Console.WriteLine($"{result} {reminder}"); (var result1, var reminder1) = Divide(11, 3);
Console.WriteLine($"{result1} {reminder1}"); var (result1, reminder1) = Divide(11, 3);
static (int, string, string) GetHumanData()
{
return (10, "Marcus", "Miller");
} (int Age, string FirstName, string LastName) results = GetHumanData(); Console.WriteLine(results.Age); Console.WriteLine(results.FirstName); Console.WriteLine(results.LastName);
ValueTuple<int, int> tuple1 = Divide(11, 3);
(int, int) n = (1,1); System.Console.WriteLine(n.Item1);
ValueTuple<int, int> n = new ValueTuple<int, int>(1, 1); System.Console.WriteLine(n.Item1);
(int,int,int,int,int,int,int,(int,int))
var tuple2 = ("Stephanie", 7);
Console.WriteLine($"{tuple2.Item1}, {tuple2.Item2}"); var tuple3 = (Name: "Matthias", Age: 6);
Console.WriteLine($"{tuple3.Name} {tuple3.Age}"); static (int radius, double area) CalculateAreaOfCircle(int radius)
{
return (radius, Math.PI * Math.Pow(radius, 2));
} var circle = CalculateAreaOfCircle(2);
Console.WriteLine($"A circle of radius, {circle.radius}," +
$" has an area of {circle.area:N2}."); class Person
{
private readonly string _firstName;
private readonly string _lastName;
public Person(string firstname, string lastname)
{
_firstName = firstname;
_lastName = lastname;
}
public override String ToString() => $"{_firstName} {_lastName}";
public void Deconstruct(out string firstname, out string lastname)
{
firstname = _firstName;
lastname = _lastName;
}
} var p1 = new Person("Katharina", "Nagel");
(string first, string last) = p1;
Console.WriteLine($"{first} {last}"); public class Rectangle
{
public Rectangle(int height, int width)
{
Height = height;
Width = width;
}
public int Width { get; }
public int Height { get; }
}
public static class RectangleExtensions
{
public static void Deconstruct(this Rectangle rectangle, out int height, out int width)
{
height = rectangle.Height;
width = rectangle.Width;
}
} var r1 = new Rectangle(100, 200);
(int height, int width) = r1;
Console.WriteLine($"height: {height}, width: {width}"); List<Employee> allEmployees = new List<Employee>()
{
new Employee { ID = 1L, Name = "Fred", Salary = 50000M },
new Employee { ID = 2L, Name = "Sally", Salary = 60000M },
new Employee { ID = 3L, Name = "George", Salary = 70000M }
};
var wellPaid =
from oneEmployee in allEmployees
where oneEmployee.Salary > 50000M
select new { EmpName = oneEmployee.Name,
Income = oneEmployee.Salary }; var wellPaid =
from oneEmployee in allEmployees
where oneEmployee.Salary > 50000M
orderby oneEmployee.Salary descending
select (EmpName: oneEmployee.Name,
Income: oneEmployee.Salary);
var highestPaid = wellPaid.First().EmpName; public Task<(int index, T item)> FindAsync<T>(IEnumerable<T> input, Predicate<T> match)
{
var dictionary = new Dictionary<(int, int), string>();
throw new NotSupportedException();
} public void showMsg(int age, string name){/*...*/}
(int age, string name) value =(20, "Jessy");
showMsg(...value); //? or something else // query dapper via "dynamic"
int id = ...
var row = conn.QuerySingle("select Name, LastUpdated from Records where Id=@id", new {id});
string name = row.Name; // "dynamic" // query dapper via C# 7 tuples
int id = ...
var row = conn.QuerySingle<(string name, DateTime lastUpdated)>(
"select Name, LastUpdated from Records where Id=@id", new {id});
// use row.name and row.lastUpdated Car car = new("Test", "Blue");
var (model, color) = car;
// Initialization
string model = string.Empty;
string color = string.Empty;
(model, color) = car;
// Assignment string model = string.Empty; (model, var color) = car; // Initialization and assignment