为什么元组的项目是只读的

为什么元组的项目是只读的

本文介绍了为什么元组的项目是只读的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 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.

这篇关于为什么元组的项目是只读的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 08:17