Partial (partial method, local method), virtual (virtual method), abstract (abstraction method)

Source: Internet
Author: User

Partial method Gu Mingsi is part of the method, incomplete, when the IDE compiles, all parts of the method will be loaded into a 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, structs, and delegates and enumerations 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. Local type of attention point

(1) The keyword partial is a contextual keyword that only has the meaning of the keyword when it is put together with class, struct, interface. Therefore, the introduction of partial does not affect variables with the name partial in the existing code.
(2) Parts of a local type are generally separated into several different. cs files, but the C # compiler allows us to place them in the same file.

4. Application characteristics of local types

The attribute on the local type has an "additive" 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 property allows multiple use on a class.

5. Modifiers on a local type

(1) Access modifiers on the various parts of a type must maintain consistency.
(2) If a partial class uses the abstract modifier, the entire class is treated as an abstract class.
(3) If a partial class uses the sealed modifier, the entire class is treated as a sealed class.
(4) Each part of a class cannot use conflicting modifiers, such as the inability to use abstract on one part, and the use of sealed on another.
(5) If a partial class uses the static modifier, the entire class is treated as a static class Virtual virtual methodThe first virtual method is deferred loading, which requires attention1. Implementation of a virtual member can be changed by an overriding member in a derived class

When a virtual method is called, the runtime type of the object is checked for the overriding member. The overriding member in most derived classes is called, and if no derived class overrides the member, it may be the original member.

By default, the method is non-virtual. Non-virtual methods cannot be overridden.

The virtual modifier cannot be used with static,abstract, private , or override modifiers.

The behavior of a virtual property is the same as an abstract method, except that the declaration and invocation syntax are different.

    • Using the virtual modifier on a static property is an error.

    • You can override a virtual inheritance property in a derived class by including a property declaration that uses the override modifier.

When should I use virtual to use abstract? Abstract is to identify a class or method that can be extended but not manifested and must not be implemented, declaring an abstract when there is no concrete implementation of the abstraction, the virtual method can have the original implementation,
The abstract method must be overridden in a derived class, and virtual does not have to,
The abstract method cannot declare a method entity,
Abstract public void AAA ();
The virtual method can
public virtual void BBB ()
{
Console.WriteLine ("A");
}
Virtual methods can achieve polymorphism, and abstract methods can not ...

Partial (partial method, local method), virtual (virtual method), abstract (abstraction method)

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.