Click here to Skip to main content
Sign Up to vote bad
good
See more: C#
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 3 Nov '12 - 8:20


3 solutions

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)[^]
  Permalink  
Comments
Espen Harlinn - 4 Nov '12 - 11:38
5'ed!
Sandeep Mewara - 4 Nov '12 - 11:50
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
  Permalink  
Comments
Espen Harlinn - 4 Nov '12 - 11:38
5'ed!
Sergey Alexandrovich Kryukov - 4 Nov '12 - 11:51
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.
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 419
1 Mahesh Bailwal 303
2 CPallini 245
3 Maciej Los 240
4 Aarti Meswania 173
0 Sergey Alexandrovich Kryukov 9,162
1 OriginalGriff 7,179
2 CPallini 3,913
3 Rohan Leuva 3,176
4 Maciej Los 2,588


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 3 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid