class fields vs class properties

  • Thread starter Thread starter Guest
  • Start date Start date
Koda,

A field holds data, a property access that data.

I hope this helps,

Cor
 
I will clarify Cor's response:

A field is a variable that is declared - it holds the actual data.
A property is syntactic sugar for Get and Set methods that you would other
wise have to call. So instead of having to say:

myObj,GetObjID()

you can say:

myObj.ObjID

So a property is really a different way to call get/set methods, and it
relies on the 'fields' in the class to get the correct data.
 
So based on your response they are "interchangeable" in their use just not in
how they are called.

Thanks
Mike
 
Think of a fielf as a variable.

Think of a Property as syntatic sugar for an getter and setter.

e.g. with a property you can do stuff like this:

private string someString;

public string SomeString
{
set
{
if(value == null)
{
throw new ArgumentNullException("someString cannot be null because
I said so");
}
someString = value;
}
}


i.e. you can have code that will execute within a property and still use it
as you would a normal field.

Does this help?
 
Koda said:
What is the difference between class fields and class properties?

I think it's a little difficult to explain. Generally, a field is a
declared variable:

class Foo {
int bar;
}

This field is a private field, and good OO practice dictates that fields
should always be private, so they can't be accessed from outside the
class Foo. Now, if the field must be accessed from the outside, one used
to use getter/setter methods to accomplish this, for example in
languages like C++. So you'd have public methods that would allow for
getting/setting the value of the bar variable, probably including some
code for validity checking, especially on the setter. Like this:

class Foo {
int bar;
public int GetBar() {
return bar;
}
public void SetBar(int newbar) {
// check newbar for validity
bar = newbar;
}
}

Properties are a syntactically nicer way of putting this same code. In
C#, the same class might look like this, using a property:

class Foo {
int bar;
public int Bar {
get { return bar; }
set {
// check the new value
bar = value;
}
}
}

Looking at the class Foo from the outside, it's also easier to work with
properties, because you can use normal syntax to get the value of a
property or assign a value to it:

...
Foo foo = new Foo();
foo.Bar = 5;
Console.WriteLine(foo.Bar);
...

This advantage is seen by some as a disadvantage, I should mention -
because the user of the property can't see the implementation of the
getter code, it's possible that the property getter does a lot of
computations every time the setter is called. In these cases it might
still be a better idea to code a "traditional" getter in the form of a
method, to suggest to the caller that getting the value is not a trivial
operation.

Now, one final thing I should note: you were asking about *class* fields
and *class* properties. In some languages, this is a way of referring to
what's called (in C#) a "static" field or property. I'm not sure if
that's what you meant, but just in case it is... a static field or
property is one that only exists once per class type, not once per
instance. To declare it, you'd use the keyword "static" with the
declaration of the variable and/or the property. I'm not going into any
more depth with this for now, please ask if you need more information on
this.


Oliver Sturm
 
Hi,
class fields are member variables of a class which can hold values, class
properties also serve the same purpose, however, can store(read/write)
values through accessors(get/set). Through accessors we can trigger whenever
there is a change or whenever the value of the property is accessed

best
Subin Kushle
 
Back
Top