C #. NET Learning Experience Summary

Source: Internet
Author: User
Keywords We string numeric byte can

1. box, unboxing or alias

Many introduce C #. NET learning experience books on the introduction of the int-> Int32 is a boxing process, the reverse is the process of unpacking. This is true of many other variable types, such as short <-> int16,long <->int64. For the average programmer, it is not necessary to understand this process, because these boxes and unboxing actions can be automatically completed, do not need to write code to intervene. But we need to remember the relationships between these types, so we use "aliases" to remember their relationships.

C # is a fully object-oriented language, and is more thorough than Java object-oriented-it encapsulates simple data types through default boxing actions into classes. Int32, Int16, Int64 and so on are the corresponding class names, and those familiar, simple and easy to remember names, such as int, short, long, and so on, we can call it the Int32, Int16, Int64 and other types of aliases. What other classes have "aliases" Besides these three types? Commonly used are the following:

BOOL-> System.Boolean (Boolean, with a value of true or false)
Char-> System.Char (character type, occupies two bytes, representing 1 Unicode characters)
byte-> system.byte (byte type, 1 bytes, 8-bit positive integer, range 0 ~ 255)
SByte-> System.SByte (signed byte, representing 1 bytes, 8-bit integer, range-128 ~ 127)
ushort-> system.uint16 (unsigned short integer, accounting for 2 bytes, representing 16-bit positive integers, range 0 ~ 65,535)
UINT-> system.uint32 (unsigned integer, accounting for 4 bytes, representing 32-bit positive integers, range 0 ~ 4,294,967,295)
ULONG-> System.UInt64 (unsigned long integer, accounting for 8 bytes, representing 64-bit positive integers, range 0 ~ approximately 10 20 times)
Short-> system.int16 (2 bytes, representing 16-bit integers, range-32,768 ~ 32,767)
int-> System.Int32 (integer, 4 bytes, 32-bit integer, range-2,147,483,648 to 2,147,483,647)
Long-> System.Int64 (8 bytes, representing 64-bit integers, range approximately-(10 of 19) to 10 of 19)
Float-> system.single (single-precision floating-point type, up to 4 bytes)
Double-> system.double (double-precision floating-point type, up to 8 bytes)

We can do an experiment with the following code:

private void Testalias () {


//This.textbox1 is a text box with a type of System.Windows.Forms.TextBox


The Multiline property has been set to true in the design





byte a = 1; Char b = ' a '; Short C = 1;


int d = 2; Long e = 3; UINT F = 4; bool G = true;


this.textBox1.Text = "";


this.textBox1.AppendText ("byte-&gt;" + A.gettype (). FullName + "\ n");


this.textBox1.AppendText ("char-&gt;" + b.gettype (). FullName + "\ n");


this.textBox1.AppendText ("Short-&gt;" + c.gettype (). FullName + "\ n");


this.textBox1.AppendText ("int-&gt;" + d.gettype (). FullName + "\ n");


This.textBox1.AppendText ("Long-&gt;" + e.gettype (). FullName + "\ n");


this.textBox1.AppendText ("uint-&gt;" + f.gettype (). FullName + "\ n");


this.textBox1.AppendText ("bool-&gt;" + g.gettype (). FullName + "\ n");


}

Create a new button in the form and call the Testalias () function in its Click event to run the following results:

BYTE-> System.Byte
Char-> System.Char
Short-> system.int16
int-> System.Int32
Long-> System.Int64
UINT-> System.UInt32
BOOL-> System.Boolean

This is enough to explain the corresponding class of aliases!

2. Conversion between numeric types

The numeric types mentioned here include Byte, short, int, long, fload, double, etc., according to which the various types of values can be converted automatically backwards. For example, to assign a short type of data to a variable of type int, the short value is automatically converted to an int value and then assigned to an int variable. The following example:

private void Testbasic ()


{


byte a = 1; Short B = A; int C = b;


long d = C; float E = D; Double F = e;


this.textBox1.Text = "";


this.textBox1.AppendText ("byte a =" + a.tostring () + "\ n");


This.textBox1.AppendText ("Short B =" + b.tostring () + "\ n");


this.textBox1.AppendText ("int c =" + c.tostring () + "\ n");


This.textBox1.AppendText ("long d =" + d.tostring () + "\ n");


This.textBox1.AppendText ("Float e =" + e.tostring () + "\ n");


This.textBox1.AppendText ("Double f =" + f.tostring () + "\ n");


}

The result is that the values of each variable are 1; Of course, their type is System.Byte type ... System.Double type. Now let's try, what if the order of assignment turns out to be? Append the following statement to the Testbasic () function:

int g = 1;


short h = g;


this.textBox1.AppendText ("h =" + h.tostring () + "\ n");

Result compilation error: G:\Projects\Visual C#\convert\form1.cs (118): Could not implicitly convert type "int" to "short", where the line of Form1.cs 118 is short h = G.

This time, if we insist on conversion, we should use coercion type conversion, which is often mentioned in C language, using statements in the form of "(type name) variable name" to cast data. The above example is amended as follows:

short g = 1;


byte h = (byte) g; Cast the value of the short type G to the short type and assign to the variable H


this.textBox1.AppendText ("h =" + h.tostring () + "\ n");

The compiler passed, the results of the operation output H = 1, the conversion succeeded.
However, if we use a cast, we have to consider another problem: the short type range is 32768 ~ 23767, and the byte range is 0 ~ 255, so what happens if the size of the variable g exceeds the byte range? We might as well rewrite the code again, change the value to 265, 10 more than 255.

Short g = 265; 265 = 255 + 10


byte h = (byte) g;


this.textBox1.AppendText ("h =" + h.tostring () + "\ n");

There is no error in compiling, but the result is not H = 265, but H = 9.
Therefore, when converting, we should be aware that the converted data cannot exceed the scope of the target type. This is not only reflected in multibyte data types (relative, as in the case of short in the example above, when converted to a less-byte type (relative, as in byte in the example above), it is also reflected between the same signed and unsigned types of byte numbers, such as converting byte 129 to sbyte overflow. This is a very similar example, not to elaborate.

3. ASCII code and Unicode code for characters

Many times we need to get an ASCII code for an English character, or a Unicode code for a kanji, or to query the encoding of which character it is from. Many people, especially those who have switched from VB to learning C #, complain that there are no ready-made functions to do this in C # because there are ASC () functions and CHR () functions in VB for such conversions.
But if you've studied C, you'll know that we just need to force the English character data to be converted to the appropriate numeric data, and we can get the corresponding ASCII code, otherwise, if a suitable numeric data is coerced into character data, the corresponding character can be obtained.
The range of characters in C # is expanded to include not only single-byte characters, but also double-byte characters, such as Chinese characters. In the case of conversion between characters and encodings, the C-language approach is still in force-casting. Consider the following example

private void Testchar ()


 {


char ch = ' a '; Short II = 65;


this.textBox1.Text = "";


This.textBox1.AppendText


("The ASCII Code of \" "+ ch +" \ is: "+ (short) ch +" \ n ");





This.textBox1.AppendText


("ASCII is" + ii.) ToString () + ", the char is:" + (char) II + "\ n");


char cn = ' Medium '; Short UC = 22478;





This.textBox1.AppendText


("The Unicode of \" + CN + "\ is:" + (Short) cn + "\ n");


This.textBox1.AppendText


("Unicode is" + UC.) ToString () + ", the char is:" + (char) UC + "\ n");


}

The result of its operation is

The ASCII code of ' a ' is:97
ASCII is is:a, the Char
The Unicode of ' is:20013 '
Unicode is 22478, the char is: City

From this example, we can understand very clearly--by casting, you can encode the characters, or get the characters that are encoded. If you do not need a short type of code, please refer to the 1th to convert, you can get int and other types of encoded values.

4. Conversion between numeric string and numeric value

First, we need to figure out what a numeric string is. We know that in C #, strings are represented by a pair of characters contained in double quotes, such as "123". and "123" is relatively special, because the character that makes up the string is a number, such as a string, a numeric string. In our eyes, this is a string of characters and a number, but the computer only thinks it is a string, not a number. So, at some point, we convert a string to a numeric value when we enter a value, and at other times we need the opposite conversion.

Converting a number to a string is simple because each class has a void ToString () method. All numeric type void ToString () methods can convert data to numeric strings. such as 123.ToSting () will get the string "123".

So what happens when you convert a numeric string to a numeric value? When we look closely, we find that numeric types such as short, int, float have a static Parse () function. This function is used to convert the string to the corresponding numeric value. We take a float-type conversion as an example: float f =float. Parse ("543.21"); The value of the result F is 543.21F. Of course, other numeric types can be converted using the same method, and the following example can be more explicit about the conversion method:

private void Teststringvalue ()


{


float f = 54.321F;


string str = "123";


this.textBox1.Text = "";


This.textBox1.AppendText ("f =" + f.tostring () + "\ n");


if (int. Parse (str) = = 123


{


this.textBox1.AppendText ("str convert to int successfully.");


}


Else


{


this.textBox1.AppendText ("str convert to int failed.");


}


}

Run Result:

f = 54.321
STR convert to int successfully.

5. Conversion between string and character array

The String class System.String provides a void ToCharArray () method that enables the conversion of strings to character arrays. The following example:

private void Teststringchars ()


{


string str = "MyTest";


char] chars = str. ToCharArray ();


this.textBox1.Text = "";


This.textBox1.AppendText ("Length of \" Mytest\ "is" + str. Length + "\ n");


This.textBox1.AppendText ("Length of char array is" + chars.) Length + "\ n");


This.textBox1.AppendText ("char[2] =" + chars[2] + "\ n");


}

example, the length of the character array to which the transformation was converted and one of its elements were tested, with the following results:

Length of "MyTest" is 6
Length of char array is 6
CHAR[2] = t

The

can see that the results are completely correct, which indicates a successful conversion. So what about converting character arrays to strings, in turn?
We can use the constructor of the System.String class to solve this problem. The System.String class has two constructors that are constructed from a character array, i.e. String (char]) and string[char[], int,int). The latter has two more arguments because you can specify which part of the character array to construct the string. The former, however, constructs the string with all the elements of the character array. Let's take the former example and enter the following statement in the Teststringchars () function:

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.