IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
C#
Search C# Group:

Advanced
-Collapse +Expand C# To/From
To/FromCODEGuides
-Collapse +Expand C# Study Test
PRESTWOODCERTIFIED
-Collapse +Expand C# Store
PRESTWOODSTORE

Prestwood eMagazine

October Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingC#OOP   Print This     
  From the March 2016 Issue of Prestwood eMag
 
C# OOP:
C# Partial Classes (partial)
 
Posted 15 years ago on 1/14/2009 and updated 2/27/2009
C# Code Snippet:
 A flashcard from our C# Flashcards Library
 A code snippet from our C# Code Snippets Page

KB101807

General Info: Partial Class

A partial class, or partial type, is a class that can be split into two or more source code files and/or two or more locations within the same source file. Each partial class is known as a class part or just a part. Logically, partial classes do not make any difference to the compiler. The compiler puts the class together at compile time and treats the final class or type as a single entity exactly the same as if all the source code was in a single location.

Languages Focus: Partial Class

For languages that have implemented partial classes, you need to know usage details and restrictions. Can you split a class into two or more files? Can you split a class within a source code file into two or more locations? What are the details of inheritance? Does it apply to interfaces as well?

C# Partial Classes

C# uses the keyword partial to specify a partial class. All parts must be in the same namespace.

Syntax Example:
class partial Cyborg: System.Object
{
}

Partial classes and C#

Microsoft added the concept of partial classes to C# 2.0 (along with the arrival of the .Net Framework 2). With the .Net implementation, you can split a class into multiple source files, and/or multiple locations in the same source file so long as they are all in the same namespace. All parts must use the same base class so it's typical to indicate the base class in only the first part. All other parts don't need to specify the base class. All the parts included at compile time are compiled. This presents some interesting possibilities.

.Net Features

  • Parts can specify different base interfaces, and the final type implements all of the interfaces listed by all of the partial declarations.
  • Any class, struct, or interface members declared in a partial definition are available to all of the other parts.

.Net Limitations

  • All parts must be defined within the same namespace.
  • All the parts must use the partial keyword.
  • All of the parts must be available at compile time to form the final type.
  • All the parts must have the same accessibility (i.e. public, private, etc.).
  • If any of the parts are declared abstract, then the entire type is abstract.
  • If any of the parts are declared sealed, then the entire type is sealed.
  • If any of the parts declare a base type, then all parts use that same base type. All parts that specify a base class must specify the same base class. You can omit a base class in one or more of the parts in which case the part still uses the same base class.

Visual C# 2008 Working Winforms Example

The following simple example demonstrates partial classes.

Create a form and place a button on it and add code as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
  
namespace Partial
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
  
    private void button1_Click(object sender, EventArgs e)
    {
      Cyborg MyRobot = new Cyborg();
     
      //Optionally set CyborgName property:
      //MyRobot.CyborgName = "Cameron";
  
      MyRobot.IntroduceYourself();
    }
  }
  
  //
  //One use of partial classes is to organize
  //large classes into logical groups. For example,
  //you can use partial classes to separate your
  //class properties and methods. Although these
  //are in the same file, you could put them in
  //separate source files.
  //
  //Cyborg Properties...
  public partial class Cyborg : System.Object
  {
    public string CyborgName { get; set; }   
  }
  
  //Cyborg Methods...
  public partial class Cyborg : System.Object
  {
    public virtual void IntroduceYourself()
    {
      if (CyborgName == null)
        MessageBox.Show("Hi, I do not have a name yet.");
      else
        MessageBox.Show("Hi, my name " + CyborgName + ".");
    }
  }

}
 

 

 

More Info

Definition:  Partial Class

Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1116A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile


Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Advanced

3 Advanced Level Questions

Question #1: Multiple Choice

You can put parts of a partial class where?

Answer:
1. 

In the same source file, namespace does not apply.

2. 

Anywhere within a namespace even in separate source files.

3. 

In the same source file, but all parts must be in the same namespace.

4. 

Each part must be in a separate namespace and in a separate source file.

Question #2: Yes or No?

Do all parts of a partial class have to be in the same source file?

Answer:
  • Yes
  • No
  • Question #3: True or False?

    All parts of a partial class must be in the same namespace.

    Answer:
  • True
  • False

  •  KB Article #101807 Counter
    30389
    Since 1/14/2009
    Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
    www.prestwood.com


    ©1995-2024 Prestwood IT Solutions.   [Security & Privacy]