As we check our coding page in .net application we found that at the top after the namespaces there is a call that is partially defined in every web page. The question arrives in our mind that what is this Partial and why we use the class with partial access specifier. Why we not use the class as publicly or privately? Here we are to answer all these question. First we will get the partial class definition and its need.



A Partial class gives us the ability to split a single class into more then one source code file that is .cs or .vb file. For example: if the class named here “Student” became particularly long and intricate, we might decide to break it into two pieces. Here we are taking an examply of Student class that is stored in two different files.

1. Student class is stored in Student1.cs

Public partial class Student
{
      private string name,
      private string address
      public string Name
      {
         get
         {
          return name;
         }
         set
         {
          name=value;
         }
      public string Address
      {
         get
         {
           return address;
         }
        set
        {
           address=value;
        }
     public Student(string name, string address)
     {
           Name=name;
           Address=address;
     }
}

2. The Student class stored in Student2.cs

public partial class Student
{
   string htmlstring;
   htmlstring="<h1>"+name+"</h1><br/>";
   htmlstring+="<h3>"+address +"</h3><br/>";
   return htmlstring;
}

The partial class behaves the same as normal class. When we compile the application the compiler tracks down each piece of the partial class and assembles it into a complete unit. So the Student class can be used as a complete unit and the similar as other classes.

Why we use the Partial class:

The real purpose of partial classes in .Net is to hide automatically generated designer code by placing it in a separate file from our code. Visual Studio use this technique when we create a web page in web application and a form in window application

admin (155 Posts)


Tagged with →  

One Response to What are the Partial Classes in C#

  1. Jijin Mohan says:

    Got some really good inputs, thanks for the share.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Set your Twitter account name in your settings to use the TwitterBar Section.