This is the 4th day of my participation in the August More Text Challenge

A, constant

Constants are fixed values that do not change during program execution. Constants can be any basic data type, such as integer constants, floating-point constants, character constants, or string constants, and enumerated constants.

Constants can be treated as regular variables, but their values cannot be changed after they are defined.

  • Define constants

Constants are defined using the const keyword. The syntax for defining a constant is:

constType constant name = constant value;Copy the code

use

const double d=3.14;// The correct way to declare constants
const int b;         // Error, no initialization
Copy the code

Error at compile time when changing constant value:

  • Dynamic constant: readonly

Values are determined at run time, can only be initialized at declaration time or in a constructor, and can only be defined in a class. The definition method is as follows:

using System;

namespace VSProject
{
    class Program
    {
        readonly int a = 1;  // Initialize at declaration time
        readonly int b;    

        Program() // init in the constructor
        {
            b = 2; 
        }

        void Add()
        {
            Console.WriteLine("a + b = "+ (a + b)); }}}Copy the code

Usage suggestions: In the following two cases:

  1. Values are permanent (such as PI, the number of hours in a day, the radius of the Earth, etc.).
  2. Very demanding on program performance.

Const constants can be used, but otherwise readonly constants should be preferred.


Second, the variable

A variable is simply the name of a storage area on which a program operates. In C#, each variable has a specific type that determines its memory size and layout. Values in the range can be stored in memory, and a series of operations can be performed on variables.

C# states that variables must be “defined before they are used”.

The syntax for defining a variable is as follows:

Variable type Variable name;Copy the code

The variable type can be any data type or var supported in C#, and the variable names follow the naming conventions for identifiers in C#.

  • Some valid variable definitions are as follows:
int i, j, k;
char c, ch;
float f, salary;
double d;
Copy the code
  • A simple example
using System;

namespace VSProject
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 1;
            int b = 2;

            Console.WriteLine("a + b = "+ (a + b)); Console.ReadKey(); }}}Copy the code

A variable value is a constant that is of the same type or can be implicitly converted to that type, a variable that has been defined and assigned a value of the same type or can be implicitly converted to that type, or an expression consisting of them as operands.

  • The Var keyword

C#3.0 supports using the var keyword to define implicitly typed variables, which must be initialized when declared. An example of an implicitly typed variable definition is as follows:

// declare an implicitly typed variable a, whose type is int according to its initial value 1
var a = 1;
Copy the code
  • Nullable types

Nullable types can represent all values and null of the underlying type, defining nullable types

The syntax for variables is as follows:

Variable type? The variable name.Copy the code

Variable types can be simple, enumerated, and structural types in the C# type system, but not reference types.

Example:

int? i;
double? d1 = 3.14;
bool? flag = null;
Copy the code

When using nullable variables, C# provides two methods to test for null values to avoid variable misreferences.

 int? a = 1;
 int b = 2;

 if (a == null)
 {
     Console.WriteLine("A value is empty and cannot be computed.");
 }
 else
 {
     Console.WriteLine("a + b = " + (a + b));
 }
Copy the code