?
Solved

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

Posted on 2008-10-24
4
Medium Priority
?
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 10

Accepted Solution

by:
jinn_hnnl earned 2000 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 2000 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

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

770 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