Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

726 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