Solved

What is the difference between a Field and a property in a class?

Posted on 2008-10-24
4
260 Views
Last Modified: 2010-04-21
Can any one explain me the difference between Fields and Properties of class with a small example? when should we use fields and when should we go for properties?
0
Comment
Question by:GouthamAnand
  • 3
4 Comments
 
LVL 10

Accepted Solution

by:
jinn_hnnl earned 500 total points
ID: 22794804
Hi,

In a lot of case they appears to be the same, the are several discussion about this.

  1. In case you are designing a class library and the consumer is not under your control then add properties for all public fields to prepare for future needs to add checks to field access
  2. In case you can version (change) the client code as well, do not add properties just because its good, or because you think you might need functionality of checks, lazy loading in the future. In case you do not need these features right-away allow clients to directly use fields. With the new refactoring features of VS2005 changing all references to the field to properties is a non-issue.
  3. With the above two filtering you are sure that once you have a property its definitely going to do some additional work and so only use the property from everywhere (including methods from same class).
More information about the discussion:
http://blogs.msdn.com/abhinaba/archive/2005/09/22/472706.aspx

Wait for my next post, I will give you the example for more understanding, and which case you might use

JINN





0
 
LVL 10

Assisted Solution

by:jinn_hnnl
jinn_hnnl earned 500 total points
ID: 22794854
Ok I split them to make clearer explanation.

The fields are like (file UserClass.cs):
class UserClass
{
     private string _username;
     public int _age;
     private string _company;
     private string _status = "very young";
 
     //properties
     public string UserName
    {
     get {return _username;}
     set{_username = value;}
     }

..
}

within a class; private field is enough to use among methods, and it can't be access from out side
protected void ShowName()
{
     lblMessage.Text = _username; //this._username
}

when it come to public field then it's very close to property.
Property are like:

public string UserName
{
     get {return _username;}
     set{_username = value;}
}

when you access to this field or property inside your class (in method ShowName) then
lblMessage.Text = _username; //<-- field
is exactly the same as
lblMessage.Text = Username; //<-- property

when you come out side let say in (login.aspx.cs) then
UserClass myUser = new UserClass();

myUser._username; <--- invalid because it private field
myUser.UserName <--- valid because it is public property.

but then as _age is public field
myUser._age <--- valid

//******
Ok now what is the difference  public field and public property. Well property you can do even more; for example you can create this property:

public string UserTitle
{
     get { return _username + " from " + _company; }
}

or
public string UserStatus
{
     get{
           if(_age > 30)
               return "Too old";
           else
               return "young";      
     }
}

So you see the different ?

Hope this helps

JINN

public class UserClass
{
    //field
     private string _username;
     public int _age;        //<-- public
     private string _company;
     private string _status = "very young";
 
     //properties
     public string UserName
    {
     get {return _username;}
     set{_username = value;}
     }
     
     public string UserTitle
	{
	     get { return _username + " from " + _company; }
	}
	
	public string UserStatus
       {
	     get{
	           if(_age > 30)
	               return "Too old";
	           else
	               return "young";	   
                }
       }
 
       //method
       protected void ShowName()
	{
	     lblMessage.Text = _username; //this._username
	}
	....
}

Open in new window

0
 

Author Closing Comment

by:GouthamAnand
ID: 31509579
Very Good explanation. Thank a lot.
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22798416
Glad to help

JINN ^^
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PrintingFoundIt(Almost!(Damn!)) 1 27
Printing 1 52
.NET 2008 VB and C# 6 28
SSIS Feed MS SQL Server Stored procedure with loop 4 7
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question