当前位置:网站首页>Detailed explanation of C const: definition and use of C constant
Detailed explanation of C const: definition and use of C constant
2022-06-26 13:02:00 【Dusk and starry sky】
Constants and variables are containers for storing data , When defining, you need to specify the data type , The only difference between them is : Variable (Variable) Values stored in are allowed to be changed , And constant (Constant) The value stored in cannot be changed .
Last section 《C# Variable 》 The definition and use of variables have been explained in , In this section, we will explain the definition and use of constants .
C# Definition of constant
Unlike variables , The value of a constant cannot be changed after it is first assigned . Defining constants requires the use of keywords const To complete .
The specific grammatical form is as follows :
const data type Constant names = value ;
It should be noted that , A constant must be assigned a value when it is defined , Because if you don't assign values, you can't assign values anymore . in addition , You can also define multiple constants at the same time .
Using constants in programs also brings many benefits , It includes enhancing the readability of the program and facilitating the modification of the program . For example, in a program that calculates the rate , In order to ensure the uniform tax rate in the procedure , Set a name to TAX Constant to complete , If you need to modify the tax rate, just modify the value of this constant .
【 example 1】 Find the area and perimeter of the circle respectively , And use constants to store π Value , take π The value of is defined as 3.14.
Copy in plain text
class Program
{
static void Main(string[] args)
{
const double PI = 3.14;
int r = 3; // Storage radius
Console.WriteLine(“ The circumference of the circle is :” + 2 * PI * r);
Console.WriteLine(“ The area of a circle is :” + PI * r * r);
}
}
Execute the code above , The effect is as follows .
边栏推荐
猜你喜欢
National standard gb28181 protocol easygbs video platform TCP active mode streaming exception repair
File remote synchronization and backup artifact Rsync
Research and development practice of Kwai real-time data warehouse support system
Processing 多面体变化
【网络是怎么连接的】第二章(上): 建立连接,传输数据,断开连接
小白懒人专用-win10-win11一键安装版
[BSidesCF 2019]Kookie 1
不到40行代码手撸一个BlocProvider
processing 随机生成线动画
软件测试 - 基础篇
随机推荐
Adobe Acrobat prevents 30 security software from viewing PDF files or there are security risks
postgis计算角度
单例的常用创建和使用方式
Learning Processing Zoog
深度解析当贝盒子B3、腾讯极光5S、小米盒子4S之间的区别
无人机遥感在森林监测的部分应用研究案例总结
由错误<note: candidate expects 1 argument, 0 provided>引发的思考
processing 函数translate(mouseX, mouseY)学习
NLP-D60-nlp比赛D29
NoSQL mongodb - 02 mongodb server installation, mongodb shell, basic concepts and visualization tools
倍福PLC基于NT_Shutdown实现控制器自动关机重启
EasyGBS如何解决对讲功能使用异常?
Word文档导出(使用固定模板)
[geek challenge 2019] rce me 1
KVM video card transparent transmission -- the road of building a dream
MySQL 自定义函数时:This function has none of DETERMINISTIC, NO SQL 解决方案
复制多个excel然后命名不同的名字
710. random numbers in the blacklist
The El form item contains two inputs. Verify the two inputs
Redis learning - 04 persistence