Search code examples
c#.netpartial-classes

.NET partial class' accessibility over multiple files


If I have the core of a class defined in one file as "public partial" and I wish to create additions to this in another file, what is the difference between defining "public partial" again in my second file or just defining "partial"?

What happens if I define "private partial" in my second file?


Solution

  • You can duplicate the class modifiers or leave them out in one file, but you'll get a compiler error if they're specified in different files as different access levels.

    From The C# Programming Guide:

    The following keywords on a partial-type definition are optional, but if present on one partial-type definition, cannot conflict with the keywords specified on another partial definition for the same type:

    • public
    • private
    • protected
    • internal
    • abstract
    • sealed
    • base class
    • new modifier (nested parts)
    • generic constraints