C #3.0 learning notes (9) Conversion

Source: Internet
Author: User
Tags unpack

 

1. What is conversion?

A: conversion is a process of accepting a value of a type and using it as an equal value of another type.

 

2. implicit conversion?

Language (C #) automatically performs these conversions without losing data or precision. This is called implicit conversion.

For example, convert an 8-bit value to a 16-bit value.

 

3, explicit conversion (or forced conversion )?

If the language does not provide automatic conversion, if you still want to convert the source type to the target type, it is called explicit conversion.

The program is responsible for the loss of data or precision during implicit conversion, and the programmer is responsible for the loss of data or precision during explicit conversion.

For example:

Class Program {static voidMain (string [] args) {ushort sh = 10; byte sb = (byte) sh; // skip Console. writeLine ("sb = {0}, no loss occurred at this time. ", Sb); sh = 1365; sb = (byte) sh; // confirmed zookeeper Console. writeLine ("sb = {0}, the data volume is lost at this time. ", Sb); Console. ReadKey ();}}

The program output result is:

 

4. What is the conversion type?

As shown in:

5. How do I convert numbers?

1> implicit numeric conversion:

The numeric type that occupies a few digits can be implicitly converted to the numeric type that occupies more digits.

2> display numeric conversion:

For example:

Class Program {static voidMain (string [] args) {ushort sh = 2000; byte sb; sb = unchecked (byte) sh); // unchecked operation operator, no memory overflow occurs. Most important bits are lost. Console. writeLine ("sb = {0}", sb); Console. readKey (); sb = checked (byte) sh); // The checked operation operator, which causes overflow and OverflowException loss. Console. WriteLine ("sb = {0}", sb); Console. ReadKey ();}}


6. Reference conversion?

The reference conversion accepts the source reference and returns a reference pointing to the same location in the heap, but marks the reference as another type.

For example:

Class A {public int Field1;} class B: A {public int Field2;} class Program {static voidMain (string [] args) {B myVar1 = new B (); A myVar2 = (A) myVar1; // returns A reference to myvar1. Console. writeLine ("{0}", myVar2.Field1); // confirm the Console. readKey (); Console. writeLine ("{0}", myVar2.Field2); // specify invalid parameter because Field2 is invisible to myvar2. }}

 

7. Boxing )?

Definition: boxing is a value that accepts the value type. Based on this value, a complete reference type object is created on the stack and an implicit conversion of the Object Reference is returned.

For example:

Class Program {static voidMain (string [] args) {int I = 10; // creates and initializes the value type. Object oi = I; // create a volume and initialize the reference type (use the volume box and then change the copy of I to initialize ). Console. WriteLine ("I = {0}, oi = {1}", I, oi); // demonstrate how I and oi are operated by commit. I = 12; oi = 15; Console. WriteLine ("I = {0}, oi = {1}", I, oi); Console. ReadKey ();}}

The program output result is:

Shows the program layout in the memory:

8. unboxing )?

Definition: Unpacking refers to the process of converting the boxed object back to the value type.

Note the following:

1> unpacking is an explicit conversion.

2> An InvalidCastException is thrown when you try to unpack a value of Non-primitive type.

For example:

Class Program {static voidMain (string [] args) {int I = 10; // create and initialize the value type. Object oi = I; // pack I and assign the reference value to oi. int j = (int) oi; // unpack and assign the value to j. console. writeLine ("I = {0}, oi = {1}, j = {2}", I, oi, j); Console. readKey ();}}

The program output result is:

 

 

9. User-Defined conversion?

In addition to standard conversions, we can also define implicit and explicit conversions for classes or structures. This is called User-Defined conversions.

Note the following:

1> besides the implicit and explicit keywords, the Declaration Syntax of implicit and explicit conversions is the same.

2> public and static modifiers are required.

For example:

Class Program {static voidMain (string [] args) {Person bill = new Person ("bill", 25); // instantiate the object. Int age = bill; // converts a Person object to an int object. Console. WriteLine ("Person Info: {0}, {1}", bill. Name, age); Person anon = 35; // convert int to Person object. Console. writeLine ("Person Info: {0}, {1}", anon. name, anon. age); Console. readKey () ;}} class Person {public string Name; // declare the variable. Public int Age; public Person (string name, int age) // constructor, initialization variable. {Name = name; Age = age;} public static implicit operator int (Person p) // convert Person to int. {Return p. Age;} public static implicit operator Person (int I) // convert int to Person. {Return new Person ("Nemo", I );}}

The program output result is:

 

I wrote about the conversion here today. You are welcome to make a brick. Thank you!

 

Author: Permanent Wheat

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.