Data type conversion (packing and unpacking) and constants, Data Type Packing Constants

Source: Internet
Author: User

Data type conversion (packing and unpacking) and constants, Data Type Packing Constants

Implicit conversion [automatic type conversion]:

The two types must be compatible. The original value range must be smaller than the target value range, which can be easily understood as small to large.
1 public class Test2 {3 private void Start () 4 {5 int a = 10; 6 float B = a; // implicit conversion of the int type to the float Type 7} 8}
 

Display conversion [Forced type conversion]:

The two types must be compatible. The original value range must be greater than the value range of the target type, which can be simply understood as large to small.

[Disadvantages]: 1. Data overflow. 2. Loss of precision.

Conversion between numeric types.

1 public class Test2 {3 private void Start () 4 {5 float a = 1.5f; 6 int B = (int); // float type is forcibly converted to int type 7} 8}

To convert other types to string types, call the ToString () method.

1 public class Test 2 {3 private void Start () 4 {5 float a = 1.5f; 6 bool val = true; 7 string str = val. toString (); // forced conversion of the bool type to the string type 8 str =. toString (); // float Type forcibly converted to string type 9} 10}

To convert the string type to other basic types, call the Parse () method and the TryParse () method.

1 public class Test 2 {3 private void Start () 4 {5 string str = "true"; 6 bool v = bool. parse (str); // If the conversion is successful, the value is assigned to the variable v. If the conversion is unsuccessful, an error is returned. 7 int num; 8 bool val = int. TryParse (str, out num); // A bool value is returned no matter whether it is successful or not. If the conversion is successful, the value is assigned to the variable num. 9} 10}

Convert is used for conversion between other basic types. (To use Convert, You need to reference the using System assembly)

 1 using UnityEngine; 2 using System; 3  4 public class Test 5 { 6     private void Start() 7     { 8         bool val = false; 9         int num = Convert.ToInt32(val);10         int a = 0;11         bool v = Convert.ToBoolean(a);12     }13 }

 

Packing: Convert the value type to the reference type.

Binning: converts a reference type to a value type.

Note: 1. binning or unboxing may only occur when there is an inheritance relationship between the two types.

2. packing and unpacking are essentially changes in data storage between the stack space and the heap space. Therefore, frequent packing or unpacking will reduce the operating efficiency, therefore, try to use less packing or unpacking operations in the code.

 

Constant:A variable. Once declared, a constant cannot be assigned a value again.

Naming rules: constants are generally named in uppercase and words are separated by underscores (_ IP ).

Constant Declaration: the key word const needs to be added and must be assigned a value during declaration. (For example, const int SERVER_IP ;)

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.