Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Sir,
In the Bean File of C# project we define the set and get methods.
But before define this we declare names as a private string.. So why we declare the name as private? Is this for security reason or any other?
Posted

Bean file in C#?

About private access modifier:
private: The type or member can be accessed only by code in the same class or struct.

Read all you want to know about all access modifiers here:
MSDN: Access Modifiers (C# Programming Guide)[^]
 
Share this answer
 
Comments
Espen Harlinn 4-Nov-12 11:38am    
5'ed!
Sandeep Mewara 4-Nov-12 11:50am    
Thanks Espen. :)
This is not for security. Access modifier does not protect a member from access. With Reflection, anyone can access any members, including private and internal. This is done for the same purpose as all other features of programming languages and systems — to support programming as a human activity, to make programming less tedious and error prone. Access modifiers protect developers from additional mistakes usual when everything is accessible and allowed.

—SA
 
Share this answer
 
Comments
Espen Harlinn 4-Nov-12 11:38am    
5'ed!
Sergey Alexandrovich Kryukov 4-Nov-12 11:51am    
Thank you, Espen.
--SA
A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.

So, if the private variable isn't private, then other classes can get/set the value of that variable. And then the private variable skips the get/set process from the property.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900