C # basic data types,

Source: Internet
Author: User

C # basic data types,

First, you must understand that the basic data types recognized by C # are not built into the C # language, but are built into the. net Framework.

For example, when c # declares an int type data, it is actually an instance of the. net structure System. Int32. This may sound profound, but far-reaching: it means that all basic data types can be viewed as classes that support certain methods in syntax.

Type is actually stored as the basic type. The basic types are represented in the. Net structure in concept, so there is certainly no performance loss.

Next, let's take a look at the built-in types defined in C #. We will list each type and their definition and the name of the corresponding. net Type (CTS type.

C # There are 15 predefined types, 13 of which are value types and 2 are reference types (string and object)

1. Integer

2. Floating Point Type

Float data types are used for small floating point numbers because they require low precision.

The double data type is larger than the float data type, and the precision is doubled (15 bits ).

If a non-integer value (such as 12.3) is not hard encoded in the Code, the compiler generally assumes that the variable is double.

If you want to specify this value as float, you can add the character F (or f) after it, such:

Float f = 12.3F;

3. decimal type

The decimal type is used for financial computing. The 28-Bit mode provided by the decimal type depends on the user.

To specify a number as the decimal type, you can add the character M or (m) after the number, such:

Decimal d = 12.30 M;

4. bool (Boolean) Type

5. char character type

Char variables are enclosed in single quotes. For example, 'A'

If the character is enclosed in "A" (double quotation marks), the compiler regards it as A string and produces an error.

6. Reference Type (Object Type and string type)

3. How to declare variables and assign values?

1. variable definition: refers to the amount of changes that can occur at any time during the running of the program.

2. As we mentioned above, a variable exists in the memory during runtime and is a temporary storage space.

Data of various types such as numbers, strings, and dates can be stored in the memory.

You can take a look at this figure, which indicates a state in the memory.

Simply put, a variable represents a storage area in the memory.

It corresponds to a unique memory address, but when we are using a program, the memory address is hard to understand and remember.

So what should we do?

In daily life, we all have a name, such as "James" and "James". These names are used to facilitate memory.

Similarly, in the program, in order to distinguish multiple variables, You need to assign a brief value to each variable, which is easy to remember,

This is the variable name.

3. the naming of variables in C # is rule-based:

1), composed of letters, numbers, or underscores (_)

2) It must start with a letter or underscore "_" and cannot start with a number.

3) it cannot be a keyword in c #, such as int, string, bool, Main, class, etc.

4) case sensitive: lowercase a and uppercase A are two variables.

4. As a good learner, some variable naming rules must be observed:

1) The name of a variable must be meaningful and should be named in English as much as possible.

For example, you can name a name variable or use xingMing to name a, B, or c.

2) Avoid using a single character as the variable name (except for the variable specified in the loop)

3) when multiple words are used to form variable names, the Camel naming method should be used.

Camel naming: lowercase for the first word and upper for other words, such as myName and myAge

Multiple choice questions: Which of the following variables are correctly named ()

A, name, _ 222*1, 9 class, public

B. _ teacher, void, string, and myName

C, $ Age, corss, fire, _ grade

D, _ glass, g23, c_12, my_first_2

5. Variable declaration and value assignment

1) syntax for defining variables:

Data type variable name; (defines an age variable, age is an integer, so the variable definition is as follows :)

Int age; [The system allocates different sizes of storage space in the Memory Based on the Data Type]

Multiple variables can be defined after each data type, such as: (name, home address, nationality, nationality)

String name, address, origin, national;

2) variable assignment Syntax:

Variable name = value;

(Here, "=" indicates the value assignment operator. Assign the value on the right of "=" to the variable name on the left and end with a semicolon)

For example, if you are 18 years old, your name is "Xiao Zhang", and your home address is "XXX, Nanping, Chongqing ",

The nationality is "Chongqing" and the nationality is "Han"

Age = 18;

Name = "Xiao Zhang ";

Address = "Chongqing Nanping XXX ";

Origin = "Chongqing ";

National = "Han ";

Iv. Differences between variables and constants

Variable: refers to the amount of changes that can occur at any time during the running of the program.

Constant: a variable that does not change during program running.

Features of constants:

1. The value must be assigned in the Declaration.

2. You cannot assign a value to a constant while running the program.

3. constants are static. Not required (actually, not allowed) to include the modifier static in the constant Declaration

5. How are data types converted?

In C #, there are two types of data conversion: 1) implicit [also called automatic] type conversion 2) Explicit [also called forced] type conversion

For example:

The airport stops the plane. We call this implicit [also called automatic] type conversion.

If we take a look at the fact that the plane is equipped with an airport, this is impossible in our real life,

But the program may do this. We call this explicit [Forced] type conversion.

In C #, there are two types of data conversion: 1) implicit [also called automatic] type conversion 2) Explicit [also called forced] type conversion

For example:

The airport stops the plane. We call this implicit [also called automatic] type conversion.

If we take a look at the fact that the plane is equipped with an airport, this is impossible in our real life,

But the program can do this. We call this explicit [Forced] type conversion.

The relationship between double and int Is (airport and aircraft). We can understand that the value range of double is much larger than that of int.

So double can be loaded with int:

Int a = 1234;

Double B = a; the system will assign the value of an integer variable to the double variable B. This is implicit [also called automatic] type conversion.

In turn:

Double a = 1234;

Int B = a; // an error is reported during system compilation ,:

How can we implement forced conversion? C # provides very simple methods, such:

Double a = 1234;

Intb = (int) a; // This forces the double type to be converted to the int type.

Finally, we will introduce the conversion between other data types (1. String Conversion to other types 2. Conversion between any types)

1) convert strings to other types

Syntax:

XX. Parse (string); here xx represents: double, int, bool, etc.

For example:

String strValue = "123.45"; // This is a string with the value "123.45"

To convert it to the decimal type, you can use double. Parse (); To convert it.

Double dValue = double. Parse (strValue );

2) Conversion between any types

Syntax:

Convert. ToXX (any type );

For example, convert a boolean type to an integer type.

Bool a = true;

Int B = Convert. ToInt16 ();

Console. WriteLine ("the converted result is:" + B); // The converted result is: 1

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.