locked
Partial Class - public or internal RRS feed

  • Question

  • I have read that when a Class is created from a number of Partial Classes then each of the Partial Classes must have the same accessibility modifier - public or internal.

    Using visual Studio to create a Windows Forms project we get:-
    In the Form1.cs
    public partial class Form1 : Form
    In the Form1.Designer.cs
    partial class Form1, and as no accessibility modifier is specified and as it is a Class it will default to internal.

    This works so does that mean that the statements I have seen requiring the modifiers to be the same are wrong?


    Peter

    Thursday, October 1, 2015 11:08 AM

Answers

  • As you say, internal is the default if no access modifier is specified.

    But an access modifier IS specified, even if that is only on one of the partial class declarations.

    Remember that it doesn't matter how many partial classes you have scattered around separate files - that is just a convenience (e.g. to keep auto-generated parts of a class separate from developer-editable parts of a class). In reality they are all one single class.

    Hence it would be invalid to have different access modifiers, and by putting an access modifier on one of the partial class definitions you are really putting it on all of them - because they are all the same class.

    • Proposed as answer by Magnus (MM8)MVP Thursday, October 1, 2015 3:39 PM
    • Marked as answer by peterwt Saturday, October 10, 2015 3:19 PM
    Thursday, October 1, 2015 3:36 PM

All replies

  • partial class Form1, and as no accessibility modifier is specified and as it is a Class it will default to internal.

    Incorrect asumption (more then one actually):

    https://msdn.microsoft.com/en-us/library/wa80x488.aspx?f=255&MSPPError=-2147217396

    "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:" (access modifiers are among those).

    Nothing does not conflict with public. So public is used.

    Thursday, October 1, 2015 11:39 AM
  • Thanks for your reply Christopher84, but I am confused by it.

    You state that I a have made incorrect assumptions - I would be grateful if you will say what they are.

    In https://msdn.microsoft.com/en-us/library/ms173121(v=vs.120).aspx it states:-

    "Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified."

    There is no access modifier the Form1.Designer.cs  "partial class Form1" so it should be internal.


    Peter

    Thursday, October 1, 2015 3:20 PM
  • As you say, internal is the default if no access modifier is specified.

    But an access modifier IS specified, even if that is only on one of the partial class declarations.

    Remember that it doesn't matter how many partial classes you have scattered around separate files - that is just a convenience (e.g. to keep auto-generated parts of a class separate from developer-editable parts of a class). In reality they are all one single class.

    Hence it would be invalid to have different access modifiers, and by putting an access modifier on one of the partial class definitions you are really putting it on all of them - because they are all the same class.

    • Proposed as answer by Magnus (MM8)MVP Thursday, October 1, 2015 3:39 PM
    • Marked as answer by peterwt Saturday, October 10, 2015 3:19 PM
    Thursday, October 1, 2015 3:36 PM
  • Thanks for your reply Christopher84, but I am confused by it.

    You state that I a have made incorrect assumptions - I would be grateful if you will say what they are.

    In https://msdn.microsoft.com/en-us/library/ms173121(v=vs.120).aspx it states:-

    "Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified."

    Damn. I was certain the default accessor would have been private, not internal. Well, learn something new every day.

    But the rest RJP answered:
    You did specify one access modifier. No default is going to be used. When the compiler compiles this, all those seperate sourcecode files will result in only one, singular, final class. As sure as if you had written it all into a single file, single namespace to begin with.
    There is still only one class - split over 2 or more sourcecode files.

    Thursday, October 1, 2015 4:07 PM
  • Thanks for the replies. I realise I was not misinterpreting

    "Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified."


    Peter

    Saturday, October 10, 2015 3:19 PM