C#7 ?е?Tuple????
?????SANDEEP SINGH ???????[ 2017/3/30 10:58:21 ] ??????????????????? C#
????????
????Tuple?????????????????С? ???????????д??????????????????????????????????????????????????????? ????????Щ?????Tuple ??????C????Tuple ???????????????????????????????????????
?????????????????????????????????????????????Щ??洢??????????в?????ü???? ???????????????????????????????????C?? ?????Щ????????? Class / Struct??????????? Tuple??
????????????????????? ??????????????????????????????????????????????С??????? ???????????????????????С???????????? ????????????????????????Class / Struct??????????? Tuple?? ?????????????????????????????????????????
??????? out ????
??????????????????????????????????е?????С??? ?÷?????????????????????С??????? ????????????????????? out ?????????????????? ?????????????е???????? out ????????????е?С????
??????????????????? Operation ??????????????????????в???С???????????????????????????ù????
using System.Collections.Generic;
namespace TupleApp
{
class Operation
{
internal int FindMinMax(List<int> list?? out int min)
{
int maximum = int.MinValue?? minimum = int.MaxValue;
list.ForEach(n =>
{
minimum = n < minimum ? n : minimum;
maximum = n > maximum ? n : maximum;
});
min = minimum;
return maximum;
}
}
}
??????????????????Σ??÷?????????????????????????? maximum ?????α????? ????????е????? ??????????????????? min ????????ò??????????????е?С???
???????????????????????δ????г????????????
using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
namespace TupleApp
{
class Program
{
static void Main(string[] args)
{
List<int> numbers = Enumerable.Range(1?? 100).OrderBy(x => Guid.NewGuid()).Take(10).ToList();
int minimum = 0?? maximum = 0;
Operation operation = new Operation();
maximum = operation.FindMinMax(numbers?? out minimum);
Write($"{minimum} is min and {maximum} is max in {String.Join("??"?? numbers)}");
Read();
}
}
}
???????????????ó??? ?????????????
????
?1?????
???????????????????? out ?????????????????????
??????? Class/Struct
??????????????????????????????????–??????????и?????????????塣 ???????????????????????? ????????????? CalculateData ????壬??????????????????????????????е?????С??? ???′????????????????????塣
namespace TupleApp
{
struct CalculateData
{
public int Minimum { get; set; }
public int Maximum { get; set; }
}
}
?????????????????????????? Operation ???????????????????????в???С???????????? ???′?????????????????????
using System.Collections.Generic;
namespace TupleApp
{
class Operation
{
internal CalculateData FindMinMax(List<int> list)
{
int maximum = int.MinValue?? minimum = int.MaxValue;
list.ForEach(n =>
{
minimum = n < minimum ? n : minimum;
maximum = n > maximum ? n : maximum;
});
CalculateData data = new CalculateData
{
Minimum = minimum??
Maximum = maximum
};
return data;
}
}
}
??????????????????Σ??÷?????????????????????????? ??Щ???????????????е?????С???
??????????????г??????????????????????????????
using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
namespace TupleApp
{
class Program
{
static void Main(string[] args)
{
List<int> numbers = Enumerable.Range(1?? 100).OrderBy(x => Guid.NewGuid()).Take(10).ToList();
Operation operation = new Operation();
CalculateData data = operation.FindMinMax(numbers);
Write($"{data.Minimum} is min and {data.Maximum} is max in {String.Join("??"?? numbers)}");
Read();
}
}
}
???????????????ó??? ??????????????
????
?2: ??????
??????? Tuple
????C??7 ??????????????·????? Tuple ??????????????????????? ?????????????????????? ?????ó???????? Tuple???????????? System.ValueTuple NuGet ??.
????
?3 NuGet ??
???????????????????????????? ??????????????? Operation ??????????????????????в???С????????????? ???′???????????ù????
using System.Collections.Generic;
namespace TupleApp
{
class Operation
{
internal (int?? int) FindMinMax(List<int> list)
{
int maximum = int.MinValue?? minimum = int.MaxValue;
list.ForEach(n =>
{
minimum = n < minimum ? n : minimum;
maximum = n > maximum ? n : maximum;
});
return (minimum?? maximum);
}
}
}
??????????????????Σ??÷???????????????????????顣 ??Щ?????????????е?????С???
?????????????????????????????г???????????????
using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
namespace TupleApp
{
class Program
{
static void Main(string[] args)
{
List<int> numbers = Enumerable.Range(1?? 100).OrderBy(x => Guid.NewGuid()).Take(10).ToList();
Operation operation = new Operation();
(int?? int) data = operation.FindMinMax(numbers);
Write($"{data.Item1} is min and {data.Item2} is max from {String.Join("??"?? numbers)}");
Read();
}
}
}
??????????????????Σ???? FindMinMax ????????ò??????????顣 ???????????????????? Tuple ??????????????????????????Щ???????????????????????????????????????????????Щ??????? Item1 ?? Item2?? Item1 ????????????? Item2 ?????????????? Tupple ????????????????????
???????????????ó??? ??????????????
?4 ??????
???????? Item1 ?? Item2 ???????ε?????????????????????????????????? ?????????????????????????????????????????? Item1 ?? Item2??
????????????? Operation ??????? FindMinMax ?????з????? ???????Щ?????η????????????? ???′???????????ù????
using System.Collections.Generic;
namespace TupleApp
{
class Operation
{
internal (int Minimum?? int Maximum) FindMinMax(List<int> list)
{
int maximum = int.MinValue?? minimum = int.MaxValue;
list.ForEach(n =>
{
minimum = n < minimum ? n : minimum;
maximum = n > maximum ? n : maximum;
});
return (minimum?? maximum);
}
}
}
???????????????????????? Minimum??????????????? Maximum??
???????????????????????δ????г????????????
using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
namespace TupleApp
{
class Program
{
static void Main(string[] args)
{
List<int> numbers = Enumerable.Range(1?? 100).OrderBy(x => Guid.NewGuid()).Take(10).ToList();
Operation operation = new Operation();
var data = operation.FindMinMax(numbers);
Write($"{data.Minimum} is min and {data.Maximum} is max from {String.Join("??"?? numbers)}");
Read();
}
}
}
??????? FindMinMax ????????ò??????????顣 ?????????????????????????????????С???????
???????????????ó??????????
????
?5 ??????
????????
????Tuple ?????????????????С? ???????????????????????????????Tuple ?????????????????Tuple ?д????? 8 ???????? ?????????????????и?????? Tuple??????????????? Tuple?? Tuple ???????????????? Tuple??
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11