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. Before using a variable, you must declare the variable, that is, specify the type and name of the variable.

1. Variable type:

Variable types are defined as values and reference types.

2. Variable type;

1. Value Type:

C # simple type classification: integer, floating point, and Boolean

① INTEGER: int, sbyte, byte, short, ushort, uint, long, ulong, and char

Int type: A 32-bit signed integer with a value ranging from-2,147,483,648 ~ In the range of 2,147,483,647.

Sbyte: an 8-digit integer with a value range of 128 ~ In the range of 127.

Byte type: Unsigned 16-digit integer with a value range of 0 ~ In the range of 255.

Short: A signed 16-digit integer with a value ranging from-32,768 ~ In the range of 32,767.

Ushort: Unsigned 16-digit integer with a value ranging from 0 ~ In the range of 65,535.

Uint type: Unsigned 32-bit integer with a value range of 0 ~ In the range of 4,294,967,295.

Long: A 64-bit signed integer with a value range of 9,223,372,036,854,775,808 ~ In the range of 9,223,372,036,854,775,807.

Ulong: A 64-bit unsigned integer with a value ranging from 0 ~ In the range of 18,446,744,073,709,551,615.
Create a console application, declare the int-type variable ls, initialize it to 927, and initialize the shj variable for the byte type as 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}View Code

If the value of shj is 266, the byte value ranges from 0 ~ 255

② Boolean:

Bool: Specifies the true or false value, which is used to store values true and false.

1 bool x = 927;View Code

Boolean data types include true and false. You can assign true or false values to a Boolean variable or an expression. The obtained value is equal to one of the two:

Bool bTest = (80> 90 );

③ Floating point type

The two data types are treated as float and double. Their difference lies in the value range and accuracy:

Float: The value range is 1.5x10 ^-45 ~ Between 3.4x10 ^ 38, the precision is 7 digits.

1 float theMySum = 9.27f; // use f to forcibly specify float Type 2 float theMySums = 1.12F; // use F to forcibly specify float TypeView Code

Double: The value range is 5.0x10 ^-324 ~ Between 1.7x10 ^ 308, precision is 15 ~ 16 digits.

1 double MyDou = 927d; // use d to forcibly convert to double2 double mudou = 112D; // use D to forcibly convert to double

2. Reference Type:

The reference type is the primary object type data for Building C # applications. The pre-defined object type creates an object instance with new and stores it in the stack.

All referred to as "classes" are reference types, including 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.