Tutorial C # Getting started 3 define variables and common data types

Source: Internet
Author: User

If you have usedC LanguageIn this lesson, you can go over and learn the next section directly, because in C #, the basic data type does not change, but the data type is stored according to the data storage method, it is divided into two categories: Value Type and reference type. I will explain the value type and reference type in future courses. Next we will take a specific course today.

Variable:

Variable isProgramming LanguageThe smallest logical unit inProgramIt is used to store temporary data generated when users use applications. The data is temporarily stored in the memory, and the program then stores the required variables in the hard disk through other technologies.

To define a variable, you need to tell the memory what data type the variable is. It is like in life, a bamboo (data type) name is "Xiao Lan (variable name)" and it is (=) solid stones (temporary data). For example, a cup (data type) is named Beckham (variable name) and it is filled with (=) liquid milk (temporary data ).

Data TypeIs a special type to control the storage of temporary data;Variable nameWhen a computer language becomes an identifier and an identifier is defined as a variable, note the following:

1. variable names cannot be the same as system keywords (the keywords refer to C # pre-defined identifiers, that is, words, which have been given special meaning, for example, using, class, namespace, and main ).

2. the prefix of the variable name must start with a letter or underline.

3. Try to have actual names and easily identify data types and content for storing data; for example, _ bookname (book name) and int_booknum (number of books)

Temporary data when assigning values to variablesIt will also be reasonably stored based on the corresponding data types. For example, the bamboo basket cannot hold liquid, and the cup cannot hold stones. At the same time, the students should consider such a situation, now we have loaded milk into Beckham. When the milk is used up, we can also use Beckham to pack cola. The temporary data has changed, but the data type has not changed because it is liquid, in this case, we get used to modifying the value of the variable Beckham or assigning a value to the Beckham again.

The syntax for converting the example of my life into a variable is as follows:

Data type variable name (identifier) = initial value;

The following describes the basic data types:

1. Integer type:Represents an integer without a decimal point, that is, an integer we learned when we were a child, such as 0,-3902,38445.

C # language contains the following integer types. The difference between them is that the range of values is different. Each type is divided into two different versions based on whether there is a negative number.

The value range of the variable defined by the sbyte 8-bit signed integer ranges from-128 ~ 127

The value range of a byte 8-bit signed integer variable ranges from 0 ~ 255

The value range of the variable defined by the short 16-bit signed integer ranges from-32768 ~ 32767

The value range of the ushort 16-bit signed integer variable ranges from 0 ~ 65535

The value range of a variable defined by an int 32-bit signed integer ranges from-2147483648 ~ 2147483647

The value range of a uint 32-bit signed integer variable ranges from 0 ~ 4204067295

The value range of a variable defined by a long 64-bit signed integer ranges from-9223372036854775808 ~ 9223372036854775807

The value range of a ulong 64-bit signed integer variable ranges from 0 ~ 18446744073709551615

The Int type is the most commonly used. If the value range is insufficient, we can use the ulong type, but when the sbyte range is small, be sure to prevent the program from running errors because the data value is out of the range. The default integer value is 0.

Define the initial value of a 32-bit integer variable X as 5: int x = 5;

2. floating point type:

The floating point type is the type that contains decimal places. This type has two versions:

Float 7-Bit Single-precision floating point variables range from 1.5 × 10 − 45 to 3.4 × 1038

The value range of a double 15-bit double-precision floating point variable ranges from 5.0 × 10 − 325 to 1.7 × 10308.

If decimals appear, the compiler defaults to the double type. If you want to indicate that the decimal value is of the float type, you can add f or f after a small value, the syntax for defining these two types of variables is:

Float x = 3.75f;

Double Y = 3.75;

Please remember that when you want to perform a float operation, you must add f or f after the decimal number. explicitly inform that the float type is used; otherwise, an error will occur.

3. Character Type

The char type is often called the character type. When assigning values to a char type variable, the value should be enclosed in single quotation marks and the value should be a character, such as char a = 'B '; char B = '1'; char c = 'hao ';. In addition to English, numbers, and Chinese characters, the value can also be a hexadecimal character or a unicode16 character for the char type. However, this method is rarely used and does not need to be mastered at present.

4. boolean type

Boolean values are only true or false. the syntax of defining a Boolean variable is bool d = true. It is very clever and commonly used for Boolean variables.

5. string type

We can regard the string type as a combination of strings. When assigning values to variables of the string type, double quotation marks should be used to cause the values, such as string a = "B "; string d = "C #";

String B = "Hello! "; The string type is a very important type. In the future, some features and functions of the string type will be described separately. Now we only need to learn its definition.

The focus of today's learning is the use of the above five types. You must be familiar with the definition of these five types of variables. Below are some questions for the students:

1. Define a char type variable char_c. Its value is 1: Char char_c = '1 ';

2. Define the string type variable TXT. Its value is 256: String TXT = "256 ";

3. Define the variable num. Its value is the number 5.6: Double num = 5.6; or float num = 5.6f; (F can also)

4. Modify the value of the num variable to the number 7.3: num = 7.3; or num = 7.3f;

When you modify the value of a variable, if the variable has been defined, for example, question 3rd, you should not add a type before the variable during modification and directly assign a value again, if we change the answer to Question 4 to double num = 7.3, the compiler will prompt that the variable num with the same name has been defined, it means that if there is a data type before the variable name, they are by default creating a new variable num, but because the 3rd question has defined a num with the same name, it will prompt an error with the same name, so if we modify the value of a defined variable, assign a value to the variable directly.

In the next lesson, we will learn how to convert the input and output types in the console.

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.