Solved

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

Posted on 2008-10-24
4
258 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now