آموزش تبدیل نوع داده در سی شارپ
قبلاً در این کتاب، دیدیم که تمام داده ها، صرفنظر از نوع شان، دنباله ای از بیت ها هستند؛ یعنی دنباله ای از صفرها و یک ها می باشند. معنای متغیر، با نحوه تفسیر این داده ها تعیین می شود. ساده ترین مثال آن، نوعِ char می باشد. این نوع، یک کاراکتر را در مجموعه کاراکترهای یونیکد(Unicode) با استفاده از یک عدد، مشخص می کند.
In fact, the number is stored in exactly the same way as a
ushort—both of them store a number between 0 and 65535.
However, in general, the different types of variables use varying schemes to represent data.
This
implies that even if it were possible to place the sequence of bits from one variable into a variable of
a different type (perhaps they use the same amount of storage,
or perhaps the target type has enough
storage space to include all the source bits), the results might not be what you expect.
Instead of this one-to-one mapping of bits from one variable into another, you need to use type
conversion on the data. Type conversion takes two forms:
➤➤ Implicit conversion—Conversion from type A to type B is possible in all circumstances, and
the rules for performing the conversion are simple enough for you to trust in the compiler.
➤➤ Explicit conversion—Conversion from type A to type B is possible only in certain circumstances
or where the rules for conversion are complicated enough to merit additional processing
of some kind.