C # Type Conversions

Source: Internet
Author: User

The knowledge of C # type conversion is a big catch in every book and blog post. So why do I have to repeat these words----only because they are described in various articles is very good, clear, but in the actual use (writing code) has been ignored. This is the only note.

For example, when a database is encapsulated to perform a method that returns the number of affected rows excutenonquery The method returns the object type, and to the top of the business, it needs to be converted to a numeric type (such as int), depending on whether it is greater than zero to determine whether the operation succeeds.

At the time, my idea was that the underlying Excutenonquery method returned the int type, and the direct strong

String insertstr= "insert INTO ..."     ///INSERT DATABASE statement Xxxdal xxxdal=new xxxdal ()       //dal bottom for ORM a encapsulated object result = Xxxdal.excutenonquery (INSERTSTR)  if ((int) result>0) {//todo after successful operation ....} else{}

Fatal error, (int) result class conversion errors. The object returned at the bottom is actually a long type (about why the underlying is returning a long and temporarily ignoring it).

It was a long time on this question, why should I write this? ----Just simply think that the number of rows to be returned is an int; second, it is reasonable to write, others frame returns an object, and you need to turn it into an int, how to write it reasonable? -----1, Convert.toint (), 2, Int. TryParse (); 3, direct strong turn.

C # Type value types

BOOL-type Boolean with a value of true or False

Char, character type, occupies two bytes, representing 1 Unicode characters (C # is a unified two bytes, not so-called Chinese only two bytes)

byte-to-bytes, 1 bytes, representing 8-bit positive integers, range 0 to 255

SByte, signed byte type, 1 bytes, 8-bit integer, range 128 to 127

ushort, unsigned short integer, accounting for 2 bytes, representing 16 positive integers, range 0 to 65,535

UINT, unsigned integer, 4 bytes, representing 32 positive integers, range 0 to 4,294,967,295

ULONG, unsigned long integer, accounting for 8 bytes, representing 64 positive integers, range 0 ~ approximately 10 20-time Square

Short integer, 2 bytes, representing 16-bit integers, range 32,768 to 32,767

int, Integer, 4 bytes, representing 32-bit integers, range 2,147,483,648 to 2,147,483,647

Long-I, which is 8 bytes, represents 64-bit integers and ranges approximately-(10 of 19) to 10 of a second square

Float, single-precision floating point, 4 bytes

Double-precision floating-point type, accounting for 8 bytes

Decimal->128-bit high-precision floating-point number, often used in financial operations, do not appear the error of floating-point calculation (not the basic type in the CLR, the operation of the decimal type variable is not directly generated IL directive, but the method of invoking the System.Decimal structure)

enumeration, struct

Reference type

1, string

2. Object

3. Class

4. Commission

5. Interface

6. Arrays

Type conversions

Conversions between basic types

C # Type Conversions

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.