2. Basic data types, constants and variables

Source: Internet
Author: User

First, the basic knowledge:

    1. Common data types: Int,short,char, etc., note: Nullable data types

A) integer type

1) signed integer: Sbyte,short,int,long

L sbyte occupies 1 bytes (8 bits), Value range: 128 ~127

L Short occupies 2 bytes (16 bits), value range: 32768 ~32767

l int occupies 4 bytes (32 bits), the value range: 2147483648 ~2147483647 such as: int age = 13;

L Long occupies 8 bytes (64 bits), Value range: 9,223,372,036,854,775,808~9,223,372,036,854,775,807

Example: Long num = 31413L

2) unsigned integer: Byte,ushort,uint,ulong

L byte occupies 1 bytes (8 bits), value range: 0 ~255

L ushort occupies 2 bytes (16 bits), Value range: 0~65535

L UINT occupies 4 bytes (32 bits), value range: 0~4294967295

L ULONG occupies 8 bytes (64 bits), Value range: 0~18,446,744,073,709,551,615: ULong num = 3414324UL

b) floating point number (with decimal points)

1) Single precision: float, valid bit: 7 digits after the decimal point. such as float price = 3.0987392F

2) Double precision: Double valid bit: 15 digits after the decimal point.

3) as Double price = 3.0987392222D

4) Decimal valid bit: 28 digits after the decimal point. such as decimal price = 3.0987392222M

Note: decimal the money that corresponds to the database The data type.

5) scientific notation denotes a value: Double d=2.5e-6; indicates d=2.5*.

c) Boolean data type: BOOL

Only true and False

d) Character type

1) Single character (single quote): Char occupies 2 bytes (16 bits) such as: char c = ' a ';

2) string (double quotation mark): string: String name = "Zhang San";

Note: If a string is a double with a decimal number and cannot be directly coerced into an int type, you need to first convert the double type in the conversion int type. Cases:

String a= "3.8";

int b= (int) convert.todouble (a);

String retention issues:

Because strings are immutable objects, if multiple strings make reference to multiple strings with the same content, this wastes resources, and if only one string object is kept in the managed heap, it makes more efficient use of memory. The principle of string retention is like this, so the same string has only one string instance, such as String p_str= "abc" and string p_str2= "abc" two string variable refers to a string object.

E) Object data type: Is the final base class for all data types

Only object such as: Object obj = "123";

f) Nullable data type:? such as: int? age = null;

Note: Assigning a variable of a large data type to a small data type variable must be cast. As follows:

int B = 10;

byte C = (byte) b;//cast

Note: integers are by default the int type in the computer, and the decimal is the double type by default. If you are doing operations with a small data type, you also need to do a strong turn:

Example: Byte b=4; BYTE b1=3; BYTE b2=7;

B= b1+3; Error message: Cannot convert type int to ' byte ', there is a display conversion.

Corrected: b= (byte) (b1+3);

Also, a strong turn is required if the data of two byte types is being calculated.

Example: B=B1+B2; Cannot convert int to "byte", there is a display conversion, two variables of type byte are added, automatically converted to int, to prevent out of range.

    1. The definition of a variable: varies by the defined location:

A) field: The variable that defines the inside of the class, the outside of the method

b) Local variables: variables defined inside the method

Class Program {

Field

Static Void Main ()

{//local variable}

c) The naming convention for variables: the first position must be a letter or an underscore or @, but only letters, numbers, and underscores can be included.

d) Format: Data type variable name (note: If it is a field, you need to precede the [access modifier]:[access modifier] Data type variable name)

    1. 5 access modifiers: private,protected,internal,protected internal,public.

A) Private: modified variables can only be accessed in the current class, but not in other classes.

b) protected (Protected): Modified variables can only be accessed in the current class, or in subclasses of the current class (derived classes).

c) Internal: Modified variables can only be accessed by all classes in the current assembly (project).

d) protected internal: Modified variables can only be accessed in the current class, or in subclasses of the current class (derived classes), accessed in all classes in the current assembly (project).

e) Public: modified variables can be accessed in any class.

    1. Constant, read-only field.

Constants: You cannot modify the values saved by a constant during the run. Keyword: const, which must be initialized (assigning values to variables) to define the format: Field constants and local constants. Define constants with public

Field constants: [access modifier] Const data type constant NAME = initial value such as: Public const string sex = "male";

Local constant: The name of the const data type constant = initial value. such as: const string sex = "male";

Note: Const cannot be used in conjunction with static because construction is static by default.

Read-only field: The value saved by the constant cannot be modified while it is running, but the only value that can be modified is in the constructor of the class. Keyword: readonly

In general, only read-only fields such as: Private readonly string connstr = "Data source=."

Const differs from ReadOnly: 1. The const field can only be initialized in the declaration of the field. The readonly field can be initialized in a declaration or constructor. Therefore, depending on the constructor used, the ReadOnly field may have different values. 2. The const field is a compile-time constant, and the ReadOnly field can be used to run a constant number.
3. The const default is static, and readonly must display a declaration if it is set to static.
4. Const for a constant of a reference type, the possible value can be only string and null. ReadOnly can be of any type
Summary: const can only be initialized with constants at an early stage. For each compiled result, the const value is fixed, and the value of ReadOnly can be determined at run time ~ ~

2. Basic data types, constants and variables

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.