Partial

You can have partial classes and structures.


Partial Classes

Added in .NET 2.0
A partial class allows you to split a class into separate source files
Prior to C# 2.0 you had to define each class entirely in a single file
Automatically generated code can now live in a different file which prevents this code from being accidentally modified


This allows you to split the definition across two or more files
There are a number of situations where splitting a class definition makes sense:
1 - when working on large projects this allows multiple developers to create a single class
2 - automatically generated source code can be added without recreating the source file. This is used for windows forms, web service wrappers etc


To split a class use the partial keyword modifier
All the parts must have the same accessibility (public or private)



For example the hidden classes behind Document VSTO solutions use partial classes as a way of seperating the auto-generated code from the code you write.
Partial classes are also used in Windows forms to separate the auto-generated code from the code you write.


The Partial Keyword is not needed for the main class definition, it is needed only for additional class definitions that share the same class name.
When you compile the code, the code is automatically merged together.


You can also use partial classes to divide a programming task between 2 developers.


You can also define partial interfaces and structures.


Partial Structures



Partial Methods

Added in .NET 3.5
Partial methods can only be used in conjunction with partial classes.
A partial method has its signature defined in one part of a partial class and its implementation defined in another part of the partial class.
If an implementation is not provided then the program will still compile and the redundant signature is automatically ignored.


A partial method is a method whose signature is declared in one class but its implementation is in a different class.
There are several restrictions though:
partial methods must have a return type of void
partial methods cannot accept out parameters
partial methods cannot be extern
partial methods cannot be virtual
partial methods are implicitely private


Applies to classes, structures, interfaces
A partial modifier can be used to split the definition of a class, struct or interface across multiple files.
Each file contains a different component of the definition
Both components are combined when the application is compiled
If any of the components are declared abstract or sealed then the entire type is abstract or sealed respectively


Signatures in both parts must be identical The method must return void The only access modifier that you can use is private.


    partial class A 
    {
      //implicitly private
        partial void DoSomething(string s);
    }

    // This part can be in a separate file.
    partial class A
    {
        partial void DoSomething(String s)
        {
            System.Console.WriteLine("Something happened: {0}", s);
        }
    }



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext