为什么C#数字类型不变? [英] Why are C# number types immutable?

查看:135
本文介绍了为什么C#数字类型不变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 INT 双击取值不变?什么是你想要改变的值,每次返回一个新对象的目的

Why are ints and doubles immutable? What is the purpose of returning a new object each time you want to change the value?

我想问的原因是因为我正在做一个类: BoundedInt ,它具有一个值和一个上限和下限。所以我在想:我应该让这种类型的不可变的呢? (或者它应该是一个结构?)

The reason I ask is because I'm making a class: BoundedInt, which has a value and an upper and lower bound. So I was wondering: should I make this type immutable too? (Or should it be a struct?)

推荐答案

首先:

什么是你想改变的价值呢?

What is the purpose of returning a new object each time you want to change the value?

我想你可能搞错了值类型是如何工作的。这不像你可能会想象一些代价高昂的操作; 。它只是数据的覆盖(而不是,例如,新的内存动态分配)

I think you might be mistaken about how value types work. This isn't some costly operation like you may be imagining; it's simply the overwriting of data (as opposed to, e.g., dynamic allocation of new memory).

其次:这里就是为什么数字是不可变的很简单的例子:

Secondly: here's a very simple example of why numbers are immutable:

5.Increase(1);
Console.WriteLine(5); // What should happen here?



当然,这是一个人为的例子。因此,让我们考虑一对夫妇更复杂的想法。

Granted, that is a contrived example. So let's consider a couple more involved ideas.

首先,有这样一条:如果整数是一个可变引用类型?

First, there's this one: what if Integer were a mutable reference type?

class Integer
{
    public int Value;
}



然后,我们可以有这样的代码:

Then we could have code like this:

class Something
{
    public Integer Integer { get; set; }
}

Integer x = new Integer { Value = 10 };

Something t1 = new Something();
t1.Integer = x;

Something t2 = new Something();
t2.Integer = t1.Integer;

t1.Integer.Value += 1;

Console.WriteLine(t2.Integer.Value); // Would output 11

这似乎违背直觉:该行 T2 .Integer = t1.Integer 仅复制的值(实际上,它;但价值实际上是一个参考),因此该 t2.Integer 将保持独立的 t1.Integer

This seems to defy intuition: that the line t2.Integer = t1.Integer would simply copy a value (actually, it does; but that "value" is in fact a reference) and thus that t2.Integer would remain independent of t1.Integer.

这可以走近另一种方式,当然,保持整数作为值类型,但保持其可变性:

This could be approached another way, of course, keeping Integer as a value type but maintaining its mutability:

struct Integer
{
    public int Value;

    // just for kicks
    public static implicit operator Integer(int value)
    {
        return new Integer { Value = value };
    }
}



但是,现在让我们说我们这样做:

But now let's say we do this:

Integer x = 10;

Something t = new Something();
t.Integer = x;

t.Integer.Value += 1; // This actually won't compile; but if it did,
                      // it would be modifying a copy of t.Integer, leaving
                      // the actual value at t.Integer unchanged.

Console.WriteLine(t.Integer.Value); // would still output 10



基本上,永恒的的是什么,是的高度直观。 。与此相反的是非常直观的。

Basically, immutability of values is something that is highly intuitive. The opposite is highly unintuitive.

我想这是主观的,但是,平心而论;)

I guess that is subjective, though, in all fairness ;)

这篇关于为什么C#数字类型不变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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