Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

15 Experts available now in Live!

Get 1:1 Help Now