当前位置:网站首页>C: readonly and Const
C: readonly and Const
2022-07-23 13:52:00 【qq_ forty-two million nine hundred and eighty-seven thousand ni】
One 、const keyword
1.const Modifiable types
C# In the middle of const No, C++ So flexible , The types of variables that can be modified are very limited .
There are mainly these kinds : Basic data type 、 String type 、 A fellow const The variable of
Illustrate with examples :
const float f=9;
const string s = "999";
const int i1 = 8;
const int i2 = i1+1;2. Note that
1)const You can modify not only member variables but also local variables .
2)const The principle of is to replace variable values directly at compile time .
3)const Decorated variables can no longer be used static Variable to modify , And it can only be initialized in the declaration of the field
This part mainly refers to :const keyword - C# Reference resources | Microsoft Docs
C# Basic knowledge series 8 (const and readonly keyword ) - Xiaole - Blog Garden
Two 、readonly keyword
readonly Keywords compared to const Keywords are more flexible .
1. Instructions
1)readonly There are no restrictions on variable types . but readonly Modified keywords can only be used in Declaration period Or in the same class Constructors Assign values to fields . You can assign and reassign read-only fields multiple times in field declarations and constructors , At other times, it cannot be assigned .
Illustrate with examples :
public class ReadOnlyTest : MonoBehaviour
{
readonly int i = 0;// It can be assigned at the time of declaration
readonly float f1;// It is also possible not to assign values at the time of declaration
ReadOnlyTest()
{
i = 9;// Multiple assignments can be made in the constructor
i = 8;
f1 = 2.2f;
}
}2)readonly Decorate value types and reference types .
public class RefType
{
public int i = 0;
}
public struct ValueType
{
public int i = 0;
public ValueType(int a)
{
i=a;
}
}
public class ReadOnlyTest : MonoBehaviour
{
readonly RefType refType = new RefType();
readonly ValueType valueType = new ValueType();
readonly ValueType1 valueType1 = new ValueType1();// Report errors
void Test()
{
refType.i = 9;// Don't complain
valueType.i = 2;// Report errors
}
}It can be seen that readonly The type of value decorated , The member variable of value type will also be readonly Of , But when decorating reference types , Its essence is a constant pointer , Member variables of reference types are not subject to readonly Modifier constrained .
2. Be careful
1)readonly Decorated variables are assigned at runtime .
2)readonly Cannot decorate general local variables , Currently tested , You know ref Follow readonly Connected together, you can decorate .
3)readonly The modified variable can be static Of
This part refers to :readonly keyword - C# Reference resources | Microsoft Docs
C# Basic knowledge series 8 (const and readonly keyword ) - Xiaole - Blog Garden
3、 ... and 、 other
use readonly modification struct There are also other precautions , May refer to :C# Read only structures in (readonly struct) Detailed explanation
however readonly It can't be modified class Of .
边栏推荐
- Database view detailed exploration
- High school Chinese teaching material examination outline
- Kotlin - suspend function
- SeekTiger的Okaleido有大动作,生态通证STI会借此爆发?
- 数据库系统原理与应用教程(049)—— MySQL 查询(十一):子查询
- Talking about the CPU type of anroid device and the placement directory of so files
- 微信小程序--动态设置导航栏颜色
- [Part 2] full analysis of oak-d+turnlebot3 robot project
- ES6 - weekly examination questions
- Redis常用命令
猜你喜欢

2022 summer vacation software innovation laboratory training project practice 1

Kotlin - Job 任务/取消

Warcraft map editor trigger notes
腾讯MMKV的原理与实现

图形管线(一)后处理阶段 alpha测试 模版测试 深度测试 混合

解决MySQL向表中增加数据插入中文乱码问题

docker mysql

Elephant Swap的LaaS方案优势分析,致eToken表现强势
![[JS advanced] basics of regular expressions - about regular expressions you want to know_ 01](/img/b8/7961a3c4375c93971ddd74b1a2473f.jpg)
[JS advanced] basics of regular expressions - about regular expressions you want to know_ 01

基于OpenCV实现对图片及视频中感兴趣区域颜色识别
随机推荐
数据库系统原理与应用教程(039)—— MySQL 查询(一):SELECT 命令的语法分析
数据库系统原理与应用教程(046)—— MySQL 查询(八):分组查询(GROUP BY)
Remove title block
【STM32】串口通信基础知识
Vs2019:constexpr function "qcountleadingzerobits" cannot generate constant expressions
2. Les règles quantitatives
LeetCode_46_全排列
Starfish OS:以现实为纽带,打造元宇宙新范式
基于OpenCV实现对图片及视频中感兴趣区域颜色识别
200 行代码,深入分析动态计算图的原理及实现
KingbaseES DENSE_RANK 函数用法
LeetCode_491_递增子序列
Unity关于本地加载图片涉及webrequest或者byte
[record] golang cross platform compilation
LeetCode_2341_数组能形成多少数对
LeetCode_ 2341_ How many pairs can an array form
PHP gets the current timestamp three bit MS MS timestamp
Special lecture 5 combinatorial mathematics learning experience (long-term update)
IP地址分类及范围
图像处理1:RGB888_YCbCr444