Reputation: 45801
I am using VSTS 2008 + C# + .Net 2.0 to develop Windows Forms application. I found by default, the new Form we created will be marked as public partial.
My concern is whether expose class as public has any security risks? Should we mark it as private? Any impact for functionality if we mark it as private?
BTW: I met with compile error when marking the class from public partial as private. Here is the compile error message, any ideas what is wrong?
Error 1 Elements defined in a namespace cannot be explicitly
declared as private, protected, or protected internal C:\FooTest\Form1.Designer.cs
Upvotes: 0
Views: 1048
Reputation: 3284
If you define it private, then what's the purpose of having the class? Nobody outside that file will have access to it. Not even your main program (probably running on Program.cs).
Partial means that your class was divided, so your Form class has more components than the one in your Form.cs file. VS just put the automatic generated code in one file and your code in another. So you can not eliminate the partial unless you move all the generated code into a single file, but if you do that and use the Form Designer to change a label font, then you are doom!.
Upvotes: 2
Reputation: 101605
It's exactly as the error message says - classes (and other types) declared in namespace scope cannot be private
, because private
means "no-one but enclosing class can see this", and there's no enclosing class at namespace scope.
If you want the class to not be seen from outside of assembly, you should use internal
.
Upvotes: 1