c sharp learn the basic concepts 25 questions 11 15

  • 2020-05-05 11:45:17
  • OfStack

11. Can I use abstract functions to override virtual functions in a base class?

A:

Can


The   new   modifier is explicitly declared to indicate that
, the implementation of the function in the base class, is hidden
Or add the   override   modifier to indicate that the abstract overrides
, the implementation of the function in the base class
Example:

        class   BaseClass
        {
                public   virtual   void   F()
                {
                        Console.WriteLine("BaseClass.F");
                }
        }
        abstract   class     DeriveClass1   :   BaseClass
        {
                public   abstract   new   void   F();
        }

Thank you         / / watson   hua (http: / / huazhihao. cnblogs. com /) advice
        // he reminded me that I could also abstract the virtual method
to override the base class in this way         abstract   class   DeriveClass2   :   BaseClass
        {
                public   abstract   override   void   F();
        }

12. Can sealed classes have virtual functions?

A:

Yes, virtual functions in the base class convert implicit functions to non-virtual functions, but the sealed class itself cannot add a new virtual function

Example:

        class   BaseClass
        {
                public   virtual   void   F()
                {
                        Console.WriteLine("BaseClass.F");
                }
        }
        sealed   class   DeriveClass   :   BaseClass
        {
                // the virtual function F in the base class is implicitly converted to the non-virtual function

                // the new virtual function G
cannot be declared in a sealed class                 //public   virtual   void   G()
                //{
                //         Console.WriteLine("DeriveClass.G");
                //}
        }

13. What is a property accessor?

A:

Property accessors (Property   Accessor), including   get   accessors and   set   accessors for the read and write operation
of the field, respectively
The main purpose of its design is to realize the encapsulation idea in object oriented (OO). According to this idea, it is best to set the field to private, and it is better for an elegant class not to set the field to public directly and provide the client caller with direct access to

Also note that the attribute itself is not necessarily associated with the field



14. Can abstract   be used with   virtual  ? Can it be used with   override  ?

A:

abstract   modifiers cannot be used with   static, virtual   modifiers

The abstract   modifier can be used with   override  , see 11

Example:


using   System;
using   System.Collections.Generic;
using   System.Text;

namespace   Example14
{
        class   BaseClass
        {
                public   virtual   void   F()
                {
                        Console.WriteLine("BaseClass.F");
                }
        }
        abstract   class   DeriveClass1   :   BaseClass
        {
            // here,   abstract is
which can be used with override                 public   abstract   override   void   F();
        }
        class   Program
        {
                static   void   Main(string[]   args)
                {
                }
        }
}

15. What members can an interface contain?

A:

Interfaces can contain properties, methods, index indicators, and events, but they cannot contain constants, fields, operators, constructors, and destructors, and they cannot contain any static member

Related articles: