gkket 发表于 2021-3-28 15:21:04

每个C#程序员都需要熟悉的数据结构

在本文中,深入研究C#7.1中的元组。这是每个C#程序员都需要熟悉的数据结构。https://p6-tt.byteimg.com/origin/pgc-image/cc365714762043b09e2e1932688df756?from=pc

元组是简单的数据结构,最多可以存储八个字段。这些字段可以包含任何类型,从字符串和整数到对象,甚至其他元组。当你发现你的方法中增加了“out”参数或者创建一个简单的POCO或模型来返回多个字段时,你应该考虑使用一个Tuple。使用元组简化代码并减少需要创建的类的数量。从C#7开始,命名参数和更好的性能使它们值得使用。有一些限制。可读和可维护的代码很重要,许多人更喜欢定义明确的模型。作为一个经验法则,如果您要返回三个以上的字段,请检查您的方法以确定Tuples是否仍然有意义,或者模型是否使代码更易于阅读。在MVC ViewModels和Views之间使用元组有一个奇怪的怪癖。https://p6-tt.byteimg.com/origin/pgc-image/180f528956ea4dbca9e3f8dd83678838?from=pc

以下是如何使用元组的例子。我们首先回顾一下最新和最伟大的旧款式。这些例子包括一些基本的单元测试,并存储在我的GitHub Tips库中。元组类如果你更喜欢直接新增Tuple对象,这里是代码:public class UserRepositoryTupleClass{public List<Tuple<string, string>> GetFirstNameAndLastNameList(){var users = new List<Tuple<string, string>>{new Tuple<string, string>("John", "Doe"),new Tuple<string, string>("Steve", "Smith")};return users;}public Tuple<string, string> GetFirstNameAndLastName(){return new Tuple<string, string>("John", "Doe");}}// Example Usage:var repository = new UserRepositoryTupleClass();var firstNameAndLastName = repository.GetFirstNameAndLastName();// Tuple items are named Item1 and Item2.var firstName = firstNameAndLastName.Item1;var lastName = firstNameAndLastName.Item2;未命名的元组使用圆括号是Tuples更常用的语法。它更干净,除了我们仍然有那讨厌的Item1,Item2的用法。请注意,示例使用与Tuple类相同。public class UserRepositoryUnnamedTuples{public List<(string, string)> GetFirstNameAndLastNameList(){var users = new List<(string, string)>{("John", "Doe"),("Steve", "Smith")};return users;}public (string, string) GetFirstNameAndLastName(){return ("John", "Doe");}}// Example Usage:var repository = new UserRepositoryUnnamedTuples();var firstNameAndLastName = repository.GetFirstNameAndLastName();// Tuple items are named Item1 and Item2.var firstName = firstNameAndLastName.Item1;var lastName = firstNameAndLastName.Item2;命名元组命名的元组真棒!从C#7开始,你可以给Tuples中的字段命名,使它们更加有用和自我记录。public class UserRepositoryNamedTuples{public List<(string FirstName, string LastName)> GetFirstNameAndLastNameList(){var users = new List<(string FirstName, string LastName)>{("John", "Doe"),("Steve", "Smith")};return users;}public (string FirstName, string LastName) GetFirstNameAndLastName(){return (FirstName: "John", LastName: "Doe");}}// Example Usage:var repository = new UserRepositoryNamedTuples();var firstNameAndLastName = repository.GetFirstNameAndLastName();// Tuple items are named.var firstName = firstNameAndLastName.FirstName;var lastName = firstNameAndLastName.LastName;嵌套元组嵌套元组遵循相同的语法。只是不要发疯,否则你可能会把你的C#代码混淆为LISP。public class UserRepositoryNestedTuples{public List<(string FirstName, string LastName,(string City, string State) Address)> GetFirstNameAndLastNameList(){var users = new List<(string FirstName, string LastName,(string City, string State) Address)>{("John", "Doe", ("Columbus", "OH")),("Steve", "Smith", ("Lansing", "MI"))};return users;}public (string FirstName, string LastName,(string City, string State) Address) GetFirstNameAndLastName(){return (FirstName: "John", LastName: "Doe", Address: ("Columbus", "OH"));}}// Example Usage:var repository = new UserRepositoryNestedTuples();var firstNameAndLastName = repository.GetFirstNameAndLastName();// Nested Tuplesvar firstName = firstNameAndLastName.FirstName;var lastName = firstNameAndLastName.LastName;var city = firstNameAndLastName.Address.City;var state = firstNameAndLastName.Address.State;资源关于元组的微软技术文档。概述了更为明确的做元组的方法,它解释了元组[ MSDN ]中支持的字段数量的限制。Tuples是如何在IL中工作的大概介绍。Joseph用MVC ViewModels和View提供了一个很好的例子。在编译时,指定的元组被转换为旧的样式,如Item1,Item2等,因此在可用时会留下可怕的代码气味。阅读他的文章,了解更多关于元组优点和缺点的细节。结论总而言之,如果由于在C#7之前缺乏命名字段或性能问题而一直拒绝使用像我这样的Tuples,现在是给他们一次机会的时候了。

XJS_权 发表于 2021-3-28 15:21:05

感谢分享,佩服佩服!

奋斗 发表于 2021-4-7 23:03:34

在遇到你之前我对人世间是否有技术大佬保有怀疑,现在我是彻底被你征服了

mao84245279 发表于 2025-11-12 17:03:00

浅蹲一个后续,楼主更新踢我一下

你熊熊 发表于 2025-11-12 17:32:18

打卡路过,支持优质原创内容~

axl 发表于 2025-11-12 17:33:14

水个经验,楼主加油,支持你~

真诚的_s2K5I 发表于 2025-11-12 18:18:00

这波反向操作,我属实没想到!

一苇以航 发表于 2025-11-12 18:42:23

这波分析到位,逻辑满分!

sgd810406122 发表于 2025-11-12 18:45:39

说得对!狠狠赞同,没毛病~

JHW 发表于 2025-11-12 20:15:41

理性围观,感觉大家说的都有道理~
页: [1] 2
查看完整版本: 每个C#程序员都需要熟悉的数据结构