问题描述
我想用元组
类来存储2的整数信息(StartAddress开始,EndAddress)我需要在我的程序。
I was thinking to use Tuple
class to store 2 integer information (StartAddress, EndAddress) I need in my program.
不过,我发现,元组
项目是只读的,所以如果我需要设置一个值,一个项目,我需要重新实例化一个元组。
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.
这篇关于为什么元组的项目是只读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!