Recognize variables and data types in the C # language

Source: Internet
Author: User

Variables in the C # language

Concept: A piece of memory that stores data, and the data content of that memory area can change

Three elements of a variable: data type, variable name, variable value

First declaration, then assign the value

Variable declaration: Specifies a piece of memory space for storing data

Syntax: Data type variable name

For example: int number; For storing integers

Assignment of variables:

For example: int number;  number=6; For storing integers

Variable naming

Hard Requirements:

1. Variables can only be composed of numbers, letters, underscores

2. Variable names must not start with a number

3. The name cannot be the same as the keyword

4. Variable names must not be the same in the same function

Soft requirements:

1. Variable name to be able to look at the text to understand

2. Variable name first letter lowercase

3. In addition to the first word, the other single first letter capitalization

Data types in the C # language

1.char character type

Unlimited number of words (Chinese characters, letters, numbers, punctuation marks)

2.string String Type

Unlimited number of words (Chinese characters, letters, numbers, punctuation marks)

notation in pairs of double quotation marks

Data example "Han" "" "Hello,world"

3.in integer type

Integral type, used to represent an integer

Write the way to write directly

Data Example 0 100-1-100

4.double Decimal Type

Used to represent numbers that contain decimals

Write the way to write directly

Data example 0.0 1.1-1.1-1.123 correct

Conversion of data:

1. Int+int=int (Discard decimal part.)

Double+int=double

The result of a 2.string type is a string type, which can be operated on with any type


3. Convert any type to string

Data to be converted.        ToString (); int a = 10;

STRINGB = A.tostring ();

Convert 4.int to Double

implicit conversion inta=123;
doubled = A;

Convert 5.double to int

Explicit conversion of Double A = 3.14
int d = (int) A;

6.string converts to numbers (int)

Int. Parse (string to be converted); return type int

Convert 7.string to Double

Double. Parse (string to be converted); return type is double

Recognize variables and data types in the C # language

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.