一看就懂——C#中readonly关键字

本文介绍了C#中readonly关键字的定义、作用和使用示例。readonly属性仅能在声明时或构造函数中初始化,之后不可修改。通过示例代码展示了即使对象的属性可变,但readonly修饰的引用类型变量本身指向的对象不可变。在依赖注入中,这一特性得到应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.定义

  • 通常是用在类的属性上
  • 使用了readonly的属性,只能在定义时或类的构造函数中初始化,除此之外不可以再修改它的值

2.作用

  • 如果你希望一个数据成员在初始化后不能被改变,可以使用【readonly】关键字。

注意:这个【readonly】关键字的作用和其字面意思“只读”没有多少关系,所以尽管忽略其字面意思

3.代码

创建一个控制台项目,包含3个文件Program.cs、ReadOnlyDemo.cs、ReadOnlyDemo2.cs
Program.cs的代码:

namespace FeatureDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //readonly是一个string
            ReadOnlyDemo demo1 = new ReadOnlyDemo("demo1");
            showResult(demo1.GetCode());

            //readonly 是一个类
            TestClass1 t1 = new TestClass1();
            TestClass2 t2 = new TestClass2();
            ReadOnlyDemo2 demo2 = new ReadOnlyDemo2(t1);
            demo2.changeValue("hello");
            showResult(demo2.showValue());


            Console.ReadLine();
        }

        public static void showResult(string info)
        {
            Console.WriteLine(info);
        }
    }
}

ReadOnlyDemo.cs的代码:

using System;
using System.Collections.Generic;
using System.Text;

namespace FeatureDemo
{
    public class ReadOnlyDemo
    {
        private readonly string _code = "empty string";

        public ReadOnlyDemo(string code)
        {
            _code = code;
        }

        public string GetCode()
        {
            return _code;
        }

        //只能在初始化或构造方法中对readonly属性进行再赋值,该方法会报错
        //public void ChangeCode(string newCode)
        //{
        //    _code = newCode;
        //}
    }
}

ReadOnlyDemo2.cs的代码:

using Microsoft.VisualBasic.CompilerServices;
using System;
using System.Collections.Generic;
using System.Text;

namespace FeatureDemo
{
    public class ReadOnlyDemo2
    {
        private readonly ITest _testInterface;

        public ReadOnlyDemo2(ITest testInterface)
        {
            _testInterface = testInterface;
        }

        public void changeValue(String code)
        {
            _testInterface.setCode(code);
        }

        public string showValue()
        {
            return _testInterface.getCode();
        }

        //只能在初始化或构造方法中对readonly属性进行再赋值,该方法会报错
        //public void changeObject(ITest testInterface)
        //{
        //    _testInterface = testInterface;
        //}
    }

    public class TestClass1: ITest
    {
        private string _code { get; set; }

        public string getCode()
        {
            return "Test1:" + _code; 
        }

        public void setCode(string code)
        {
            _code = code;
        }
    }
    
    public class TestClass2: ITest
    {
        public string _code { get; set; }

        public string getCode()
        {
            return "Test2:" + _code;
        }

        public void setCode(string code)
        {
            _code = code;
        }
    }

    public interface ITest
    {
        public string getCode();
        public void setCode(string code);
    }
}

4.运行结果

运行结果

5.说明

从Demo中不难发现下面几点:

  1. readonly定义的string一旦初始化后就不能修改其值,而如果是个类对象,是可以在初始化后修改该类中的属性值的
  2. 如果在readonly在定义时和构造函数中同时都对它赋值,定义时的值会被构造函数覆盖
  3. ReadOnlyDemo2.cs中无法实现类似changeObject这样的方法的,在依赖注入中就使用了这个特性

6.写在结尾

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值