Can be empty type nullable, type conversion checked, explicit, implicit

Source: Internet
Author: User
Document directory
  • Checked
Nullable)

We know that all reference types can assign null values to indicate null references, while value types cannot assign null values.

Difference between value type and reference type

To understand why, you have to think about how the memory is represented after assigning values to a variable. if it is a value type, a space is allocated directly in the stack. stack is managed by the operating system. if it is a reference type, it is saved as two parts. allocate a memory in heap to save the actual value of the variable, and then allocate a memory in the stack to save the address of the memory allocated in heap. (managed heap, which is managed by CLR. C # Why is code managed? Because some of the operations behind it are managed by CLR ). if the value of the reference type is null, it indicates that the heap memory has not been allocated, but only one block of memory is allocated in the stack, but the content is blank and the heap address is not saved. what is the size of the memory block in the stack? All reference types have the same block size in the stack. The specific size depends on the operating system. The 32-bit system is 4 bytes, And the 64-bit system is 8 bytes.
Some people may think of a special example of the value type, struct. What if there is a reference type in struct? In fact, it is still the same. Allocate an address in heap and allocate block storage address memory in stack. however, if it is a reference type, for example, a class you declare, all the value types in it are saved in heap and not saved to stack.

 

To put it bluntly, the type can be empty. we know that the value of a column in the table can be set to null in the database. what should we do if we get this value from the database in C. generally, dbnull is used to determine whether the value is null. If yes, it will be replaced with a default value. but this is still not good. it is best to use null as it is. so there is an empty nullable. it has a function that can represent null, and other operations are exactly the same as the value type. so we can regard it as a general class (accurately speaking, it is actually a struct), but it looks a bit like an int ordinary value type.

For example, nullable <int> A = 123; nullable B = 456; nullable c = NULL; C = A + B;

There is also a short form for nullable. We usually use the short form. nullable <int> A = 123 is equivalent to int? A = 123; that is, add a question mark after the Int. It looks strange. If you want other value types to be the same, just add one after? It can be used to indicate the null type. For example, bool? B = true; long? L = NULL;

However, although it can be null, it can also perform arithmetic operations. But it can only be between Null Types. If the value type is mixed, type conversion must be performed.

Can be empty type to Value Type: Int? A = 123; int B = (INT) A; // must be explicitly converted

Value type conversion to null type: Int A = 123; Int? B = (Int ?) A; // can also be implicitly converted to int? B =;

 

Type conversion

 

We know that C # is a strong language.. Therefore, once the type is declared, it can only be of any type and cannot be used as another type. if you want to use it as another type, type conversion is required. most of the time, we did not find it because of implicit conversions. for example, assign the int value to the doubl or float type.

Supplement: in fact, it should be true that C #3.0 and earlier versions are strongly typed systems. however, C #4.0 supports dynamic languages. it cannot be a strong language. however, if dynamic language is used in C #4.0, DLR (Dynamic Language Runtime) is used instead of CLR (Common Language Runtime.

Numeric type conversion is very convenient, such as long num = 123l; int A = (INT) num;

Checked

We know that the range of each value type is different. for example, byte indicates the range from 0 to 255. int indicates a much larger range. if you convert an int value greater than 255 to a byte value, overflow occurs, but no error is reported by default. only an error value is returned. for example, int A = 256; byte by = (byte) A; // at this time, the value of by is 0. If a is 257, by is 1.

So how can we check for such errors when overflow occurs. checked will be used here, and its usage is very simple. write the checked keyword, and then enclose a piece of code in brackets. if the code block overflows, an error is returned.

For example, checked

{

Int A = 256;

Byte by = (byte);

}

At this time, compilation fails and an error occurs. if you use checked to include multiple codes. A small part of the Code does not require overflow check. then use unchecked and then {} to enclose the code.

Explicit, implicit

We know that C # pre-defined types can be converted like this: Long num = 123l; int A = (INT) num; // (INT) it is used to represent Explicit conversions.

 

What if the type is defined by myself? For example, how should the conversion between two classes be adjusted?

This requires you to write your own code. For example, there are two classes: Staff and member.

Public class staff

{

Public string name {Get; set ;}

Public int age {Get; set ;}

}

 

Public class member

{

Public string name {Get; set ;}

Public int age {Get; set ;}

}

If you want them to convert to one another

 

Public class member

{

Public string name {Get; set ;}

Public int age {Get; set ;}

Public static explicit operator member (staff s) // indicates that a staff instance can be converted to the member type through member M = (member) staffinstance;

{

Member me = new member ();

Me. Age = S. Age;

Me. Name = S. Age;

Return me;

}

Public static explicit operator staff (member me) // indicates that a member instance can be converted to the staff type through staff M = (staff) memberinstance;

{

Staff ST = new staff ();

St. Age = me. Age;

St. Name = me. Name;

}

}

The two functions used for conversion must be static functions, and operator must be used to represent them as overload functions. the other two functions can be defined in any one of the class staff or member. you can also separate the two functions and put one in a class. but it can only be defined in two categories and cannot be defined elsewhere.

In addition, explicit indicates Explicit conversions. Implicit conversions only replace the key explicit with implicit.

If Explicit conversions are defined as above, they can be used in the code.

Staff TA = new staff ();

Ta. Name = "Arwen ";

Ta. Age = 100;

Member me = (member) Ta;

Staff ST = (staff) Me;

Note that if there is an inherited class, we don't need to write two more functions for them to convert. They can directly convert it like this.

 

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.