Modifiers: Virtual, override, new, abstract, sealed, internal

Source: Internet
Author: User
Tags modifiers
  • Internal
    • Declared Class, Class Member, interface or interface member has internal visibility.
    • The internal modifier makes the class, interface, or member visible only in the current package. Code outside the current package cannot access the internal Member. Internal types or members are accessible only in files of the same assembly.
    • Within the global range, the internal modifier is the same as the public modifier.
    • Internal modifiers cannot be combined with any other visibility modifiers (public, private, or protected. The scope of the visibility modifier relative to their definition.
  • Sealed
    • When the sealed modifier is applied to a class, this modifier prevents other classes from inheriting from the class.
    • Use the sealed modifier to override virtual methods or virtual attributes in the base class. This allows you to allow classes to inherit from your classes and prevent them from overwriting specific virtual methods or virtual attributes.
    • When applied to methods or attributes, the sealed modifier must always be used with override.
  • Abstract
    • Abstract modifier indicates that the modified content is missing or not fully implemented. Abstract modifiers can be used for classes, methods, attributes, indexers, and events. Use the abstract modifier in the class declaration to indicate that a class can only be the base class of other classes. Members marked as abstract or included in an abstract class must be implemented through classes derived from the abstract class.
    • Abstract classes cannot be instantiated.
    • Abstract classes can contain abstract methods and abstract accessors.
    • You cannot use the sealed (C # reference) modifier to modify the abstract class, because the two modifiers have the opposite meanings. Classes using the sealed modifier cannot be inherited, while abstract modifiers require class inheritance.
    • A non-abstract class derived from an abstract class must include all the inherited abstract methods and the actual implementation of the abstract accessors.
    • The abstract method is an implicit virtual method ).
    • Only abstract methods can be declared in abstract classes.
    • It is incorrect to use the static or virtual modifier in the abstract method declaration.
    • Abstract classes that implement interfaces can map interface methods to abstract methods.
  • New
    • When used as a declaration modifier, The New Keyword can explicitly hide members inherited from the base class. When the inherited member is hidden, the derived version of the member replaces the base class version. Although you can hide members without using the new modifier, you will receive a compiler warning. This warning is not allowed if new is used to explicitly hide a member.
    • It is incorrect to use both new and override for the same member because the meanings of the two modifiers are mutually exclusive. The new modifier creates a new member with the same name and hides the original member. The override modifier extends the implementation of inherited members.
  • Override
    • Override modifiers must be used to extend or modify the abstract or virtual Implementation of inherited methods, attributes, indexers, or events.
    • The override method provides a new implementation of Members inherited from the base class. The override base method must have the same signature as the override method.
    • You cannot override non-virtual or static methods. The override base method must be virtual, abstract, or override.
    • Override declares that the accessibility of the virtual method cannot be changed. The override and virtual methods must have the same access level modifier.
    • You cannot use the new, static, or virtual modifier to modify the override method.
    • When calling a method in an instance of derived, the C # compiler will first try to make the call compatible with the method version originally declared on derived. The override method is not declared on the class, but a new implementation of the method declared on the base class. Only when the C # compiler cannot match a method call with the original method on derived can it try to match the call with an override method with the same name and compatible parameters.
  • Virtual
    • Virtual keywords are used to modify methods, attributes, indexers, or event declarations, and enable them to be overwritten in a derived class.
    • By default, the method is non-virtual. You cannot override non-virtual methods.
    • Virtual modifiers cannot be used with static, abstract, private, or override modifiers.
Examples:
public abstract class A   {      public abstract void Do();      public virtual void DrawLine() { Console.WriteLine("A:DrawLine"); }      public virtual void DrawPoint() { Console.WriteLine("A:DrawPoint"); }      public virtual void DoWork() { Console.WriteLine("A:DoWork"); }   }   public class B : A   {      public override void Do() { Console.WriteLine("B:DO"); }      public new void DrawLine() { Console.WriteLine("B:DrawLine"); }      public override void DrawPoint() { Console.WriteLine("B:DrawPoint"); }      public override void DoWork() { Console.WriteLine("B:DoWork"); }   }   public class C : B   {      public sealed override void DoWork() { Console.WriteLine("C:DoWork"); }   }   public class D : C   {      // Attempting to override DoWork causes compiler error CS0239.      //public override void DoWork() {}      public new void DoWork() { Console.WriteLine("D:DoWork"); }   }   // Only accessible within the same assembly   internal class MyInternalClass   {      public string Value { get; set; }   }   //public sealed class MySealedClass { }   //public class Derived : MySealedClass { } // error CS0509: '': cannot derive from sealed type   interface I   {      void M();   }   abstract class AC : I   {      public abstract void M();   }   public class Base   {      public virtual void DoWork(int param) { Console.WriteLine("Base:DoWork(int)"); }   }   public class Derived : Base   {      public override void DoWork(int param) { Console.WriteLine("Derived:DoWork(int)"); }      public void DoWork(double param) { Console.WriteLine("Derived:DoWork(double)"); }   }   A a = new B();   a.DrawLine(); // output A:DrawLine   a.DrawPoint(); // output B:DrawPoint   a.Do(); // output B:DO   D d = new D();   d.DoWork(); // output D:DoWork   C c = (C)d;   c.DoWork(); // output C:DoWork   B b = (B)d;   b.DoWork(); // output C:DoWork   int val = 5;   Derived der = new Derived();   der.DoWork(val);  // Calls DoWork(double).   ((Base)der).DoWork(val);  // Calls DoWork(int) on Derived.


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.