The basic idea of a partial class is very easy to understand. You keep the definition of a class split into pieces. During the compilation of the code, the compiler will finds all of the pieces of the class and lump them together. This task accomplished by the compiler removes that separation, so functionally there is no difference between a class made of partial classes and a whole class.
This is great because it means that we get the benefits of being able to develop applications using partial classes without fear of it having any affect on the functionality of the code we write.
A partial class is a class whose definition is present in two or more files. Each source file has a section of the class, and all parts are combined when the application is compiled. To split a class definition partial keyword is used as shown in the example below. person class is split into 2 parts. The first part defines the Working() method and the second part defines the Idle() method. When we compile this program both the parts will be combined and compiled. Note that both the parts uses partial keyword and public access modifier.
using System; namespace PartialClass { public partial class Person { public void Working() { Console.WriteLine("I am Working"); } } public partial class Person { public void Idle() { Console.WriteLine("I am Idle"); } } public class Demo { public static void Main() { personObject = new Person(); PersonObject.Study(); PersontObject.Play(); } } }
The advantage of using partial classes is as follows. When working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time. When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when it creates Windows Forms, Web service wrapper code, and so on. You can create code that uses these classes without having to modify the file created by Visual Studio. One of the other important .Net interview question asked again and again is what is Regex , below is a simple video for the same.
Please click here to see more C# interview questions