Jon Slaughter said:
?
It will only create it if its there is a reference to it... and in any case
if they are not used by declared by the compiler implicitly they would be
optimized out.
Ah - that's extra to the logic you wrote before. Frankly I still don't
like it - creating a field only if it's referenced? It just feels
wrong.
Yeah, but I think this is simply from how you were taught properties. If you
think of a property really a field in an of itself then its probably quite
easy.
But you shouldn't think of a property as a field, because it's not.
I posted the definition that msdn gives for a property and all
properties have to do with fields.
No they don't. Consider DateTime.Now, for example.
Therefore a field exists somewhere either
explictly or implicitly. Right now we have to declare them explicitly. I
don't see it as a big leap to also include the ability to do it implicitly.
It's hiding what's going on, which I don't think is a good idea.
Although its possible to cause inital problems one can just use the old
methodology if they like and it won't cause any problems. I mean, its not
really any different than C# having an implicit destructor even if its not
declared. No one gets up and arms about that. I really don't see much of a
difference. Sometimes tradition is hard to break though.
Don't misinterpret the protests against your idea as merely tradition.
I'm quite happy for things to change - but not in this way.
How bout thinking about a property as a "container"... sorta like a class.
It contains a field and accessors. Instead of declaring the field outside
the propertly like normal we declare it inside:
public string Name
: private string name;
{
get { return name; }
set { name = value; }
}
Now *that* idea I've actually suggested before, in order to prevent
accidental use of fields within methods which should be using the
property.
But now why even do this, as the return type should be the same. Ofcourse
maybe this is not the case in general but if its a general case then one
should just use the old method:
public string Name
: private name;
{
get { return name; }
set { name = value; }
}
but most properties's fields are private which is the whole point. Obviously
they could be protected and hence one should use the old method of
explicitly declaring the field:
public string Name
: name;
{
get { return name; }
set { name = value; }
}
but now the name of the field is usually very similar if not identical to
the property(excluding case). Why not just use the name of the property
instead(and let the complier deal wiht the rest)?
Because I don't believe that implicitly generating fields is a good
idea, particularly when there's nothing making it clear that it's going
on. Creating a variable simply on the grounds that something tries to
refer to it is a bad idea IMO.
public string Name
{
get { return Name; }
set { Name = value; }
}
Now this might be a little strange since we are using a self reference. I
think its ok as the compilier knows what to do(its not very hard to make it
understand).
And what if you actually *want* to refer to the property? (It can
happen sometimes...)
But we could use the this keyword to make it clear.
public string Name
{
get { return Name.this; }
set { Name.this = value; }
}
either way works for me and it simplifies the "generic" property. For more
general properties one could ether extend this syntax or use the old method.
That would disambiguate, but I still don't like the idea of implicitly
adding fields.
Its not equivilent unless you forgot to add the accessors in the first
place. Sure, one could do that but its almost identically syntactically the
same.
Um, the point is that it *would* be equivalent if C# supported that
syntax. I didn't forget to add the accessors in the first place - the
"property" contextual keyword would tell both the compiler and the
reader that property accessors should be added.
I did think about that but then one is essentially combining the two
syntaxes into one statement block. You might as well just keep the old
method so one doesn't break anything.
My suggestion wouldn't break anything either - and it would make it
more obvious what was going on.
i.e., I'm not claming we break the accessor/property/field concept or syntax
in C#. I'm just saying that one could extend this for automatically deal
with simple properties. This takes the "work"(even though its not much) and
possible errors(even though its unlikely either... I'm thinking of
refactoring). i.e., it lets one write a simple generic property that is
commonly used. This is often done with other programming semantics too.
And my suggestion would do that too, without the presence or absence of
a field having to be inferred from whether or not it was ever referred
to.
Its just an idea. I'm not saying its some revolutionary thing or that it
will improve the lifes of programmers. I'm just curious why it wasn't done.
Maybe it was stupidity or maybe there was another reason. Maybe the reason
is simply the same reason that you have. I do not know and that is why I
asked. I know it can be done and I know it would work but that is not the
point.
Well, I suspect that the language designers would have similar feelings
to the idea of implicit field creation that I do - whether or not a
variable exists shouldn't depend on whether or not something tries to
*use* it. Now, that's not to say that there shouldn't be a simpler way
of declaring properties - just that your suggested way is a bad one,
IMO.