Thursday 21 March 2013

Partial Classes

  • They are introduced in C# 2.0 which allows us to define a class on multiple files ie we can physically split a class and put it into different files.

          parts.cs                                                          parts1.cs
         class parts                                                       partial class part{
        {                                                                      method1()
         method1()                                                       method2()
         method2()                                                       }
         method3()                                                      parts2.cs
        method4()                                                       partial class parts{                                           
        }                                                                    method3()
                                                                              method4()
                                                                              }
  • In the above case, the class which is defined in left hand side is redefined using the approach of partial classes wherein in this approach the class is physically separated parts logically it is one single unit ie in both the two cases when we create object of class arts we can access all the methods of the class.
  •  if  we want to define partial class in all the files the class name must be same to tell that this is one class only and moreover we need to use a partial modifier on the class in each and every file.
  • In case of partial class if we want to inherit the class from any other class it will be sufficient if we perform inheritance in one file so that it takes to a complete class
The advantages if defining partial classes are:
  • It allow splitting huge volumes of code into separate files so that managing becomes much easier.
  • Multiple programmers can work on the same class at the same time.

No comments:

Post a Comment