Jump to content

Partial Classes

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Termana

Termana

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,057 posts
So great, you can make classes and thats all good. But have you ever thought about being
able to put part of the class in one file, and put the other part (or more parts) in
other files? In C#, this is quite possible!

By using the ‘partial’ class modifier, you can define a class across multiple files. The compiled class merges the various partial source files into a single compiled class. So, for instance, if you have the source code

public partial class A

{

    public void method1()

    {...}

}

and somewhere else you have the source code

public partial class A

{

    public void method2()

    {...}

}
then the compiled object will exhibit both method1 and method2. Note that it’s important that the various aspects of the declaration like modifiers, type parameters, etc. all match up across the multiple partial declarations.
Your partial classes have to be within the same namespace, so if you are to be putting them in seperate files, you must have it contained in the same namespace.
You can do this by typing
namespace NameSpaceNameHere

{

//Enter Partial Class Here

}
for instance when you open a new Console Application, the default namespace is normally
ConsoleApplication1 that is if you haven't changed your project name.

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Nice tutorial. +rep!