在本文中,深入研究C#7.1中的元组。这是每个C#程序员都需要熟悉的数据结构。
元组是简单的数据结构,最多可以存储八个字段。
这些字段可以包含任何类型,从字符串和整数到对象,甚至其他元组。当你发现你的方法中增加了“out”参数或者创建一个简单的POCO或模型来返回多个字段时,你应该考虑使用一个Tuple。
使用元组简化代码并减少需要创建的类的数量。从C#7开始,命名参数和更好的性能使它们值得使用。
有一些限制。可读和可维护的代码很重要,许多人更喜欢定义明确的模型。作为一个经验法则,如果您要返回三个以上的字段,请检查您的方法以确定Tuples是否仍然有意义,或者模型是否使代码更易于阅读。在MVC ViewModels和Views之间使用元组有一个奇怪的怪癖。
以下是如何使用元组的例子。
我们首先回顾一下最新和最伟大的旧款式。
这些例子包括一些基本的单元测试,并存储在我的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 Tuples
var 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,现在是给他们一次机会的时候了。
你有没有使用新的Tuples?你喜欢新的方式吗?张贴您的意见,让我们讨论!
本文暂时没有评论,来添加一个吧(●'◡'●)