C # data type, type conversion, variables, constants, escape characters ...,

Source: Internet
Author: User
Tags define null

C # data type, type conversion, variables, constants, escape characters ...,

Let's talk about the data type... Data types can be divided into two categories:Basic Data TypeAndReference Type.

The basic data types are divided into "Value Type", "Boolean Type", and "numeric type" by function ".

The reference types include "string" and "time and date ". No picture, no truth.

Common types are circled in orange.

 

String typeStringArbitrary data can be defined, with almost no length limit. The value assignment must contain quotation marks.

String a = ""; string aa = null; only the string type can define null values.

Integer typeIntCan only be definedIntegerOrNegative integer. The length is 10 digits (including spaces ). Assign values without quotation marks

Long IntegerLongAndIntSame, but the length is almost unlimited. No quotation marks are required for the assignment.

Remember to recognize small integer bytes and medium integer short.

Float (Decimal type) is commonly used Double (default) Decimal

Pay attention to float a = 10.5 When assigning valuesF; (add f later );

Double a = 10.5; (default)

Decimal a = 10.5 m (followed by m)

Float can define integers and decimals, and can also define integers without the suffix f and m.

Boolean bool defines only true and false values for the error type.

Bool a = true/false

Character-type char can only contain a single character and must contain only one character.

Char a = "1"

Reference Type: DateTime

DateTime dt = new DataTime (int year, int month, int day); enter the first half of the brackets and press the key to select the fourth and then enter the time

DateTime nowdt = DataTime. Now; (you can obtain the time of the current computer .)

Console. WriteLine (Now); this can be used together to output the current time.

Type conversion

Implicit conversion: Convert the value type to the reference type. Do not declare the conversion method.

String is the highest level in implicit conversion, string s = "10"

Int a = 20

String ss = s +

Console. Write (ss); the final output result is 1020. When they are added, the data is assimilated by the string type, and the string type is the highest level (double floating point type will also be assimilated ).

 

Display conversion (forced conversion), which can be used to convert any type to any other type.

There are several methods: 1. int a; doubile B = 10.5; a = (int) B; (low priority is not recommended ).

2. Use convert to Convert the character type to another type: int a = Convert. ToInt32 (s); decimal d = convert. ToDecimal; the type To convert.Except string

3. string sss = "10 ";

String aaa = sss + a. Tostring (); To convert other types to string types, add. ToString ();

4. If the string type is not an integer, an error will be returned if it is converted to the int type. To convert the floating point type to the int type, if it is an odd number, it will enter 1. If it is an even number, it will be discarded.

Escape Character/"/" WillConverts a character to a string.

 

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.