C # basic-variables and constants (1)

Source: Internet
Author: User

C # basic-variables and constants (1)
I. Basic concepts of variables: variables are used to store specific types of data and change the data values stored in variables at any time. Variables have names, types, and values. Variable values can change. Before using a variable, you must declare the variable, that is, specify the type and name of the variable. 1. Variable type: the variable type is divided into value type and reference type according to its definition. 2. Variable type; 1. Value Type features: <! -- [If! SupportLists] --> ① <! -- [Endif] --> value type variables are stored in the stack. <! -- [If! SupportLists] --> ② <! -- [Endif] --> when you access a value type variable, you generally directly access its instance. <! -- [If! SupportLists] --> ③ <! -- [Endif] --> each value type variable has its own data copy. Therefore, operations on a value type variable do not affect other variables. <! -- [If! SupportLists] --> ④ <! -- [Endif] --> When copying a value type variable, the copied value is the variable value, not the variable address. <! -- [If! SupportLists] --> ⑤ <! -- [Endif] --> A value type variable cannot be NULL. It must have a definite value type that is inherited from the System. ValueType class. 2. Integer type: the integer type indicates the integer type without a decimal point. The value range is between 128 and in the 8-bit signed integer value range of Sbyte ~ The value range of the Short 16-bit signed integer between 127 is-32,768 ~ The value range of an Int 32-bit signed integer between 32,767 and 2,147,483,648 is ~ Between 2,147,483,647 Long 64-bit signed integer value range:-9,223,372,036,854,775,808 ~ Between 9,223,372,036,854,775,807 Bytes 8-bit unsigned integer value range is 0 ~ The value range of the Ushort 16-bit unsigned integer between 255 is 0 ~ Between 65,535 Uint 32-bit unsigned integer value range is 0 ~ Between 4,294,967,295 Ulong 64-bit unsigned integer value range is 0 ~ Create a console application between 18,446,744,073,709,551,615, declare the int-type variable ls and initialize it to 927, and initialize shj as a byte-type variable to 255, output 1 statci void Main (string [] arge) 2 {3 int ls = 927; // declare an int type variable ls4 byte shj = 255; // declare a byte type variable shj5 Console. writeLine ("ls = {0}, shj = {1}" + ls, shj); // output 6 Console. readLine (); 7} if the value of the byte type variable shj is 266, the value of byte ranges from 0 ~ 255 3. floating point type: floating point type variables are mainly used to process numerical data with decimals. Floating Point variables include float and double. Their difference is that the value range and precision type description range float is precise to 7-digit value range is 1.5x10 ^-45 ~ The double value between 3.4x10 ^ 38 is accurate to 15 ~ The 16-digit value range is 5.0x10 ^-324 ~ If no value is set between 1.7x10 ^ 308 and, the numeric value containing the decimal point is considered to be of the double type. If not specified, this value is of the double type. If you want to process a value of the float type, you need to use f or F to specify it as the float Type 1 double MyDou = 927d; // use d to forcibly convert to double2 double mudou = 112D; // use D to forcibly convert to double to forcibly specify the value type as double, you need to use D or d to set 1 double MyDou = 927d; // use d to forcibly convert to double2 double mudou = 112D; // use D to forcibly convert to double <! -- [If! SupportLists] --> 3. <! -- [Endif] --> boolean type: boolean type is mainly used to indicate true/false values. A boolean type variable can only be true or false, other values cannot be specified to boolean variables. boolean variables cannot be converted to other types. Assign 927 to the Boolean variable x 1 Bool x = 927. This assignment is incorrect. Constant Value 927 cannot be converted to bool. Iii. Reference Type: 1. the reference type is the primary object type data for building a C # application. The pre-defined object type creates an object instance with new and stores it in the stack. 2. Reference Type features: <! -- [If! SupportLists] --> ① <! -- [Endif] --> memory must be allocated for referenced type variables in the managed heap. <! -- [If! SupportLists] --> ② <! -- [Endif] --> you must use the new keyword to create a reference type variable. <! -- [If! SupportLists] --> ③ <! -- [Endif] --> each object allocated in the managed heap has an additional member associated with it. These members must be initialized. <! -- [If! SupportLists] --> ④ <! -- [Endif] --> reference type variable roommate garbage collection mechanism for management; ⑤ Multiple Reference variables can reference the same object. In this case, operations on one variable will affect the same object referenced by another variable. ⑥ The value before the reference type is assigned is NULL. All the objects referred to as "classes" are reference types, it mainly includes classes, interfaces, arrays, and delegation. Create a console application, create a class C in it, create a field Value in this class, and initialize it to 0. create the reference type variable of the class through new at other locations, and finally output the variable. 1 class Program 2 {3 class C // create a class C 4 {5 public int Value = 0; // declare a public int type variable Value 6} 7 static void Main (string [] args) 8 {9 int v1 = 0; // declare an int type variable v1, and initialize it to 010 int v2 = 1; // declare an int type variable v2 and assign v1 v211 v2 = 927; // assign the variable V2 to 92712 C r1 = new C () again; // use the new keyword to create the reference object 13 C r2 = r1; // set r1 = r214 r2.Value = 112; // set the value of r2 to 15 Console. writeLine ("Values; {0}, {1}", v1, v2); // you can specify 16 Console variables for output v1 and v2. writeLine ("refs; {0}, {1}", r1.Value, r2.Value); // output the value of the reference type object 17 Console. readLine (); 18} 19}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.