Packing is very good, what is it inside?

zhaozj2021-02-08  241

Packing is very good, what is it inside?

Author: Eric Gunnerson Last month, it took us some time to learn how to find and C # related things. I received some inquiry, asked about the Web site about C # or .NET, so I decided to add a web site meticulous program in the column. Write a letter tell me your site, I will randomly select five sites every month, and the URLs are listed in the column. The opening of the opening is over, and the words retired. There are two types in the type C # and public language runtime (CLR) in C #: Reference Type (Class Class Declaration in C #) and Value Type (Structure Declaration in C #). Quote and value types are different in several important aspects. The following table summarizes these differences:

Reference (Class) Value (Structure) Reserved Variables Reference Actual Value Activity Value The value of the inner inner (in the stack or inner lines) Default value null zero = Represents the replication reference replication value type "feel" like a data. It includes predefined numeric types and user-defined types such as Complex numbers, Point or Rectangle. As described above, the variable of the value type is the actual value, so when you use the variable, it usually processes the actual value. INT i = 123;

INT j = i;

i = 55;

After the second specified variable, the two independent variables contain the same value. The value of modifying i will not change the value of J. The reference type is used for all objects that cannot be used as a value type. The reference type variable points to the instance of the object in the heap. This means that when a variable is specified to another variable, only the reference is specified, not the value. Employee E = New Employee ("fred");

Employee f = e;

F.Name = "barney";

After the second specified variable, e and f point to the same object. This means that the name of modifying F will also change the name of E because they reference the same example. This triggered a related topic. Some people may always be strange, why do the functions in the System.String class do not modify strings, but always return a new copy of the string. This is because the type of string is a reference type. If you call S.trim () to modify the string of the string, you will encounter the same problem as Employee (this is very bad for the string). A member of the modified class value is called "Customer" without any modified class. The presence of a non-variable class can make the behavior of the class similar to the value, but cannot be written as a value class. If you need to use a variable string class, try StringBuilder in System.Text. Turning to a simpler model is important to use both references and values ​​in language. The value type is light and efficient, and the reference type applies to object-oriented development. However, now we have two types, and we need to be a simpler model, using a single, capable of edging all possible values. Such a general base class can:

Call any virtual function of any value. Write a collection class that can store any value. Alternative Ole Automation Variant Types. To achieve this, a method of transforming the value type when needed in the public language runtime, is a process called a package. The type of packaging is a general base class that can be referenced by various types of objects. Packaging and release packaging Consider the following code: INT VALUE = 123;

Object o = value; // Pack INT into the object

INT Value2 = (int) O; // Release Packaging to Value2

When assigning the value, as part of the assignment, the C # compiler will create a reference type package sufficient to accommodate int, copy the value to the package, then mark the package as the actual type (here is System.Int32), so that Learn the type of packaging at runtime. To take the value from the package, you must use the forced type to install the type of package (the object can keep any type). During the execution, the type of type reference reference to the object variable will be specified in the mandatory type conversion at runtime. If the type is correct, the value will copy the value type variable from the package. If the type is incorrect, it will cause an exception. Note that other conversions are not performed during the release process; the type must be fully matched. In other words, if we write code: long value2 = (long) O; // The value of the package is INT where o is the packaged int, will result in an exception. However, we can write this: long value2 = (long) (int) O;

The conversion will be done normally. Although this example demonstrates packaging and release packaging, it may cause some misunderstanding. Writing code is very rare, typically use when passing a variable of the value type to the type object parameter. Here is a small test. I hope everyone will do it with your heart. Test: How much is your packaging? The following code segment gives different solutions. Please read these code, judge which code involves packaging, and which segment has nothing to do with the packaging. Some programs (such as b) need to check multiple locations. // plan 1

INT TOTAL = 35;

DateTime Date = DateTime.now;

String s = string.format ("Your Total Was {0} on {1}", Total, Date);

/ / Solution B

Hashtable t = new hashtable ();

T.Add (0, "Zero");

T.ADD (1, "one");

/ / Solution C

DateTime D = datetime.now;

String s = d.toString ();

/ / Solution IV

int [] a = new int =

a [0] = 33;

/ / Solution 101

ArrayList a = new arraylist ();

A.Add (33);

/ / Solution VI

MyStruct s = new mystruct (15);

IProcess IP = (iProcess) S;

Ip.Process ();

Please check the answer and record the score. Scenario 1 string.format () uses the string as the first parameter, the object as the second and third parameters. INT and dateTime are all value types, so they will be packaged as the second and third parameters. String.format () uses these parameters, then calls Object.toString () to a string representation to each parameter. If you know that Int will be packaged, you have a point; if you know DateTime being packaged, you have a point. Scenary B HashTable.Add () has two parameters, one is the keyword, the other is the value. They are all types of objects. The value passed to the keyword parameter is an integer, so it must be packaged to be passed as an object. The value passed to the value parameter is a string (reference type), so the string does not need to be packaged. Every point is judged to have a certain point. The code C is very confused. One of the purpose of the packaging is to achieve virtual function calls for value type parameters. Tostring () is the virtual function of the object, so it looks when calling toString (), D will be packaged. However, when the object is converted, it is not necessary to use D, so no packaging is required. The compiler knows that the variable of type DateTime can only be used for this type (because the variable cannot be exported), so it can call DateTime.toString (), and set "this" reference, make it Point D in the stack. If you answer the correct, you have a point. The array in the solution IV CLR stores their values. For example, an int array having five elements has enough space to save 5 int instead of 5 objects. If this code is not related to the packaging, you have a point. Scenario 101 ArrayList.Add () uses an object as a parameter, so integer 33 will be packaged. If you answer the correct, you have a point. The scenario VI interface is a reference type, so it is necessary to package the value type when converting the value type to the interface implementation. If you understand this, you have a point. Finally, the score is added, and the following table is used to check the level of understanding of the package: score instructions 8. I know 6-7 on the package, but sometimes understand, sometimes confused 3-5, please continue to work harder 1-2 Continue learning 0 has not yet introduced abstracts to summarize the packaging. The packaging makes it easy to write and use a function with general purpose object parameters. Like many wonderful things, packaging has a bad side. Next month, we will discuss what these bad, how to reduce negative impacts, and further explanation to C # (this will make our C # life more beautiful). C # Web site gather below the Web site is I got by email, but I know this only. If the code there cannot be compiled, the computer crashes, or encounters all kinds of trouble, please don't complain to me. There are only two items this month:

Http://www.csharphelp.com http://dotNetwire.com

Eric Gunnerson is a QA leader of the C # compiler group, a member of the C # design group, and the author of "a Programmer's Introduction to C #". He has a long programming, and even knows what is an 8-inch disk, and it can be used with one hand. Archive Working with C # column articles

转载请注明原文地址:https://www.9cbs.com/read-1614.html

New Post(0)