Answers to basic. NET interview knowledge

Source: Internet
Author: User

Drawing from Zhao's http://blog.zhaojie.me/2011/03/my-interview-questions-for-dotnet-programmers.html

What is the difference between class and struct? Do they affect performance ?. What are the classes (structures) in the net bcl, and why are they not structures (classes )? How do you select a class or structure for custom types?

1. Value Type and reference type
Structure is value type: value type is allocated on the stack. All base types are structure types. For example, int corresponds to System. int32 structure. string corresponds to system. string Structure. You can create more value types by using the structure.
Class is a reference type: the reference type is allocated on the stack.
The execution efficiency of stacks is higher than the execution efficiency of stacks. However, the stack resources are limited and it is not suitable for processing large logical and complex objects. Therefore, structure processing is a small object to be treated as a base type, while classes process a certain business logic.
Because the structure is a value type, a value can be assigned between structures to create a new structure, while a class is a reference type. A value assignment between classes is just a copy reference.
Note:
1. Although the structure is different from the class type, their base types are all objects. in c #, all types of base types are objects.
2. although the New operator is used for structure initialization, the structure object is still allocated on the stack instead of the stack. If the new operator is not used, before all fields are initialized, the field remains unassigned and the object is unavailable.
2. Inheritance
Structure: it cannot be inherited from another structure or class. Although the structure is not explicitly declared using sealed, the structure is implicit sealed.
Class: fully scalable. Unless the declared sealed is displayed, the class can inherit other classes and interfaces, and its own can also be inherited.
Note: although the structure cannot be inherited, the structure can inherit interfaces, and methods are the same as class inheritance interfaces.
Structure:
No default constructor exists, but you can add constructor.
No destructor
No abstract and sealed (because it cannot be inherited)
The protected modifier cannot exist.
You do not need to use new for initialization.
It is incorrect to initialize the instance field in the structure.
Class:
Default constructor available
Destructor
Abstract and sealed can be used.
There is a protected Modifier
New must be used for initialization.
3. How to Select a structure or a class
After discussing the similarities and differences between structures and classes, we will discuss how to choose a structure or a class:

1. The stack space is limited. For a large number of logical objects, creating classes is better than creating structures.
2. structure indicates lightweight objects such as dots, rectangles, and colors. For example, if an array containing 1000 vertex objects is declared, additional memory will be allocated to each referenced object. In this case, the structure cost is low.
3. Classes are the best choice for presentation of abstract and multi-level object Layers
4. In most cases, this type is the best choice for structure when it is only some data.

 

What is a heap and a stack when the. NET program is running? Under what circumstances will data be allocated on the heap (stack? Are there any performance differences between them? Can "structure" objects be allocated to the stack? Under what circumstances will happen, do you need to pay attention to it?

During the program running, objects and reference types are all allocated data on the heap, and their reference addresses are all on the stack, stack is an advanced and post-release data structure (the stack is deleted from the top of the stack ). When declaring an object, you will allocate memory on the heap to save it. Each object has two additional members: one is the pointer to the class method table, and the other is the SyncBlockIndex member, CLR uses this field for thread synchronization control. Some bits can also be used as garbage collection labels.
And so on. CLR manages object instances through these two members.

The stack speed is fast, but the storage capacity is small. The stack data is relatively slow, but the storage capacity is large. If the structure itself contains a reference type, its reference type members are allocated to the stack. Therefore, two structure objects are assigned values. If this structure contains referenced object members, both structure objects point to the address of the same referenced object, rather than simply copying values. Modifying referenced members of any structure object affects two structure objects.

If a class contains a structure member, the structure member will be allocated on the heap when the class object is declared. Note that you should remember to initialize the structure when using it!

What is the role of generics? What are its advantages? Does it affect performance? What is its behavior during execution ?. Which generic types are included in net bcl? Examples illustrate the generic types that you define in normal programming.

Generics provide more powerful types of code security, better reuse, efficiency, and clear constraints. The obvious benefit of generics is that they are type-safe and reduce the number of Box-breaking operations. It has no impact on performance, or even an improvement, because it can reduce unnecessary packing and unpacking operations in the code. During the first compilation, T in the generic type will be replaced by the actual type. In this way, type security is considered and the type security can be checked during program compilation. In BCL, List <T> is a generic type.

What is the role of an exception ?. What are common exceptions in net bcl? In code, How do you capture/handle exceptions? In catch (ex), what is the difference between throw and throw ex? How do you design an exception structure? Under what circumstances will you throw an exception?

An exception is prompted when an unknown error occurs in the program, for example, reading a file but the file does not exist.

Generally, try {} catch (Exception ex) {} finally {} is used to capture code blocks that may encounter exceptions. throw will continue to pass the Exception chain to the next layer, throw ex starts to pass exception connection directly here, which is equivalent to an exception in the try {} code block.

Generally, system exceptions are used. Because capturing exceptions consumes performance, they are captured only in areas where exceptions may occur.

What is the difference between List <T> and T []? How do you choose? What is Dictionary <TKey, TValue> ?. Which of the following commonly used containers does net bcl support? How are they implemented (which data structure )? Which scenarios are applicable?

List is a generic List that can be defined as an infinite length. T [] is a generic array. You have to define the length of an array. Generally, List is used. Obviously, it is convenient to use its own methods, and it is infinite length. You can append it as needed. It should be noted that the array itself inherits from the Object, so it is always allocated on the heap. Dictionary is a Dictionary generic class, which corresponds to the original HashTable.

In addition to the two most common List and Dictionary mentioned above, there are also the following containers:

Both Queue and Stack are lists that do not allow random access. Queue is a first-in-first-out Queue, and Stack is the first-out Stack.

SortedList and SortedDictionary both return a list sorted by keys during enumeration. The difference is that the former uses less memory, while the latter adds items faster, however, if the data is sorted and added in sequence, the former is faster.

The random list is a linked list. This set does not have a corresponding non-generic version. A linked list is used to easily insert an element into it. The random access performance is slower than the array, the space is also more occupied. The difference between it and List is that it does not have pre-allocated capacity for expansion, so there is no "waste" space. If you need to be able to quickly insert at the beginning and end or insert in the middle and output in sequence, the linked list is still useful.

What is the difference between an abstract class and an interface? What do you need to pay attention to when using it? How can I choose whether to define a "completely abstract" abstract class or an interface? What is "explicit implementation" of interfaces "? Why is it important?

An abstract class is called an abstract class. If a non-abstract class inherits the abstract class, it must implement all abstract methods of the abstract class. abstract classes cannot create instances, abstract constructor methods and abstract static methods. You can have private methods and variables,

Interfaces include method definitions, attributes, indexes, and so on. classes that inherit interfaces must implement interface methods. Private methods are not allowed. The interface can be inherited more than once. The abstract class can only be inherited separately. The abstract class that is completely abstract is no different from the interface.

Generally, when multiple transactions share the same features, their features are extracted and set to abstract parent classes. In this abstract parent class, some methods are abstracted to implement their own sub-classes. Some methods have method bodies because sub-classes implement the same methods.

In this case, the abstract class is used. Abstract classes are implemented mainly for reuse. Interfaces are a set of contracts with multiple irrelevant objects. But they can define interfaces when there is a unified behavior. For example, birds can have interfaces for flying to heaven, and planes can also have interfaces for flying to heaven.

The interface display implementation is to add the interface name before the signature of the implemented interface method in the inherited interface class. The displayed implementation interface can reduce the number of unboxed operations, such as interface methods (note that the displayed implementation interface method can be called only by the interface instance, the instance of this class will not call this method)

 

Public interface ITest {void showMessage (object s );}

 

Public class TestClass: ITest

{

// Display implementation Interface Method

Void ITest. showMessage (object s)

{

ShowMessage (TestClass) s );}

// Method implemented by the class itself and the interface method has the same name, and provides type security and does not need to be boxed, so when calling the class object instance, we can directly call this method to avoid packing and provide the type security check during compilation.

Void showMessage (TestClass s)

{

ShowMessage (s );

}

}

 

 

Is the string of the reference type or structure type? Is it more special than a common reference type? What should I pay attention to when using strings? Why is StringBuilder more efficient? When connecting multiple strings, is it more efficient than directly adding strings at any time?

 

String is a reference type, but it has been created and is immutable. CLR is closely integrated with string. It knows how the fields in string are laid out, So CLR will directly access these fields. String cannot use new to create an instance. And

Each operation on the string content will generate a new string object and allocate memory on the heap. It is best to use the StringBuilder object to splice strings multiple times! I guess this object maintains an array of char [], and then creates a string in toString () and returns a reference. Therefore, it does not affect the performance by creating too many string objects when splicing characters. Of course, its char [] is overhead! If your string is short and the number of operations on it is small, you can directly use string to directly add it.

 

How to efficiently copy arrays? What is the difference between "two-dimensional array" and "array? How do I select the traversal sequence of the internal and external layers when traversing a two-dimensional array using a double loop?

In general, the Array. Copy method is called to Copy the Array (but this method is a shallow Copy. If the data element is of the reference type, it only copies the address ). An array composed of arrays is called a staggered array. elements that access it must be accessed twice or more times.

What is metaprogramming and what are the methods and scenarios of. NET metaprogramming? What is reflection? Can I introduce some common reflection scenarios? Some people say that the reflection performance is poor. What do you think of this problem? Is there any way to improve the reflection performance?

The main drawback of reflection is that the type security during compilation cannot be provided, which will affect the performance to a certain extent. Because it needs to scan metadata and reflect the call method, the CLR should first package the real parameters used by the method into an array, and then release the data in the internal radiation and put it on the thread stack.

If you want to use a program to dynamically discover the constructor type instance and call its methods and properties:

The type can be derived from a known base type during editing. At runtime, an instance of the derived class is constructed and Its Reference is placed in the variable of this base type, then, call the virtual method of the base type. Of course, this virtual method is overwritten in this type.

You can also use interfaces. We recommend that you use interfaces to improve performance.

  • What is delegation? What is an anonymous method? In C #3.0, what is a Lambda expression? What is the extension method? What is LINQ? What are the important features of C #3.0 and their advantages? Which libraries in BCL are related to these features? What do you usually use most?
  • What technical books, websites, communities and projects do you read outside of work? What other. NET technologies do you still use? Can you compare them with some targeted ones in. NET or. NET?

 

 

 

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.