Quantcast
Channel: Dotnet Aid » C#
Viewing all articles
Browse latest Browse all 6

What are the Partial Classes in C#

$
0
0

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


Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles





Latest Images