"C #" face question Finishing

Source: Internet
Author: User

Does the class in 1.c# support multiple inheritance? Please explain why.
Answer: Not supported, need to use interface to implement multiple inheritance

2. We all know that a class can have multiple constructors, and C # will provide a parameterless constructor by default when we create the class, and when I implement another constructor with parameters, can I invoke a parameterless constructor? Please explain why.
A: No, because once you implement a constructor, C # will no longer provide a default constructor, so you need to manually write a parameterless constructor.


3. Please describe briefly the difference between overloading and rewriting?
A: The method overload provides an implementation of the same method but a different invocation of the method signature parameter.
Overrides provide implementations that change the behavior of a parent class method in a subclass.


4. Can I set the Class A to be inherited, but a method in Class A cannot be overridden?
A: Yes, the modifier of Class A is marked as public, and the method that does not allow overrides in tag Class A is sealed
The sealed keyword can not only restrict classes, but also restrict methods.


What is the difference between 5.const and readonly?
A: The const keyword is used to declare a constant at compile time
ReadOnly constants used to declare the runtime


6. When do you have to declare a class as an abstract class?
A: (1) when there are abstract methods in this class, you must declare the class as abstract class
(2) When the class does not fully implement the abstract method of the parent class, it also needs to be declared as an abstract class



7. What is the difference between an interface and an abstract class?
A: None of the methods in the interface can be implemented, and the modifier of the method cannot be specified
You can have an implementation of a method in an abstract class, or you can specify the access modifier for a method
The first class that inherits an interface must implement all the methods in the interface
The implementation of abstract methods in abstract classes is implemented by the first non-abstract derived class


8. Do private members of a class inherit from the Quilt class? Please explain why.
A: The quilt class inherits, but cannot be accessed. So it seems that it cannot be inherited, indeed it is inherited.


9. Please write out the singleton mode in C #
For:
public Class A single
{
private static Single instance;
Private single () {}
public static single getinstance ()
{
if (instance = = null) {
Instance = new single ();
}
return instance;
}
}


10. Existing an integer number, please write a method to determine whether the integer is 2 of the n-th square
For:
private static bool Getflag (int num)
{
if (Num < 1) return false;
Return (num & num-1) = = 0;
}

11. Append a double-locking single-case mode

usingSystem;usingSystem.Collections.Generic;/// <summary>///For situations where only one instantiation object is guaranteed in multi-threaded situations, such as the bank's operating system/// </summary>namespacedoublelockinstance{//----------------------------------    //Double locking single case     Public Sealed classSingleton {//defines a class object for internal implementation        Private StaticSingleton myinstance; //ReadOnly-This member can only be assigned when class initialization, so-called class initialization is directly initialized in the class//The variable is marked as ReadOnly, and the instance is created the first time a member of the class is referenced        Private Static ReadOnly ObjectLockroot =New Object (); //set the construction method to private so that the class object cannot be instantiated externally        PrivateSingleton () {}//methods for instantiating objects         Public StaticSingleton getinstance () {//external cannot instantiate an object, but can invoke a static method inside a class//the external need to call this method to use the class object, if the object does not exist, create//The reason for using two judgments in this case is that we do not need to lock the instantiated statements every time, only if the object does not exist.            if(MyInstance = =NULL) {                //The purpose of locking is to ensure that the object's uniqueness is guaranteed when multithreading executes the code at the same time//locking causes threads that execute this code to queue for execution at the same time//lock needs to be judged by an already existing object, so you can't use myinstance                Lock(lockroot) {//Another reason for this is that if multithreading is queued by the outer judgment//that will instantiate multiple objects, so there's a need to make a decision to keep the thread safe .                    if(MyInstance = =NULL) {myinstance=NewSingleton (); }                }            }            returnmyinstance; }    }}

"C #" face question Finishing

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.