NET CLR via C # (4th Edition) Chapter 4th type Basics

Source: Internet
Author: User
Tags field table

Content: 1 All types derive from System.Object 2 type Conversions 3 Namespaces and assembly 4 Runtime interrelationships   This chapter covers the basics of using types and the CLR. Specifically, you will discuss a set of basic behaviors that all types have. Discusses type safety, namespaces, assemblies, and how to convert objects from one type to another. At the end of this chapter, the relationships between types, objects, line stacks, and managed heaps are explained at run time.  4.1 all types derive from System.Object the CLR requires that each type ultimately derives from the System.Object type. This ensures that each object of the class has a basic set of methods. [Public Method] equalsgethashcodetostringgettype[protected method]MEMBERWISECLONEFINALIZE CLR requires all objects to be created with the new operator. What the following new operator does: 1 computes the number of bytes that are required for all instance fields defined in the type and all of its base types (until System.Object, although it does not define its own instance field). Each object on the heap requires some extra members, including the type object pointer and the synchronization block index. The CLR uses these members to manage objects. The number of bytes in the extra member is counted to the object size. 2 allocates the memory of the object by allocating the number of bytes required by the type from the managed heap, and all allocated bytes are set to 0. 3 Initialize the object's type object pointer and synchronize block index members. 4 invokes the instance constructor of the type, passing the argument specified in the new call. Most compilers automatically generate code in the constructor to call the base class constructor. Each type of constructor is responsible for initializing the instance fields of the type definition. Eventually called the System.Object Constructor, the constructor does nothing and simply returns. After all these operations have been performed,  new returns a reference (or pointer) to the new object. And there is no delete operator corresponding to the new operator; in other words, there is no way to explicitly release the memory allocated for the object. The CLR uses a garbage collection mechanism that automatically detects that an object is no longer being used or accessed and frees the memory of that object. One of the most important features of the  4.2 type conversion CLR is type safety. At run time, the CLR always knows what type of object it is. You can call the GetType method.  CLR allows you to convert an object to its (actual) type or to any of its base types. C # does not require any special syntax to translate an object into any of its base types, because conversions to a base type are considered a safe implicit conversion; However, when you convert an object to one of its derived types, C # requires developers to only explicitly convert, because the conversionMay fail at run time.   using the IS and as operators in C # to transform is to check whether an object is compatible with a specified type, return a Boolean value, the IS operator never throws an exception, and the CLR's type checking enhances security, but undoubtedly has a certain impact on performance. This is because the CLR must first determine the actual type of the object that the variable refers to, and then the CLR must traverse the inheritance hierarchy to check the specified type with each base type. Since this is a fairly common programming pattern, C # specifically provides the AS operator to simplify the way this code is written, while improving performance. The CLR verifies that an object is compatible with a type, and if so, as returns a non-null reference to the same object, if incompatible, as returns NULL. Note: The As operator checks the object type only once.  c# allows a type to define a conversion operator method, as described in section 8.5, "Conversion operator methods". The  4.3 namespace and assembly namespaces logically group related types, and developers can easily locate types through namespaces.   for compilers, the role of namespaces is to divide the name attachments with periods, making the names longer and more likely to be unique.  clr knows nothing about namespaces. Another form of a C # using directive allows you to create aliases for a type or namespace.   Relationships between namespaces and assemblies note that namespaces and assemblies (Implementation-type files) are not necessarily relevant. In particular, types in the same name space may be implemented in different assemblies, and the same assembly may contain types in different namespaces. When you look up a type in a document, the document explicitly states the namespace to which the type belongs, and the assembly that implements the type.  4.4 Runtime Relationships This section explains the interrelationships of types, objects, line stacks, and managed heaps at run time. In addition, the difference between calling a static method, an instance method, and a virtual method is explained.   Prologue (Prologue) Code: Initializes the method before it starts the work, and the epilogue code: Cleans up the method after it is done to return to the caller. All objects on the   heap contain two additional members: the type object pointer and the synchronization block index. The   object contains a pointer to the object's type object (the type object contains a static field table and a method table).  CLR These members must be initialized when the type object is created. When the CLR starts running in a process, it immediately creates a special type object for the System.Type type defined in MSCorLib.dll. Both the employee and manager type objects are instances of that type. Therefore, their type object pointer members are initialized to a pair of System.Type type pairsThe reference to the image. Of course, the System.Type type object itself is also an object, and there is also a "type Object pointer" member inside. This pointer points to itself because the System.Type type object itself is an "instance" of a type object. This is the entire type system of the CLR and how it works. That is, the GetType method returns a pointer to the object's type object so that it can determine the true type of any object in the system.     

NET CLR via C # (4th Edition) Chapter 4th type Basics

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.