C # basic Q & A for a programmer interview
If your resume says "familiar with/understand C #", you should be able to answer the following basic questions. I will give my own brief answers for your reference. Welcome to the discussion.
· What is the name of the implicit parameter for the set method that passes in an attribute?Value. Its type is the same as that of its attribute.
· How to Implement inheritance in C?Add a colon after the class name and the name of the base class.
· C # does it support multiple inheritance?Not supported between classes, but between interfaces. Class is called an implementation interface, not an inheritance interface.
· Where can the attributes/Methods Modified by protected be accessed?It can be accessed in the subclass that inherits or indirectly inherits from this class.
· Will private members be inherited?Yes, but cannot be accessed. So it seems that they cannot be inherited, but they are actually inherited.
· Describe the modifier protected internal.In the same assembly, his access level is the same as that in public, while in cross-assembly access, his access level is the same as protected. That is, the range of protected + the range of internal.
· C # provides a default non-parameter constructor. When I implement another constructor with another parameter, I want to retain this non-parameter constructor. How many constructors should I write?Two. Once you implement a constructor, C # will no longer provide the default constructor. Therefore, you need to manually implement the non-parameter constructor.
· C # What is the common base class for all objects?System. object.
· What is the difference between heavy load and overwriting?Overload provides the implementation of calling different parameters for a method signature. Overwriting provides the implementation of modifying the behavior of the parent class method in the subclass.
· What does virtual mean in method definition?The virtual method can be overwritten by the quilt class.
· Can I override non-static methods into static methods?No. The signature of the overwriting method must be consistent with that of the overwriting method, except for changing virtual to override.
· Can I override private virtual methods?No, or even the sub-classes cannot access the private methods in the parent class.
· Can a class be prevented from being inherited by other classes?Yes. Use the keyword sealed.
· Can a class be inherited, but cannot a method be overwritten?Yes. Mark this class as public and the method as sealed.
· What is abstract class )?A class that cannot be instantiated. Abstract classes generally contain abstract methods. Of course, they can also be implemented. An inherited class can be instantiated only when abstract methods of all abstract classes are implemented.
· When must a class be declared as an abstract class?When this class contains abstract methods, or the class does not fully implement the abstract methods of the parent class.
· What is an interface?Class that only contains a total of abstract methods. These methods must be implemented in the subclass.
· Why cannot I specify the modifier of methods in the interface?The methods in the interface are used to define the contract for communication between objects. It is meaningless to specify the methods in the interface as private or protected. They are public methods by default.
· Can I inherit multiple interfaces?Of course.
· What if there are repeated method names in these interfaces?In this case, you can decide how to implement it. Of course, you must be very careful. However, there is no problem in the compilation process.
· What is the difference between interfaces and abstract classes?All methods in the interface must be abstract and the access modifier of the method cannot be specified. Abstract classes can be implemented by methods, or you can specify the access modifier of methods.
· How to distinguish between overload methods?Different parameter types, different parameter numbers, and different parameter order.
· What is the difference between const and readonly?Const can be used for local constants, while readonly is actually the initonly field of the class. Obviously, it cannot be local.
· What is the difference between system. String and system. stringbuilder?System. String is an unchangeable string. System. stringbuilder stores a variable string and provides some methods to modify the string.