当前位置:网站首页>[C # variable constant keyword] - variable constants and keywords in C #
[C # variable constant keyword] - variable constants and keywords in C #
2022-07-19 08:01:00 【Burning and blowing】
##################################################
Catalog
Calculate the area and perimeter of a circle
##################################################
C# keyword
——————————
C# All keywords
Here is C# Key words of In alphabetical order :
abstract as
base bool break byte
case catch char checked class const continue
decimal default delegate do double
else enum event explicit extern
false finally fixed float for foreach
get goto
if implicit in int interface internal is
lock long
namespace new null
object operator out override
params partial private protected public
readonly ref return
sbyte sealed set short sizeof stackalloc static string struct switch
this throw true try typeof
uint ulong unchecked unsafe ushort using
value virtual volatile void
where while
yield##################################################
C# Of Variable 、 Constant
——————————
C# Common data types
Each development language has its own data type
C# The data type provided by is similar to Java be similar Compare common data types :
data type Java C#
plastic int int
floating-point float float
Double precision type double double
String type String string
Boolean type boolean boolWe can see Boolean type Follow Java Different Use bool
also Character C# The middle initial is lowercase string
Apart from these two points There's another one that needs attention
C# It is required to be floating-point When variables are assigned With
f
or
F
end
Here is C# The complete data type provided :
data type size Example
byte Unsigned 8 An integer byte myByte = 2;
sbyte A signed 8 An integer sbyte mySbyte = -100;
int A signed 32 An integer int count = 800;
uint Unsigned 32 An integer uint sum = 600;
long A signed 64 An integer long population = 294967296;
ulong Unsigned 64 An integer ulong height = 92233720368547;
short A signed 16 An integer short salary = 3500;
ushort Unsigned 16 An integer ushort money = 2000;
decimal 128 Bit floating point Accurate to the decimal point 28~29 position decimal result = 1200.56M;
double 64 Bit floating point Accurate to the decimal point 15~16 position double cash = 35.23D;
float 32 Bit floating point Accurate to the decimal point 7 position float score = 59.9F;
char 16 position Unicode character char male = 'M';
string Unicode character string Reference type string color = "red";
bool Boolean value True/Flase bool isStudent = Ture;——————————
C# Use of variables
C# The variable declaration method in is the same as Java It's the same grammar :
data type Variable name ; The same is true of variable naming rules however $ Fuzai C# Cannot be used in
Therefore, the naming rules of variables can be simply summarized as three
form :
52 English letters A~Z、a~Z
10 A digital 0~9
Underline _
start :
Only with Letter 、 Underline start
prohibit :
C# Key words of %%%%%
Variable naming
Let's talk about variable naming conventions
First Namely See the name and know the meaning
The naming of variables should be meaningful And try to name it in English
for example A variable represents the name To use name instead of aaa、bbbin addition Try to avoid using single character naming
for example a、b、c It should be named index、temp
Of course except Loop variableWhen using multiple words to form variable names Best use Camel nomenclature namely Camel
That is to say First word lowercase Other words are capitalized
for example myName、yourAge etc.
——————————
C# Use of constants
Why use constants ?
Constants are values that remain constant during program operation For example, it means the number of days in a week This should be the same So it is defined as constant
Definition grammar :
const data type Constant name = value ;Constants are quantities that do not change during program operation If you want to forcibly modify her value You're going to report a mistake for example :
The left side of the assignment number must be a variable , Attribute or index value
So it can only be modified when the constant is defined This ensures that it will not be misoperated
Constants are very common in programs So when can we use constants ?
First of all Once set in the program, the value that cannot be modified
for example PI The value of cannot be modified randomly in the whole program
second Used for values that are often referenced in programs
For example, an error message may be referenced in many places in the program Define it as a constant
Third Used for values with specific meanings in programs
for example Used in the program 1 Indicates login status use 0 Indicates that you are not logged in
At this time, two meaningful constants can be defined to represent Sign in / Not logged in Constants are essential in programs In other words, you know how many constants there are in an operating system ?
%%%%%
Constant naming
Constant naming conventions are as follows :
The name must have practical meaning
Constant names should be capitalized
In the middle, underline can be used as a link according to the connectivity of meaning
It is best to have a simple comment on the right side of each constant Explain its role
The constant name should not exceed 25 Characters Otherwise, the readability is extremely poor
——————————
The sample program
We use VSCode Editor collocation SDK Write compiled code Please check this link for details :

Right click on the space choice adopt Code open :

%%%%%
Calculate the area and perimeter of a circle
Now let's start our example For example, we will use two formulas to calculate the area and perimeter of a circle :
2
Area of circle = πr
The circumference of a circle C = 2πrthat PI /π How to express it in the program ?
using System;
namespace _1. Constant _ PI
{
class Program
{
static void Main(string[] args)
{
/* Show me how to use constants
*
* because π/ PI Is a constant quantity And it's very troublesome to knock
* So we define her as a constant PI Then use it directly in the program PI Calculate
*/
/* Program preparation */
const double PI = 3.1415926; // Define constants PI
int r = 5;
double girth = 2 * PI * r; // Calculate the circumference of a circle
double area = PI * r * r; // Calculate the area of a circle
/* Results output */
Console.Write(" The circumference of the circle is >>> ");
Console.WriteLine(girth); // Output the circumference of the circle
Console.Write(" The area of a circle is >>> ");
Console.WriteLine(area); // The area of the output circle
/* Program end */
Console.ReadLine(); // Program end
}
}
}VSCode demo:
The circumference of the circle is >>> 31.415926
The area of a circle is >>> 78.539815At this point, the last line of code is waiting for us to enter :

Just go back :


边栏推荐
- Xinlinx zynq7020, 7045 domestic replacement fmql45t900 national production arm core board + expansion board
- FMC sub card: 4-channel 250msps sampling rate 16 bit AD acquisition sub card
- 【刷题篇】完全平方数
- A set of Jenkins style for personal use
- cad填充转多段线脚本
- How to write the highlights of SCI papers (seriously teach you how to write)
- V8 引擎如何进行垃圾内存的回收?
- [MySQL] transaction: basic knowledge of transaction, implementation principle of MySQL transaction, detailed explanation of transaction log redolog & undo
- 力扣114题:二叉树展开链表
- If a number in C language is exactly equal to the sum of its factors, this number is called "perfect". For example, 6=1 + 2 + 3 programming
猜你喜欢

Random forest of machine learning

redis分布式锁

YOLOV5-打标签建立自己的数据集

Flink entry to practice - phase I (cluster installation & use)

Pytoch notes (3)

《痞子衡嵌入式半月刊》 第 58 期

4-channel FMC interface baseband signal processing board (2 FMC interfaces, 2 fmc+ interfaces)
![Virtual machine stack of [JVM]](/img/7b/5d847112230adcac087b9d6071baed.png)
Virtual machine stack of [JVM]

2022危险化学品经营单位主要负责人复习题及模拟考试

Understand LSTM and Gru
随机推荐
4-channel fmc+ baseband signal processing board (4-channel 2G instantaneous bandwidth ad+da)
Visual Studio 生产环境配置方案:SlowCheetah
半导体材料技术
MongoDB的使用
Facial key point detection CNN
@ConditionalOnMissingBean 如何实现覆盖第三方组件中的 Bean
Ku115 FPGA high performance 10G Optical fiber network hardware accelerator card / 2-way 10G Optical fiber data accelerator card
Go语言圣经
Introduction & use of Maxwell
网站漏洞修复服务商分析可控参数带来的越权
Forecast sales xgboost
Flink entry to practice - phase I (cluster installation & use)
Random forest of machine learning
912. 排序数组(数组排序)
导出文件or下载文件
Pytorch notes (5)
Spark3.x-practical double flow join (window and redis implementation method and template code)
Download, installation and use of mongodb
Discussion sur la technologie RISC - V
Use of mongodb