在C#中不可变类型和属性 [英] Immutable type and property in C#

查看:218
本文介绍了在C#中不可变类型和属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是用C#不变型的和不可改变的属性是什么意思?你可以给简单的例子?

What is meant by immutable type and immutable property in C# ? can you give simple example?

推荐答案

这是不可变的类型是一个类型,它的属性只能在初始化设置。一旦对象被创建,没有什么可以再更改。一个不可变的属性仅仅是一个只读属性。

An immutable type is a type of which its properties can only be set at initialization. Once an object is created, nothing can be changed anymore. An immutable property is simply a read-only property.

在下面的例子中, ImmutableType 是一个不可变型与一个属性测试。测试是一个只读属性。它只能在建筑中设置

In the following example, ImmutableType is an immutable type with one property Test. Test is a read-only property. It can only be set at construction.

class ImmutableType
{
    private readonly string _test;
    public string Test
    {
        get { return _test; }
    }

    public ImmutableType(string test)
    {
        _test = test;
    }
}



另请参见:< A HREF =htt​​p://en.wikipedia.org/wiki/Immutable_object>维基百科的文章和的一些堆栈 溢出问题在话题。

这篇关于在C#中不可变类型和属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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