One thing that I do not like about properties in C#

S

SpookyET

It would have been better if you had more control on the properties in C#:

class Foobar
{
int myFoo;

property int MyFoo
{
public get
{
return myFoo;
}

internal set
{
myFoo = value;
}
}
}

I have not yet looked at properties in C# 2.0, but I'm sure that this has
not yet been implemented.
Since those properties are translated into methods (get_MyFoo()
set_MyFoo() [weird, the D code below works fine without the ugliness of
get_ and set_ prefixes]), similar to the D code below, the only thing that
needs to be changed is the C# compiler to support this type of control.
In the D programming language, you do have something like this:

class Foobar
{
private int myFoo;

public int MyFoo()
{
return myFoo;
}// read property

protected void MyFoo(int value)
{
myFoo = value;
} // write property
}

To use it:

void main()
{
Foobar f;

put(f.MyFoo); // same as f.MyFoo();
f.MyFoo = 3; // same as f.MyFoo(3); error inaccessible
f.MyFoo + 3; // same as f.MyFoo(f.MyFoo() + 3); error inaccessible
}
 
J

Jon Skeet [C# MVP]

SpookyET said:
It would have been better if you had more control on the properties in C#:

Agreed.

I have not yet looked at properties in C# 2.0, but I'm sure that this has
not yet been implemented.

You're wrong - it has.

From http://msdn.microsoft.com/chats/vstudio/vstudio_032103.asp

<quote>
Q: Will separate access specifiers for getting and setting properties
become a part of the language?

A: Regarding different accessibility of get and set assessors of a
property, yes, we will support that in the Whidbey release of C#.
Since those properties are translated into methods (get_MyFoo()
set_MyFoo() [weird, the D code below works fine without the ugliness of
get_ and set_ prefixes]), similar to the D code below, the only thing that
needs to be changed is the C# compiler to support this type of control.

Actually, the CLR specs need to be changed to allow the getter and
setter to have different accessibility, too, if you care about being
CLS-compliant.

<quote>
CLS Rule 25: The accessibility of a property and of its accessors shall
be identical.
</quote>
 
D

Daniel O'Connell [C# MVP]

SpookyET said:
It would have been better if you had more control on the properties in C#:

class Foobar
{
int myFoo;

property int MyFoo
{
public get
{
return myFoo;
}

internal set
{
myFoo = value;
}
}
}

I have not yet looked at properties in C# 2.0, but I'm sure that this has
not yet been implemented.
Since those properties are translated into methods (get_MyFoo()
set_MyFoo() [weird, the D code below works fine without the ugliness of
get_ and set_ prefixes]), similar to the D code below, the only thing that
needs to be changed is the C# compiler to support this type of control.
In the D programming language, you do have something like this:

class Foobar
{
private int myFoo;

public int MyFoo()
{
return myFoo;
}// read property

protected void MyFoo(int value)
{
myFoo = value;
} // write property
}

To use it:

void main()
{
Foobar f;

put(f.MyFoo); // same as f.MyFoo();
f.MyFoo = 3; // same as f.MyFoo(3); error inaccessible
f.MyFoo + 3; // same as f.MyFoo(f.MyFoo() + 3); error inaccessible
}

Personally I think this is a pretty bad design, over all. Without explicitly
defining something as a property you are just going on the hope that you
won't need that particular overload. That is probably enough to make me
refuse to use properties as properties in D. The get and set prefixes are
designed so that there would be no naming collisions. Almost no one ever
actually has to directly access get_ and set_ so there is really no
uglieness there.
get_ and set_ aren't absolutly required either, IIRC, but are strongly
recommended.

Anyway, C# indeed will have properties which can have different accessiblity
on there accessors
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top