Learn C # (value type 3) a week

zhaozj2021-02-16  66

Search C # (value type 3) C # Talent Bird (QQ: 249178521)

7. Integer conversion

· Implicit conversion, from small to big conversion

W Will not lose accuracy, not throwing an error

· Explicit conversion, from large to small conversion (forced conversion)

W may lose the accuracy and may throw an error

INT m = int.maxvalue; // Maximum value

Short S;

Checked {s = (short) m;} // Explicit conversion, will throw overflow errors

Long N;

Checked {s = m;} // implicit conversion, will not throw overflow errors

8. Type conversion table

Sbyte

Short

int

Long

Byte

Ushort

Uint

Ulong

Sbyte

I i i

E e e e short e

I i i

E e e e int e e

I

E e e e long e e e e e

E e e e

BYTE E I i

I i i ushort e e i i

E

I I uint e e e i

E e

I ulong e e e e e

E e e

In the above table, e represents an explicit conversion, i represents implicit conversion.

转载请注明原文地址:https://www.9cbs.com/read-22165.html

New Post(0)