Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of a partial class?

Tags:

c#

.net

asp.net

How can a partial class be useful in coding? Can anyone explain in detail with examples?

like image 327
Sai Sankar Avatar asked Jul 19 '11 12:07

Sai Sankar


2 Answers

The main use is to separate designer-generated code (e.g. a UI, or entities) from hand-written code; the two are mashed together at compile time, so you still just get a single class, but you don't see the designer cruft when looking at code.

It's not the only useful situation, but it's the main one I've come across.

like image 50
Jon Skeet Avatar answered Oct 06 '22 04:10

Jon Skeet


  1. When working on large projects, spreading a class over separate files allows multiple programmers to work on it simultaneously.
  2. 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 creating Windows Forms, Web Service wrapper code, and so on. You can create code that uses these classes without having to edit the file created by Visual Studio.

Source msdn.

Have a look at this link.

like image 36
FIre Panda Avatar answered Oct 06 '22 03:10

FIre Panda