Learning C # advanced programming together 1-type security,

Source: Internet
Author: User

Learning C # advanced programming together 1-type security,

Reference page:

Http://www.yuanjiaocheng.net/CSharp/Csharp-enum.html

Http://www.yuanjiaocheng.net/CSharp/Csharp-stringbuilder.html

Http://www.yuanjiaocheng.net/CSharp/csharp-array.html

Http://www.yuanjiaocheng.net/CSharp/Csharp-multi-dimensional-array.html

Http://www.yuanjiaocheng.net/CSharp/Csharp-jagged-array.html

I felt that I had encountered difficulties on the first day. It was very time-consuming. I had to go back to the dormitory for more than 8 o'clock, and then I had to read only a few books. Then write the essay. First, remember, organize, and finally type. There is also the days of network disconnection in the dormitory, which is particularly inconvenient and can only be sent to the company the next day. I am currently. net Development intern. I learned a little before. net knowledge. Now, I plan to consolidate C #. After reading C # advanced programming, I feel that I have a huge amount of knowledge, and I still have some branches that I don't know. Although I don't know what is worth learning, learn it first.

Now, let's get started: Starting from the forced conversion of operators and types in chapter 1 of C # Advanced Programming

Type Security

Type conversion

First, let's look at a piece of code:

Byte value1 = 10;

Byte value2 = 20;

Byte total = value1 + value2;

Console. WriteLine (total );

There seems to be no error at first glance, but the following error message will pop up in:

 

Error: The type "int" cannot be implicitly converted to "byte ". There is an explicit conversion (is forced conversion missing ?)

This is because byte can only be 8 bits. When two byte types are added together, it is easy to obtain a value larger than 8 bits, which reflects the strong security of the C # type;

 

In this case, set Byte total = value1 + value2;

Change to Byte value3 = (byte) (value1 + value2); // display Conversion

Or Int value3 = value1 + value2; // hermit Conversion

 

The preceding two types of conversions are used,Implicit conversionAndExplicit Conversion

Implicit conversion:

That is, it can be converted from a small data type to a large data type.

Note:

Unsigned variables can be converted to signed variables, as long as the unsigned variable value is within the signed variable value.

 

· An empty type to be converted (the type character is followed by "?", Can be Null ):

· The non-empty type conversion rules follow the non-empty type conversion rules, that is, the conversion from a small type to a large type

· The non-empty type can be implicitly converted to an empty type, but the non-empty type cannot be implicitly converted. This is because the null type value can be null, but the non-null type cannot be null.

 

 

Explicit conversion:

It is forced conversion. It is about the overflow of values.

For example:

Long val = 3000000000;

Int I = (int) val;

Arithmetic overflow, which can be detected by unchecked and checked.

The default value is unchecked, that is, it does not detect value overflow.

Using checked will force the runtime to throw an exception:

Long val = 3000000000;

Int I = checked (int) val );

Or use {} to wrap the statement for verification overflow.

Checked

{

Int I = checked (int) val );

}

 

Exception Handling will also be introduced later using try {} catch {}.

 

As mentioned earlier, the non-empty type must be converted to the display type. Such as int? Convert to int. This is because the null type is allowed to be null.

If the value of the null type is converted to a non-null type, an exception is thrown.

Int? A = null;

Int B = (int) a; // will throw an exception

 

If you need to convert between numbers and strings, you can try the method provided in the. Net Class Library: ToString ();

Int I = 10;

String s = I. ToString ();

 

For type conversion,. Net also provides two ways to change the seat belt: Parse () and Convert.

For example:

String someString = "22 ";

Int value1 = Int32.Parse (someString );

Int value2 = Convert. ToInt32 (someString );

 

We also mentioned thatPackingAndUnpack.

Packing means converting the value type to the Object type of the reference type. In this case, the Object type is converted to the value type again.

Example:

Int myInt = 10;

Object myObject = myInt; // boxed, value type can be converted to reference Object type

Int myInt2 = (int) myObject; // binning, the opposite process of packing, requires forced conversion

 

Packing and unpacking can be used. For example, if a method needs to reference A type parameter, the value type is boxed into a reference type, and the value type can be split into a value type.

 

At, I wrote this article first today. Since the reading time is relatively late, the writing speed is slow and far lags behind the reading speed. With a little tail left, it's aboutComparison between objects.

Write next tomorrowOperator.

In the future, I will try to share some C # learning content every day. I hope you can work together to develop C #. Haha. Please kindly advise!

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.