InterviewSolution
Saved Bookmarks
| 1. |
What are partial classes? |
|
Answer» While programming in C#, we can split the definition of a class over two or more source files. The source files contain a SECTION of the definition of class, and all parts are combined when the APPLICATION is compiled. For splitting a class definition, we need to use the PARTIAL keyword. Example: We have a file with a partial class named as Record namespace HeightWeightInfo { class File1 { } public partial class Record { private int h; private int w; public Record(int h, int w) { this.h = h; this.w = w; } } }Here is another file named as File2.cs with the same partial class Record namespace HeightWeightInfo { class File2 { } public partial class Record { public void PrintRecord() { Console.WriteLine("Height:"+ h); Console.WriteLine("Weight:"+ w); } } }The main method of the project namespace HeightWeightInfo { class Program { static void Main(string[] args) { Record myRecord = new Record(10, 15); myRecord.PrintRecord(); Console.ReadLine(); } } } |
|