C # Data types

Source: Internet
Author: User

In the first chapter we understand C # input, output statements, I this section is mainly about the basic knowledge of C #, this section is the basis of the following chapters, good beginning equals half of the success. After you read this chapter, you will have enough C # knowledge to write a simple program. However, inheritance or other object-oriented features cannot be used.

This chapter focuses on the following sections:

1, what is the computer to store the data used?

2. What are the basic data types?

3. How to declare variables and Assign a value?

4. What are the differences between variables and constants?

5. How are the different data types converted?

Below, we are now one to understand these issues.

What is the computer that stores the data used?

The question is summed up in a nutshell: The computer uses memory to memorize the data used in the calculation.

In real life data are various, integers, decimals, strings, characters and so on, they are all different types, so you want to use these types in the computer, you must in memory for it to apply for a suitable space.

What are the data types that C # can use? Let's have a look together.

 

Ii. What are the underlying data types

the first thing to understand is that the underlying data types that C # recognizes are not built into the C # language, but are built into the. NET Framework.

For example, when declaring data of type int in C #, the declaration is actually an instance of the. NET structure System.Int32. This may sound esoteric, but it's far-reaching: this means that all the underlying data types can be thought of as a class that supports some methods in syntax.

The type is actually still stored as a base type. The basic type is conceptually used. NET structure, so there must be no performance penalty.

Let's take a look at the built-in types defined in C #, we'll list each type, along with their definitions and the name of the corresponding. NET type (CTS type).

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

1. Integral type

2. Floating-point type

The float data type is used for smaller floating-point numbers because it requires less precision.

The double data type is larger than the float data type and provides one-fold greater precision (15-bit).

If you do not hardcode a non-integer value (such as 12.3) in your code, the compiler generally assumes that the variable is double.

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

float F = 12.3F;

3. Decimal type

The decimal type is specifically used for financial calculations, and the 28-bit method provided with the decimal type depends on the user.

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

Decimal d=12.30m;

4, BOOL (Boolean) type

5. Char character type

Character variables of type char are enclosed in single quotation marks. such as ' A '

If you put the character in the "A" (double quote), the compiler will think of it as a string, resulting in an error.

6. Reference type (object type and String type)

Third, How do I declare a variable and assign a value ?

1, the definition of variables: refers to the process in the course of operation can be changed at any time the amount of

2, we mentioned above, the variable in the runtime is in memory, it is a temporary storage site.

In memory, you can store various types of data, such as numbers, strings, dates, and so on.

You can take a look at this picture and it will represent a state in memory.

in simple terms, A variable is a piece of memory that represents a storage area.

It will correspond to a unique memory address, but we use the program when the memory address is not good to understand the memory.

So what do we do?

In daily life we all have a name, such as "Zhang San", "John Doe" and so on, these names are for the convenience of memory.

So again, in the program, in order to distinguish multiple variables, it is necessary to assign a value to each variable a short, easy-to-remember name,

this is Variable name .

3 . The naming of variables in C # is regular:

1), consisting of letters, numbers, or underscores "_"

2), must start with a letter or underscore "_" and cannot begin with a number

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

4), case-sensitive, such as: lowercase A and uppercase A is two variables

4, as a better learner, you must abide by a number of variable naming norms:

1), the name of the variable to have meaning, as far as possible with the corresponding English name, with "See the name of understanding" role.

such as: The name variable is named name or with a spelling xingming, avoid naming them with A,b,c.

2), avoid using a single character as the variable name (except for variables that are fixed in the loop)

3) When you use multiple words to make a variable name, you should use the Camel name Method

Camel (Camel) Nomenclature: The first letter in lowercase, with the first letter of the other words capitalized, such as: Myname,myage

Select Question : The following variables are named correctly ()

A, Name, _222*1, 9class, public

B, _teacher, Void, string, myName

C, $Age, Corss, Fire, _grade

D, _glass, G23, C_12, my_first_2

5. Declaration and assignment of variables

1) Define the syntax of the variable:

Data Type      variable name; ( Define an age variable, the age is an integer, so the variable is defined as follows:)

int age; [The system allocates different sizes of storage in memory based on the data type]

after each data type, you can define multiple variables, such as: (Define name, home address, hometown, ethnicity)

String Name,address,origin,national;

2) Assign a value to the variable syntax:

variable name = value ;           

(The = number here indicates the assignment operator, the value to the right of the = number, assigned to the variable name on the left, and the last one; [ Semicolon] End)

such as: age 18 years old, the name is "Xiao Zhang", the home address is "Chongqing South ping xxx",

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

Age = 18;

Name = "Xiao Zhang";

Address = "Chongqing South ping xxx";

Origin = "Chongqing";

National = "Han";

Iv. The difference between a variable and a constant

Variable: refers to the amount of change that can occur at any time during the course of a program's operation.

Constant: Refers to a variable that does not change during the course of a program's operation.

Characteristics of constants:

1, must be in the declaration is assigned value

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

3, the constant is static. does not have to (in fact, is not allowed) to include modifiers in a constant declaration static

V. how the different data types are converted

There are two types of data type conversions in C #: 1) Implicit [also called Automatic] type conversion 2) explicit [also called coercion] type conversions

To give a simple example:

It's a matter of course we call this implicit [also called Automatic] type conversion in an airport.

Well, if we look at it in reverse, the plane is loaded with an airfield, which in our real life feels like it's impossible,

But the program may do this, and we call this explicit [also called coercion] type conversion

There are two types of data type conversions in C #: 1) Implicit [also called Automatic] type conversion 2) explicit [also called coercion] type conversions

To give a simple example:

It's a matter of course we call this implicit [also called Automatic] type conversion in an airport.

Well, if we look at it in reverse, the plane is loaded with an airfield, which in our real life feels like it's impossible,

But the program can do this, we call this explicit [also called coercion] type conversion

between double and int is the relationship between (an airfield and an airplane), and we can understand that double values are much larger than int

So the double can be loaded with int:

int a=1234;

Double b=a; The system assigns the value of the a integer variable to the double variable B. This is the implicit [also called Automatic] type conversion

That in turn:

Double a=1234;

int b=a; When the system compiles, it will error:

So how do you make a cast? C # provides a very simple method, such as:

double a=1234;

int b= (int) A; This forces the double type to be converted to the INT type

Finally, the conversion between other data types (1, string conversion to other type 2, conversion between any type) is introduced.

1), string conversion to other types

Grammar:

XX.  Parse (string); xx Here is representative of such as: Double,int,bool, etc.

Give an example to illustrate:

String strvalue= "123.45"; This is a string, the value of the time face is "123.45"

Now you want to convert it to a decimal type, you can use double. Parse (); to convert

Double dvalue=double. Parse (strvalue);

2), conversion between any type

Grammar:

Convert.toxx (any type);

such as: Converting a Boolean type to an integral type

bool A = true;
int B = Convert.ToInt16 (a);
Console.WriteLine ("The result after conversion is:" +b); The converted result is: 1

OK, after the above study, I believe that you have some basic knowledge of C # has a certain understanding, then the next chapter I will introduce C # arithmetic operators, logical operators, relational operators, and control program flow statements.

C # Data types

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.