Excerpt from-it Enterprise must read 200. NET face Questions-03. NET Type Syntax basics

Source: Internet
Author: User
Tags finally block net thread

Underlying types and syntax

What methods are included in the Q:system.object, and which are virtual methods

System.Object contains 8 methods, including Finalize, with 3 virtual methods: The Equals, GetHashCode, and ToString methods.

Q: difference between a value type and a reference type

All value types inherit from System.ValueType, and commonly used value types include structs, enumerations, integers, floating-point, Boolean, and so on. Assigning a value type results in a new copy of the data, so each value type has a copy of the data, and the assignment of the reference type is an assignment reference. Objects of value types are allocated on the stack, whereas objects of reference types are allocated on the heap. When comparing two value types, there is a comparison of the content, and a reference comparison is made when comparing two reference types.

Q: brief description of packing and unpacking principle

Analysis: Boxing is to move the value type on the stack onto the heap, and unpacking the object in the heap to the stack and return its value. Take a look at the steps required for boxing: Allocate a memory space on the heap, size equal to the size of the value type object that needs to be boxed plus the members that have two reference type objects: type Object pointer and synchronous block reference, copy the value type object on the stack to the newly allocated object on the heap, return a reference to the new object on the heap, and stored in an object of the value type that is boxed on the stack.

Answer: Boxing and unboxing are essentially a series of move operations on the stack and heap that the value type throws when transitioning to System.Object. The boxing value type is copied from the stack onto the heap, while unpacking is copied from the heap onto the stack. Packing and unpacking have a great impact on performance.

Q: Whether there are global variables in C #

C # has no traditional global variables, and in C # programs, any object data must belong to a type. The function of global variables can be realized through public static variables.

Q: What is the difference between struct and class, and where does the struct apply?

A struct is a value type and a class is a reference type. Structs do not have inherited attributes, structs cannot have parameterless construction methods, and cannot define initial values for member variables. Structs are often used to store collections of data and should be designed as classes rather than structs if complex operations are involved.

Q: When the initializer of the type is called

The initializer of a type is a method that has the same name as the type, no parameters, no return, and is defined in static. Invokes a type initializer before any member of the type is used.

Q: What kinds of methods can be passed in C # for a method's parameters

Ref, out, and params. Both ref and out implement a reference pass of a parameter, except that ref requires that the parameter be initialized before it is passed in, and out requires that the parameter be initialized before the method returns. The params implements a method that has a variable number of parameters.

Q: . NET supports which types of accessibility levels, C # implements which of the several

. NET defines 6 levels of accessibility, private, Family, Assembly, family&assembly, Family, or Assembly, and public. C # implements 5 levels of accessibility other than family&assembly, and the respective keywords are private, protected, internal, protected internal, and public.

Q: brief description of attributes and similarities and differences of properties and methods

A property in C # is a special method that has a return value without parameters, allowing the programmer to conveniently provide a pair of get/set (or any other) method to the member variable, with the use of the property fully consistent with the public member variable, but with better extensibility. The nature of the property is the same as the traditional getxxx/setxxx function, but the syntax is more graceful and the code more concise and readable.

Q: Brief description of shallow copy and deep copy in C #

Shallow copy refers to copying all value type members in a type, copying only references to reference type members, and having the target object share the reference type member object of the original object. Deep replication refers to objects that replicate both value type members and reference type members.

Q: What does a using statement in C # do

The using statement calls the Dispose method for a type object that implements the IDisposable, which guarantees that the Dispose method of the object being used is called at the end of the using statement block. The C # compiler automatically adds a try/finally block to the using statement at compile time. All using objects should be initialized after the using statement to ensure that all objects can be dispose.

Memory Management and garbage collection

Q: brief description. The characteristics and differences of stacks and heaps in net

. NET program allocates stacks, managed heaps, and unmanaged heaps in process memory. References to all value-type objects and reference-type objects are allocated on the stack, and the stacks are allocated and deallocated according to the lifetime of the object, and the stack allocates memory based on a pointer to the tail of the stack, which is more efficient.

. NET All reference type objects are allocated on the managed heap, the managed heap allocates memory continuously, and is managed by the. NET garbage collection mechanism, which is less efficient relative to the stack. Unmanaged heap is not affected. NET garbage collection mechanism management, memory blocks are manually applied and released by programmers.

Q: . The operation mechanism of GC in net

Garbage collection refers to collecting the freed object memory that is no longer being used on the managed heap. The basic process consists of finding objects that are no longer being used by the algorithm, moving objects so that all the objects that are still being used are on the side of the managed heap, and adjusting each state variable. The running cost of garbage collection is higher, which has great effect on performance.

Q: When the Dispose method and finalize method are called

The Dispose method is actively invoked by the user, and the Finalize method is used by a dedicated after the object is reclaimed by the first round of garbage collection. NET thread to make the call. The Dispose method is not guaranteed to be executed, and the. NET garbage collection mechanism guarantees that the Finalize method of the type object that owns the Finalize method and needs to be called is executed. Calling the Finalize method involves a complex set of operations with high performance costs that programmers can use to notify. NET that the Finalize method of the object does not need to be called by the Gc.supressfinalize method.

The following is a code template that correctly uses Dispose and finalize:

 Public classfinalizedisposebase:idisposable {Private BOOL_disposed =false; ~finalizedisposebase () {Dispose (false); }        //this implements the Dispose method in the IDispose         Public voidDispose () {Dispose (true); //tells the GC that the Finalize method of this object no longer needs to callGc. SuppressFinalize (true); }        //to do the actual destruction work here//declaration as a virtual method for subclasses to override if necessary        protected Virtual voidDispose (BOOLisdisposing) {            if(_disposed) {return; }            if(isdisposing) {//release the managed resources here//executes only when the user calls the Dispose method            }            //release the unmanaged resources here//tagged object has been disposed_disposed =true; }    }
     Public Sealed classFinalizedispose:finalizedisposebase {Private BOOL_mydisposed =false; protected Override voidDispose (BOOLisdisposing) {            if(_mydisposed) {return; }            if(isdisposing) {//release the resources that are managed and declared in this type here            }            //release the resources that are unmanaged and declared in this type here//call the Dispose method of the parent class to free the resources in the parent class            Base.            Dispose (isdisposing); //to set the markup for a child class_mydisposed =true; }    }

Q: What is the generation in GC, divided into generations

3 Generations: 0, 1, 2 generations. The smaller generations have more opportunities to be released, and the instances of objects that are still alive in the GC are moved to the next generation.

Q: How to tell if an object is still being used in the GC mechanism

The GC iterates through all referenced object instances by using the root reference, which is considered no longer used when an object cannot be traversed.

Q:. NET, memory leaks may occur in the managed heap of

A memory leak is a memory that is created in memory space that is no longer actually used but cannot be allocated. A memory leak will cause the host's memory to gradually decrease with the program running.

. NET may have serious memory leaks in the managed heap, mainly due to the frequent allocation and release of large objects, inappropriate retention of root references, and incorrect finalize methods.

Object-oriented implementation

Q: Briefly describe the concepts of overriding, overloading, and hiding in C #

Overrides are the use of the override keyword to re-implement a virtual method in a base class, in which the method of the real object type is called, regardless of the type of reference. Hiding means re-implementing a method in a base class with the New keyword. Overloading means that multiple methods share the same method name but have different parameters (note that overloading is not related to the return type of a method, that is, the return type cannot differentiate whether the function is overloaded).

Q: Why calling virtual methods in a constructor method can cause problems

Calling a virtual method in a construction method will result in a run-time error. This is because the constructor method of the subclass is not called when the constructor method of the base class is called, but depending on the type of the actual object, the virtual method called by the base class is the method defined in the subclass, which means that a method that has not completed the constructed type object is called, and an unpredictable error can occur.

Handling of exceptions

Q: How to capture for different exceptions

A try block in C # can have multiple catch blocks, and each catch can be specially handled for special exceptions. For security reasons, you should finally add a catch block that captures the exception of the exception type to ensure that no exception is thrown without processing.

Q: How to use the conditional feature

The conditional attribute is used to write a method that runs in a particular compiled version, and it usually writes some methods that support testing in the debug version. When the version does not match, the compiler will empty the method contents of the Conditional attribute.

Q: How to avoid exceptions for type conversions

Using the IS and as statements instead of direct use casts can effectively avoid invalidcastexception anomalies and perform relatively high efficiency.

Excerpt from-it Enterprise must read 200. NET face Questions-03. NET Type Syntax basics

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.