25 basic C # concepts you must know

Source: Internet
Author: User
Tags modifiers

1. What are the differences between static and non-static variables?
Static variables: static variables are declared using the static modifier. They are created when the class is loaded and accessed through the class.
The same static variable of all instances is the same value.
Non-static variables: variables declared without the static modifier are called non-static variables. They are created when the class is instantiated and accessed through objects.
Q: The same non-static variable of different instances of the same class can be different values.

2. What is the difference between const and static readonly?
Const: The member declared with the const modifier is called a constant, Which is initialized during compilation and embedded into the client program.
Static readonly: The member declared with the static readonly modifier is still a variable, but it is similar to a constant.
Usage: it can be accessed through a class and cannot be modified after initialization. But unlike constants, these variables are initialized at runtime.

3. What does extern mean?
Extern: modifier is used to declare member functions implemented outside the Assembly and is often used to call system API functions.
Note: The static modifier must be added when used together with dllimport. It can also be used for components of different versions of the same assembly.
Call (use extern to declare the alias). It cannot be used with the abstract modifier at the same time.

4. What does abstract mean?
Abstract modifiers can be used for classes, methods, attributes, events, and index indicators (Indexer) to represent abstract members.
Abstract cannot be used with static, virtual, or override.
Declarations as abstract members can not include the implementation code, but the class cannot be instantiated only if there are unimplemented abstract members in the class,
It is usually used to force the inheritance class to implement a certain member.

5. What is the role of the Internal modifier?
The internal modifier can be used for types or members. The types or members declared using this modifier can only be accessed within the same set,
The interface member cannot use the internal modifier.

6. What is the sealed modifier?
The sealed modifier indicates a seal. When used for a class, it indicates that the class cannot be inherited and cannot be used together with abstract,
Because these modifiers are mutually exclusive in meaning, when used for methods and attributes, it indicates that the method or attribute cannot be inherited,
Must be used with the override keyword, because the method or attribute using the sealed modifier must be the corresponding virtual member in the base class,
It is usually used to implement third-party class libraries that do not want to be inherited by the client, or for classes that do not need to be inherited to prevent misuse of inheritance.
The hierarchy is chaotic. Proper use of the sealed modifier can also improve the running efficiency.

7. What is the difference between override and overload?
Override indicates rewriting, which is used to inherit the Implementation of Virtual members in the base class.
Overload indicates overload, which is used to implement different parameters (including different types or numbers) of methods with the same name in the same class.

8. What is an index indicator?
The class that implements index indicator (Indexer) can use the object after its instance like an array, but what is different from the array is the index indicator's
The parameter type is not limited to int. Simply put, it is essentially a property containing parameters.

9. What is the role of the new modifier?
The new modifier and the new operator are two concepts.
The new modifier is used to declare a class or class member, indicating that the member with the same name in the base class is hidden. The new operator is used to instantiate a type.
The new modifier can only be used to inherit classes. It is generally used to make up for the shortcomings of the base class design.
The new modifier and override modifier cannot be used on one member at the same time, because the two modifiers are mutually exclusive in meaning.

10. What is the meaning of this keyword?
This is a reserved word and is only used by constructors and method members.
The constructor of the class indicates the reference to the object itself being constructed,
In the method of the class, it indicates a reference to the object that calls the method.
In the structure constructor, the function indicates a reference to the structure being constructed,
A reference to the result of calling the method appears in the structure method.
This reserved word cannot be used in the implementation of static members, because the object or structure is not instantiated.
In the C # system, this is actually a constant, so this ++ cannot be used.
This reserved word is generally used to restrict hidden members with the same name, use the object as a parameter, declare the index accessor, and determine whether the object of the input parameter is itself.

11. Can I use Abstract Functions to override virtual functions in the base class?
Yes, but the new modifier must be used for explicit declaration to hide the implementation of the function in the base class.

12. Can there be virtual functions in the seal class?
Yes. The virtual functions in the base class are implicitly converted to non-virtual functions, but the new virtual functions cannot be added to the sealed class itself.

13. If there is only one attribute accessor for the virtual attribute in the base class, how many attribute accessors can be created after the inherited class overrides this attribute? What if the base class contains get and set?
If there is only one property accessor for the virtual attribute in the base class, the inherited class should have only one property after rewriting this property.
If the base class has two Property accessors: Get and set, only one or both of the inherited classes can have two Property accessors.

14. can abstract be used with virtual? Can it be used with override?
Abstract modifiers cannot be used with static, virtual, and override modifiers.

15. What members can an interface contain?
An interface can contain attributes, methods, index indicators, and events, but cannot contain constants, fields, operators, constructors, and destructor, and cannot contain any static members.

16. What is the difference between classes and structures?
Class: the class is allocated by the reference type on the stack. The class instance only copies the reference and points to the memory allocated by the same actual object,
Classes include constructor and destructor. classes can be inherited or inherited.
Structure: The value type is allocated on the stack (although the access speed of the stack is faster than that of the stack, the stack resources are limited ),
The Structure assignment will generate a new object. The structure has no constructor, but can be added. No destructor for the structure,
The structure cannot inherit from another structure or be inherited, but it can inherit from interfaces like a class.

17. What problems does interface multi-inheritance bring about?
The interfaces in C # are different from the classes. You can use multi-inheritance, that is, a sub-interface can have multiple parent interfaces.
However, if the two parent members have members with the same name, this creates ambiguity (this is one of the reasons why the class in C # cancels multi-inheritance ), in this case, it is best to use an explicit statement for implementation.

18. What is the difference between an abstract class and an interface?
Abstract class can contain function definitions and implementations,
An interface can only contain function definitions. abstract classes are abstract concepts from a series of related objects. Therefore, they reflect things.
Internal commonalities. interfaces are a functional Convention defined to satisfy external calls. Therefore, they reflect the external characteristics of things and analyze objects,
Extract internal commonalities to form abstract classes, which are used to represent the object nature, that is, "What". interfaces are preferred for external calls or functions to be expanded.

19. What is an alias indicator?
You can use alias indicators to create an alias for a certain type, which is mainly used to solve conflicts or
Avoid redundant namespaces. Alias indicators only work in one unit file.

20. How to release unmanaged resources?
The. NET platform provides GC (garbage collection) for Memory Management, which is responsible for automatically releasing managed resources and collecting memory.
Work, but it cannot release the unmanaged resources. In this case, we must provide our own methods to release the unmanaged resources allocated in the object,
For example, if you use a COM object in the object implementation code, you can implement
Protected void finalize () (The Destructor will become this stuff during compilation) to release unmanaged resources,
When the GC releases an object, it checks whether the object implements the finalize () method. If yes, it is called.
There is a better way, that is, to explicitly provide an interface to the client call end to manually release objects,
Instead of waiting for GC to release our objects (not to mention the low efficiency ),
There is an idisposable interface in the system namespace. It is very suitable to do this, so we need to declare another interface on our own.
In addition, this implementation does not need to be used only after hosting resources are used. If the class you design is larger during running
Instance (like geometry in GIS). To optimize program performance, you can also implement this interface so that the client can confirm
You do not need to manually release these objects.

21. P/invoke?
A transaction is generated when the controlled code interacts with the uncontrolled code ),
This usually occurs when platform invocation services are used, that is, P/invoke,
For example, to call system APIs or deal with COM objects, use the system. runtime. interopservices namespace,
Although InterOP is very convenient, it is estimated that 10 to 40 commands will be executed every time a transaction is called, resulting in a lot of overhead,
Therefore, we should try to call as few transactions as possible. If the transaction is not available, we recommend that you execute multiple actions in one call,
Instead of multiple calls, you can only execute a small number of actions at a time.

22. What is the difference between stringbuilder and string?
Although string is a reference type, a new object is generated during the value assignment operation, while stringbuilder does not,
Therefore, it is best to use stringbuilder when splicing a large number of strings or frequently performing operations on a string. Do not use string.

23. What is the meaning of explicit and implicit?
Explicit and implicit are conversion operators. Using these two operators allows us to exchange custom types,
Explicti indicates Explicit conversions. For example, forced conversions (B = (B) a) must be performed from A to B ),
Implicit indicates implicit conversion. For example, from B-> A, you only need to assign values directly (A = B ),
Implicit conversions make our code look more beautiful and more concise and easy to understand, so it is best to use the implicit operator more.
However! If the object itself loses some information (such as precision) during conversion, we can only use the explicit IT operator so that the client can be warned during compilation.

24. What is the use of Params?
The Params keyword is used in the parameter list of a method member to provide a variable number of parameters for this method,
It can appear only once and cannot be followed by parameter definitions.

25. What is reflection?
Reflection, reflection, through which we can get various information at runtime,
Such as assembly, module, type, field, attribute, method, and event,
After dynamic instantiation of types, you can also perform operations on them, which are generally used to implement plug-in framework programs and design patterns,
Of course, reflection is a means to make full use of its energy to accomplish anything you want to do.

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.