当前位置:网站首页>[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 :


边栏推荐
- csp-2020-6-《角色授权》
- Jira --- workflow call external api
- FMC sub card: 4-channel 250msps sampling rate 16 bit AD acquisition sub card
- RISC-V技術雜談
- Spark3.x-practical double flow join (window and redis implementation method and template code)
- V8 引擎如何进行垃圾内存的回收?
- 网站漏洞修复服务商分析可控参数带来的越权
- Facial key point detection CNN
- CCF-CSP《202206-2—寻宝!大冒险!》
- PCIe bus architecture high performance data preprocessing board / K7 325t FMC interface data acquisition and transmission card
猜你喜欢
随机推荐
Export file or download file
美联储降息,为何长期利好数字货币市场? 2020-03-05
redis缓存雪崩、穿透、击穿
Flutter3.0 (framework) - UI rendering
1669. 合并两个链表(两个链表的合并)
Is it necessary to buy pension insurance? What are the pension products suitable for the elderly?
Facial key point detection CNN
4-channel FMC interface baseband signal processing board (2 FMC interfaces, 2 fmc+ interfaces)
How to choose flash for new products?
How to write the highlights of SCI papers (seriously teach you how to write)
【JVM】之堆内存、逃逸分析、栈上分配、同步省略、标量替换详解
redis消息订阅
Real time data warehouse - Design & Implementation of real-time data warehouse from 0 to 1 (sparkstreaming3.x)
网站漏洞修复服务商分析可控参数带来的越权
VMware cloud director 10.4 release (including download) - cloud computing provisioning and management platform
Modify scroll bar style
Spark introduction to proficient - external part (operation and maintenance and simple operation of standaone cluster)
[MySQL] lock mechanism: detailed explanation of lock classification, table lock, row lock and page lock in InnoDB engine
力扣114题:二叉树展开链表
企业微信上的应用如何与门店的sybase数据库关联


![[day01] preface, introductory program, constant variables](/img/aa/8ebb3fd11aa054264103a6f018ce6b.png)






