-
May 23rd, 2014, 07:52 PM
#1
Fields or Properties?
what is the difference between a field and a property?
thx
s
-
May 23rd, 2014, 09:28 PM
#2
Re: Fields or Properties?
-
May 24th, 2014, 04:00 AM
#3
Re: Fields or Properties?
what is the difference between a field and a property?
filed is the variable in C# . just like
Private double _hours ; // here _hours is field .
normally fileld value are stored in the Property .
Code:
public double Hours { get { return seconds / 3600; } set { seconds = value * 3600; } }
see the following link as wellhttp://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx
Last edited by firoz.raj; May 24th, 2014 at 04:02 AM.
-
May 27th, 2014, 06:14 AM
#4
Re: Fields or Properties?
Properties allow you to control access to variables that you wish to be private
(not accessible from out side the class.)
or better said encapsulate fields.
private int length =0;
public Length{ get{return length;}} // since you cant set the length directly no one can muck it up
public void Add(... ){ ..... ; length ++; }
this is the equivalent of Setter and Getter Methods
it is simply a syntax difference to better express the idea of encapsulation applied at a low level
thus this is a clearer ideology for ... setters and getters.
...
newer versions of .net will allow access to the backing feild's maybe...
at least on user voice that's what they say.
i wanted to expand on this
properties are basically the idea of a method and (member variable or field if you like) rolled into one
it is based on the newer idea or older argument that perhaps, variables are a old outdated idea themselves
however it hasn't fully been embraced by c# but fairly close
.
to show what i mean in a practical sense
ill give a method example
private int length =0;
public int GetSetLength(int i){ length = i; return length;}
public int GetLength(){return length;}
public void SetLength(int i){length = i;}
now what is shown above can be represented by either version below.
practically equal to the above without you being able to set the length ie it is private on declaration to you
as if length were hidden from you and automatically initialized to 0
public int Length{get;set;} // (aka private hidden field auto included and initialized)
basically equal to the above, as you see you could in either case make the field public
private int length = 5;
public int Length{get{return length;}set{length = value;}}
the proposed next version is basically the combination of hidden and initialization parts
public int Length{get;set;} = 5; // (aka private hidden field auto included but can be initialized by you)
now the difference between this and just a regular field is
you can limit access to just a set or get as shown at the very top of the page
Last edited by willmotil; May 27th, 2014 at 05:22 PM.
Reason: clarity
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|