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

C# keyword

C# All keywords

C# Of Variable 、 Constant

C# Common data types

C# Use of variables

Variable naming

C# Use of constants

Constant naming

The sample program

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    bool

         We 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、bbb

         in addition Try to avoid using single character naming
         for example a、b、c It should be named index、temp
         Of course except Loop variable

         When 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 :

[C# SDK/IDE]-VSCode build C# development environment _ Flaming blog -CSDN Blog _vscode Development c# Form program Get installation Visual Studio Code Download and install the official website of localization and plug-in configuration .NET Code SDK First time to know .NET SDK Some of the commands are basic C# Program compilation and running command line are used separately SDK Command initializes the workspace and compiles and runs C# The program is in VSCode Medium collocation SDK Compile operation C# Program installation configuration Code Runner The plug-in is easier to compile and run, and the resources in this section can be downloaded .........https://blog.csdn.net/m0_67268286/article/details/125529352        This is our working directory :

 working directory

        Right click on the space choice adopt Code open :

 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πr

         that 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.539815

         At this point, the last line of code is waiting for us to enter :

 Waiting for input

        Just go back :

 Directly enter

VSCode demo

原网站

版权声明
本文为[Burning and blowing]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207170621168855.html