当前位置:网站首页>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 .
边栏推荐
猜你喜欢

Xilinx FPGA一路时钟输入两个PLL

Light chain dissection / tree chain dissection

Learn to use canvas to build line chart, bar chart and pie chart

第二章关系数据库课后习题

微服务重点

IP address classification and range

C#:in、out、ref關鍵字

200 行代码,深入分析动态计算图的原理及实现

Talking about the CPU type of anroid device and the placement directory of so files

【STM32】串口通信基础知识
随机推荐
图像处理3:Sobel边缘检测
图像处理6:顶层文件
LeetCode_ 46_ Full Permutation
Optimising a 3D convolutional neural network for head and neck computed tomography segmentation with
[Muduo] epollplayer event distributor
LeetCode_ 491_ Longest increasing subsequence
图像处理4:腐蚀
腾讯MMKV的原理与实现
常用的鼠标事件和键盘事件
LeetCode_ 47_ Full arrangement II
KingbaseES DENSE_RANK 函数用法
Establish stm32f103c8t6 project template and STM32 st-link utility burn hex file
[record] golang cross platform compilation
十大券商开户风险性大吗,安全吗?
图像处理7:测试文件
数据库系统原理与应用教程(047)—— MySQL 查询(九):连接查询
数据库系统原理与应用教程(041)—— MySQL 查询(三):设置查询条件
prometheus+node-exporter+grafana 监控服务器系统资源
Optimising a 3D convolutional neural network for head and neck computed tomography segmentation with
数据库系统原理与应用教程(050)—— MySQL 查询(十二):SELECT 命令的执行过程分析