当前位置:网站首页>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 .
边栏推荐
- 老司机总结的12条 SQL 优化方案(非常实用)
- 洛谷P3426 [POI2005]SZA-Template 题解
- Beifu PLC passes MC_ Readparameter read configuration parameters of NC axis
- National standard gb28181 protocol easygbs cascaded universal vision platform, how to deal with live message 403?
- 轻流完成与「DaoCloud Enterprise 云原生应用云平台」兼容性认证
- goto语句实现关机小程序
- 第01章_Linux下MySQL的安装与使用
- Adobe Acrobat prevents 30 security software from viewing PDF files or there are security risks
- Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology
- ES6:迭代器
猜你喜欢
随机推荐
RSS rendering of solo blog system failed
NoSQL mongodb - 01 introduction to NoSQL and mongodb
Redis learning - 06 drifting bottle case
National standard gb28181 protocol easygbs video platform TCP active mode streaming exception repair
Lightflow completed the compatibility certification with "daocloud Enterprise Cloud native application cloud platform"
710. 黑名单中的随机数
软件测试 - 基础篇
倍福通过CTU和TON实现时间片大小和数量的控制
Deeply analyze the differences between dangbei box B3, Tencent Aurora 5S and Xiaomi box 4S
不到40行代码手撸一个BlocProvider
KITTI Detection dataset whose format is letf_top_right_bottom to JDE normalied xc_yc_w_h
黑马笔记---常用API
Goto statement to realize shutdown applet
一个快速切换一个底层实现的思路分享
File remote synchronization and backup artifact Rsync
Beifu PLC passes MC_ Readparameter read configuration parameters of NC axis
.NET MAUI 性能提升
不到40行代码手撸一个BlocProvider
Accumulation of interview questions
【网络是怎么连接的】第二章(上): 建立连接,传输数据,断开连接
![[esp32-C3][RT-THREAD] 基于ESP32C3运行RT-THREAD bsp最小系统](/img/4a/503240b332e3279047c438f1d9845e.png)







