为什么 Tuple 的项目是只读的? [英] Why Tuple's items are ReadOnly?

查看:38
本文介绍了为什么 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 的项目是只读的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆