Hi,
I'm new to C#. Most examples about properties I found are sth like the
following:
class A{
private int property;
public int Property {
get { return property; }
set { property = value; }
}
}
My question is, Can I simply do the following:
class A {
public int Property;
}
Then later, if it turns out that I need to do sth when the property is set
or get, I change it to
class A{
public int Property {
get { <do sth>; return ... }
set { <do sth>; }
}
}
This way, the code that uses A should still compile. Am I right? This way
sounds much simpler to me, you don't have to write those repetitive get&set
unless you later find out that you need. Is this the right way of using
properties? Am I missing sth here?
thanks
vh 7 2279
"vh" <ve*********@yahoo.ca> wrote in message
news:e4*************@TK2MSFTNGP11.phx.gbl... Hi, I'm new to C#. Most examples about properties I found are sth like the following: class A{ private int property; public int Property { get { return property; } set { property = value; } } }
My question is, Can I simply do the following: class A { public int Property; }
Then later, if it turns out that I need to do sth when the property is set or get, I change it to class A{ public int Property { get { <do sth>; return ... } set { <do sth>; } } } This way, the code that uses A should still compile. Am I right? This way sounds much simpler to me, you don't have to write those repetitive get&set unless you later find out that you need. Is this the right way of using properties? Am I missing sth here?
yes, an 'o', 'm', 'e','i','n', and a 'g'.
Anyway, the primary differences are:
1) in already compiled code, changing to a property requires all dependent
code to be recompiled. Field to property is a binary breaking change
2) Reflection works differently for fields and properties and therefore will
cause code that works against one to break when it is applied against
others. It may change serialization or other systems that use reflection.
3) a field may be passed as a ref argument to a method, a property may not.
Changing the two will break any code which passes Property by ref(although
thats a bad idea over all, IMHO).
4) public fields are considered bad form in libraries, mainly due to #1 and
#2 above.
If none of these issues apply to your case, you are in the clear. Otherwise,
just get used to typing it and\or write a macro to do it for you.
Daniel O'Connell [C# MVP] wrote: "vh" <ve*********@yahoo.ca> wrote in message news:e4*************@TK2MSFTNGP11.phx.gbl... Hi, I'm new to C#. Most examples about properties I found are sth like the following: class A{ private int property; public int Property { get { return property; } set { property = value; } } }
My question is, Can I simply do the following: class A { public int Property; }
Then later, if it turns out that I need to do sth when the property is set or get, I change it to class A{ public int Property { get { <do sth>; return ... } set { <do sth>; } } } This way, the code that uses A should still compile. Am I right? This way sounds much simpler to me, you don't have to write those repetitive get&set unless you later find out that you need. Is this the right way of using properties? Am I missing sth here?
yes, an 'o', 'm', 'e','i','n', and a 'g'.
?? Anyway, the primary differences are: 1) in already compiled code, changing to a property requires all dependent code to be recompiled. Field to property is a binary breaking change
(more field-to-property points snipped).
If I read the OP right, the issue was not starting with a public field and
changing it to a property later, but starting with an empty property and
adding the "get" and "set" parts later.
Joachim
Hi vh,
You are right - you *could* do:
class A {
public int Property;
}
but this allows anyone to change or read the value - so there would be no
point in property accessors!
This approach is really looked down apon for a number of reasons principle
among them a subject called encapsulation - external classes shouldnt be
able to "see" the internals of a class. They should only see what the class
chooses to expose. - This prevents the client class from depending on the
implementation of the class, which may subsequently change.
For example. Imagine you have a class called age. If you let a client class
set or get age in any which way it chooses, what happens when you want to
restrict the sorts of ages that can be entered. Say you only want numbers
between 0 and 120 to be entered? In a set property, you could provide that
logic, whereas in a public variable the client classes can do whatever they
want.
Another example - quite often in larger projects you may not want client
code to be able to both set and get particular values. Often you want one or
the other.
Anyway, there are all sorts of quite technical reasons why you shouldnt do
direct access. When you begin to get more confident and want to start taking
on larger projects and want to incorporate patterns and best practive
examples, you really will want to have learned to do things the "correct"
way seeing as everyone else is doing it. Even though at the moment you may
not understand all the reasons for having to do things a particular way.
One last thing - look into OO in general and more specifically
encapsulation. This should help you understand why programmer like to
compaetmentalise everything. (Think building blocks)
HTH
Take care
Simon
Joachim Pense wrote: Daniel O'Connell [C# MVP] wrote:
"vh" <ve*********@yahoo.ca> wrote in message news:e4*************@TK2MSFTNGP11.phx.gbl... Hi, I'm new to C#. Most examples about properties I found are sth like the following: class A{ private int property; public int Property { get { return property; } set { property = value; } } }
My question is, Can I simply do the following: class A { public int Property; }
Then later, if it turns out that I need to do sth when the property is set or get, I change it to class A{ public int Property { get { <do sth>; return ... } set { <do sth>; } } } This way, the code that uses A should still compile. Am I right? This way sounds much simpler to me, you don't have to write those repetitive get&set unless you later find out that you need. Is this the right way of using properties? Am I missing sth here?
yes, an 'o', 'm', 'e','i','n', and a 'g'.
??
Anyway, the primary differences are: 1) in already compiled code, changing to a property requires all dependent code to be recompiled. Field to property is a binary breaking change
(more field-to-property points snipped).
If I read the OP right, the issue was not starting with a public field and changing it to a property later, but starting with an empty property and adding the "get" and "set" parts later.
Reading the OP again, it turns out that I posted nonsense. Sorry for wasting
the bandwidths.
Joachim
"Joachim Pense" <sp************@pense-online.de> wrote in message
news:co*************@news.t-online.com... Joachim Pense wrote:
Daniel O'Connell [C# MVP] wrote:
"vh" <ve*********@yahoo.ca> wrote in message news:e4*************@TK2MSFTNGP11.phx.gbl... Hi, I'm new to C#. Most examples about properties I found are sth like the following: class A{ private int property; public int Property { get { return property; } set { property = value; } } }
My question is, Can I simply do the following: class A { public int Property; }
Then later, if it turns out that I need to do sth when the property is set or get, I change it to class A{ public int Property { get { <do sth>; return ... } set { <do sth>; } } } This way, the code that uses A should still compile. Am I right? This way sounds much simpler to me, you don't have to write those repetitive get&set unless you later find out that you need. Is this the right way of using properties? Am I missing sth here?
yes, an 'o', 'm', 'e','i','n', and a 'g'.
??
Anyway, the primary differences are: 1) in already compiled code, changing to a property requires all dependent code to be recompiled. Field to property is a binary breaking change
(more field-to-property points snipped).
If I read the OP right, the issue was not starting with a public field and changing it to a property later, but starting with an empty property and adding the "get" and "set" parts later.
Reading the OP again, it turns out that I posted nonsense. Sorry for wasting the bandwidths.
np.
I do find this to be one major missing feature in C#. IMHO they should have
added a property keyword and allowed simple
public property int Property;
but...its not that way and probably never will be.
I think it's an important distinction, becauseit allows the caller to know
whether code is run or not.
"Daniel O'Connell [C# MVP]" wrote: "Joachim Pense" <sp************@pense-online.de> wrote in message news:co*************@news.t-online.com... Joachim Pense wrote:
Daniel O'Connell [C# MVP] wrote:
"vh" <ve*********@yahoo.ca> wrote in message news:e4*************@TK2MSFTNGP11.phx.gbl... > Hi, > I'm new to C#. Most examples about properties I found are sth like the > following: > class A{ > private int property; > public int Property { > get { return property; } > set { property = value; } > } > } > > My question is, Can I simply do the following: > class A { > public int Property; > } > > Then later, if it turns out that I need to do sth when the property is > set or get, I change it to > class A{ > public int Property { > get { <do sth>; return ... } > set { <do sth>; } > } > } > This way, the code that uses A should still compile. Am I right? This > way sounds much simpler to me, you don't have to write those repetitive > get&set > unless you later find out that you need. Is this the right way of using > properties? Am I missing sth here? >
yes, an 'o', 'm', 'e','i','n', and a 'g'.
??
Anyway, the primary differences are: 1) in already compiled code, changing to a property requires all dependent code to be recompiled. Field to property is a binary breaking change
(more field-to-property points snipped).
If I read the OP right, the issue was not starting with a public field and changing it to a property later, but starting with an empty property and adding the "get" and "set" parts later.
Reading the OP again, it turns out that I posted nonsense. Sorry for wasting the bandwidths.
np.
I do find this to be one major missing feature in C#. IMHO they should have added a property keyword and allowed simple public property int Property;
but...its not that way and probably never will be.
"Bonj" <Bo**@discussions.microsoft.com> wrote in message
news:78**********************************@microsof t.com... I think it's an important distinction, becauseit allows the caller to know whether code is run or not.
Come again? Using a property still doesn't nessecerily look like a property,
changing the language to support simple properties wouldn't help here,
whatsoever. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: neutrinman |
last post by:
My question is how should I use "property" which wraps up
__get_channel() and __set_channel()in the following program.
I tried the program that written below, and it worked. Then I tried:
channel...
|
by: Yeongja_Choi |
last post by:
How Dare Could America Industrial Property Office Be In Conspiracy With
Jungang International Patent Office To Make An Extravagant
International Crime ?
Currently a Korean party now holds the...
|
by: Tom Kiefer |
last post by:
Question:
If I have an ASP.NET User Control which defines/exposes a property that the
page can use to specify a mode or data subset for the control to use, is
there a way to tell the @OutputCache...
|
by: Noozer |
last post by:
Trying to specify a single public property on my user control to hold three
different images. I figure out how to do this and Google searches are not
helping. (I probably am not searching on the...
|
by: djake |
last post by:
Someone can explain me why to use property get and property set to
access a value in a class, insted of access the value directly?
What's the usefulness of property statements in VB.NET?
Thanks
|
by: pinetaj |
last post by:
Hello,
I have a question of using 'property' on accessing elements of array.
There is an array member in a class. I'd like to restrict accessing the
elements of the array through property. And...
|
by: tomjbr.32022025 |
last post by:
I have started looking at the nhibernate framework, but do not really
like the string based API which makes it impossible to use automatic
refactoring of a property name without the risk of getting...
|
by: David Moss |
last post by:
Hi,
I want to manage and control access to several important attributes in
a class and override the behaviour of some of them in various
subclasses.
Below is a stripped version of how I've...
|
by: Hussein B |
last post by:
Hey,
I noted that Python encourage the usage of:
--
obj.prop = data
x = obj.prop
--
to set/get an object's property value.
What if I want to run some logic upon setting/getting a property?...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |