Why Tuple#39;s items are ReadOnly?(为什么 Tuple 的项目是只读的?)
问题描述
我正在考虑使用 Tuple
类来存储我在程序中需要的 2 个整数信息(StartAddress、EndAddress).
I was thinking to use Tuple
class to store 2 integer information (StartAddress, EndAddress) I need in my program.
但是我发现 Tuple
项是只读的,所以如果我需要为一个项设置一个值,我需要重新实例化一个元组.
But I discover that Tuple
items are ReadOnly, so if I need to set a value for an item, I need to re-instantiate a Tuple.
这个设计决策背后的原因是什么?
What is the reason behind this design decision?
推荐答案
元组起源于 函数式编程.在(纯)函数式编程中,一切在设计上都是不可变的——某个变量在任何时候都只有一个定义,就像在数学中一样..NET 设计人员在将函数式样式集成到 C#/.NET 时明智地遵循了相同的原则,尽管它最终主要是一种命令式(混合?)语言.
Tuples originated in functional programming. In (purely) functional programming, everything is immutable by design - a certain variable only has a single definition at all times, as in mathematics. The .NET designers wisely followed the same principle when integrating the functional style into C#/.NET, despite it ultimately being a primarily imperative (hybrid?) language.
注意:虽然我怀疑元组是不可变的这一事实并没有真正使您的任务变得更加困难,但您可能还想使用匿名类型(或者可能只是一个简单的结构).
Note: Though I suspect the fact that tuples are immutable doesn't really make your task much harder, there are also anonymous types (or perhaps just a simple struct) you might want to use.
这篇关于为什么 Tuple 的项目是只读的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么 Tuple 的项目是只读的?
基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- rabbitmq 的 REST API 2022-01-01