C # Partial partial classes and partial methods

Source: Internet
Author: User

First, partial

It is a keyword modifier. You can split the definition of a class or struct, interface, or method into two or more source files. Each source file contains a part of a type or method definition, and all parts are combined when the application is compiled. Modifiers are not available in delegate or enumeration declarations.

Second, the Division class

You need to split the classification definition in the following situations:

    • When working with large projects, having a class spread across separate files allows multiple programmers to process the class at the same time.

    • When you use an automatically generated source, you can add code to the class without recreating the source files. Visual Studio uses this method when creating Windows forms, Web service wrapper code, and so on. You can create code that uses these classes without modifying the files created by Visual Studio.

Simple example

 Public Partial classTest { Public intId {Get;Set; }  Public stringName {Get;Set; }  Public voidShowage () {Console.WriteLine ($"My Age was {this. AGE}"); }    }     Public Partial classTest { Public stringAge {Get;Set; }  Public voidShownewage () {changeage (); Console.WriteLine ($"My New Age was {this. AGE}"); }        Private voidChangeage () { This. Age =" -"; }    }   classProgram {Static voidMain (string[] args) {            varTest =NewTest () {Id =1, Name ="1", age =" One" }; Test.            Showage (); Test.            Shownewage (); varInfo = $"Name:{test. Name},age:{test. AGE}";        Console.WriteLine (info); }    }

  The properties of the partial type definition are merged at compile time, and the following are merged from all partial type definitions: XML annotations, interfaces, generic type parameter properties, class attributes, members

  Public classtestbase {} Public InterfaceITest {voidShowage (); } [SerializableAttribute] Public Partial classTest:testbase { Public intId {Get;Set; }  Public stringName {Get;Set; }  Public voidShowage () {Console.WriteLine ($"My Age was {this. AGE}"); }} [ObsoleteAttribute] Public Partial classTest:itest { Public stringAge {Get;Set; }  Public voidShownewage () {changeage (); Console.WriteLine ($"My New Age was {this. AGE}"); }        Private voidChangeage () { This. Age =" -"; }    }

They are equivalent to:

[SerializableAttribute]    [ObsoleteAttribute]      Public class test:testbase,itest    {        //...    }

If any part is declared abstract, the entire type is considered abstract. If any part is declared as sealed, the entire type is considered sealed.

//Abstract class Example     Public Partial classPTest {} Public Abstract Partial  classPTest {}classProgram {Static voidMain (string[] args) {            //PTest PTest = new PTest ();//Tip: You cannot create an instance of an abstract class or interface "PTest"        }    }    //examples of sealed classes     Public Partial classstest {} Public Sealed Partial classstest {}//Public class Subtest:stest {}

Third, the Division method

  Partial classes or structs can contain partial methods. A part of the class contains the signature of the method. A partial method declaration consists of two parts: definition and implementation.

But it has a lot of limitations and can only be used in some scenarios.

    • A partial method declaration must begin with the context keyword partial, and the method must return void.

    • A partial method can have a ref parameter, but cannot have an out parameter.

    • The partial method is an implicit private method and therefore cannot be a virtual method.

    • A partial method cannot be an extern method, because the presence of the principal determines whether the method is defined or implemented.

    • Partial methods can have static and unsafe modifiers.

    • The partial method can be generic. Constraints are placed on the definition of partial method declarations, but you can also choose to repeat them on the implementation declaration. parameter and type parameter names do not have to be the same in implementation declarations and definition declarations.

    • You can generate a delegate for a partial method that you have defined and implemented, but you cannot generate a delegate for a partial method that has already been defined but not implemented.

Reference:

Https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods

Http://www.cnblogs.com/OpenCoder/archive/2009/10/27/1590328.html

C # Partial partial classes and partial methods

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.