Partial (Partial method, Partial method), virtual (virtual method), abstract (abstract method), partialabstract

Source: Internet
Author: User

Partial (Partial method, Partial method), virtual (virtual method), abstract (abstract method), partialabstract

Part of the Partial method is a part of the method, which is incomplete. During ide compilation, all the methods are loaded together for unified compilation. If the Partial method is not implemented, the compiler will not compile them.

Restrictions on local types

(1) local types are only applicable to classes, interfaces, and structures. Delegation and enumeration are not supported.
(2) Each part of the same type must have a modifier partial.
(3) When using a local type, each part of a type must be in the same namespace.
(4) Each part of a type must be compiled at the same time.

3. Notes for local types

(1) the keyword "partial" is a context keyword. It indicates a keyword only when it is put together with class, struct, and interface. Therefore, the introduction of partial does not affect the variables named partial in the existing code.
(2) Each part of the local type is usually put in several different. cs files separately, but the C # compiler allows us to put them in the same file.

4. Local application features

The features of local types have the "accumulate" effect.
[Attribute1, Attribute2 ("Hello")]
Partial class Class1 {}

[Attribute3, Attribute2 ("Exit")]
Partial class Class1 {}
Equivalent
[Attribute1, Attribute2 ("Hello"), Attribute3, Attribute2 ("Exit")]
Class Class1 {}
Note: The Attribute2 attribute can be used multiple times on the class.

5. Modifier on local type

(1) The access modifiers on each part of a type must be consistent.
(2) If a partial classification uses the abstract modifier, the entire class will be treated as an abstract class.
(3) If a partial classification uses the sealed modifier, the entire class will be regarded as a sealed class.
(4) Each part of a class cannot use conflicting modifiers. For example, abstract cannot be used in one part, and sealed can be used in another part.
(5) If a partial classification uses a static modifier, the entire class will be treated as a static class. Virtual MethodFirst, the virtual method delays loading. Note that 1. The virtual member implementation can be changed by the override member in the derived class.

When a virtual method is called, The system checks the runtime type of the object for the override member. This override member in most Derived classes will be called. If no derived class is used to override this member, it may be the original member.

By default, the method is non-virtual. You cannot override non-virtual methods.

VirtualThe modifier cannot matchStatic,Abstract, privateOrOverrideModifier.

Except for the differences in the declaration and call syntax, the behavior of virtual attributes is the same as that of abstract methods.

  • Use static attributesVirtualThe modifier is incorrect.

  • UseOverrideModifier attribute declaration. You can override the virtual inheritance attribute in a derived class.

When should I use Virtual and abstract? Abstract indicates a class or method that can be expanded but cannot be materialized and must not be implemented. When declaring an abstract, there is no specific implementation in the abstract idea, and the virtual method can have the original implementation,
Abstract methods must be rewritten in a derived class, but virtual methods do not,
Abstract METHODS can only be declared in abstract classes, while virtual methods are not
Abstract METHODS cannot declare method entities,
Abstract public void AAA ();
You can use the virtual method.
Public virtual void BBB ()
{
Console. WriteLine ("");
}
Virtual methods can realize polymorphism, but abstract methods cannot...

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.