473,779 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is C# 2.0 syntax of Properties closed?

I red MSDN article of C# 2.0 this week... and i found
very strange syntax for properties e.g.:

public int MyIntValue {
get {
// ...
}
protected set {
// ...
}
}

.... Is this syntax defined and obligatory?

Maybe it's not only my feeling that aboved syntax is
not a best choice.
Any answer and discussion will be very interesting.

Marcin
Nov 16 '05 #1
23 2073
Hi Marcin,

This is just an example that you can have different access modifiers for
get and set. Get is public, set is private, and so on.

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Morten Wennevik wrote:
Hi Marcin,

This is just an example that you can have different access modifiers for
get and set. Get is public, set is private, and so on.

Happy coding!


Thanx... but i don't like this syntax. Because it is
self-conflicting.
First i have set property access to public "public" and then i have to
set e.g. set access to "protected" or "private".. .

I think that so much better from logical point-of-view looks:

int MyInt {
public get {
// ...
}
protected set {
// ...
}
}

// or even

public int MyInt {
get {...}
}

protected int MyInt {
set { ... }
}

Marcin
Nov 16 '05 #3

"Marcin GrzA?bski" <mg*******@taxu ssi.no.com.spam .pl> wrote in message
news:c4******** **@nemesis.news .tpi.pl...
Morten Wennevik wrote:
Hi Marcin,

This is just an example that you can have different access modifiers for
get and set. Get is public, set is private, and so on.

Happy coding!
Thanx... but i don't like this syntax. Because it is
self-conflicting.
First i have set property access to public "public" and then i have to
set e.g. set access to "protected" or "private".. .

I think that so much better from logical point-of-view looks:

int MyInt {
public get {
// ...
}
protected set {
// ...
}
}

// or even

public int MyInt {
get {...}
}

protected int MyInt {
set { ... }
}


Frankly, while I don't particularly care for the existing syntax, I don't
think either of these are any better. Seperating the accessors makes it less
immedaitly clear that they are related and putting the access modifiers on
the accessor makes it less immediatly clear that the property is public(as
far as most langauges are concerned at any rate, in metadata a property has
no accessibility).

Marcin

Nov 16 '05 #4
Hi Daniel,

int MyInt {
public get {
// ...
}
protected set {
// ...
}
}

// or even

public int MyInt {
get {...}
}

protected int MyInt {
set { ... }
}

Frankly, while I don't particularly care for the existing syntax, I don't
think either of these are any better. Seperating the accessors makes it less
immedaitly clear that they are related and putting the access modifiers on
the accessor makes it less immediatly clear that the property is public(as
far as most langauges are concerned at any rate, in metadata a property has
no accessibility).


What do You think about a sample code below...
(I don't know real C# 2.0 syntax rules and this sample is only
for speculate)

internal int MyInt {
get {
// ...
}
protected set {
// is this "protected internal" or "protected" ?
}
}

or

internal int MyInt {
public get {
// is this "internal" or "public" ?
}
set {
// ...
}
}

and what about setting "main" accessor to "protected" or
"private"?

I think that a properties are so close to a methods that it
was no reason to treat them other than a methods.

Marcin
Nov 16 '05 #5

"Marcin Grzebski" <mg*******@void .taxussi.com.pl .void> wrote in message
news:c4******** ***@mamut1.aste r.pl...
Hi Daniel,

int MyInt {
public get {
// ...
}
protected set {
// ...
}
}

// or even

public int MyInt {
get {...}
}

protected int MyInt {
set { ... }
}

Frankly, while I don't particularly care for the existing syntax, I don't
think either of these are any better. Seperating the accessors makes it
less immedaitly clear that they are related and putting the access
modifiers on the accessor makes it less immediatly clear that the
property is public(as far as most langauges are concerned at any rate, in
metadata a property has no accessibility).


What do You think about a sample code below...
(I don't know real C# 2.0 syntax rules and this sample is only
for speculate)

internal int MyInt {
get {
// ...
}
protected set {
// is this "protected internal" or "protected" ?
}
}

or

internal int MyInt {
public get {
// is this "internal" or "public" ?
}
set {
// ...
}
}

and what about setting "main" accessor to "protected" or
"private"?


This is where things get tricky. If I was writing the complier I would issue
an error in both cases because you are providing an accessor at greater
accessibility than the property. IMHO, explicit accessor access modifiers
should only be able to reduce accessibility.

I think that a properties are so close to a methods that it
was no reason to treat them other than a methods.
In that case, then, I would say that properties should be done away with and
java like GetXXX and SetXXX should be used instead. A property *is not* a
method as far as the langauge is concerned. Its a field with accessors that
happen to be implemented as methods in the runtime.
Marcin

Nov 16 '05 #6
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
In that case, then, I would say that properties should be done away with and
java like GetXXX and SetXXX should be used instead. A property *is not* a
method as far as the langauge is concerned. Its a field with accessors that
happen to be implemented as methods in the runtime.


Here I disagree. It's far closer to a method call than a field, to my
mind. It has the syntax of a field, but the semantics of a method call,
which is why you can't use a property as a ref parameter, for instance.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
Marcin Grzębski <mg*******@void .taxussi.com.pl .void> wrote in
news:c4******** ***@mamut1.aste r.pl:
What do You think about a sample code below...
(I don't know real C# 2.0 syntax rules and this sample is only
for speculate)

internal int MyInt {
get {
// ...
}
protected set {
// is this "protected internal" or "protected" ?
}
}
This would beg for a separation of get and set clauses in a
construction I think. I then would opt for:
internal int MyInt get
{
// code
}

internal protected int MyInt set
{
}
or

internal int MyInt {
public get {
// is this "internal" or "public" ?
}
set {
// ...
}
}

and what about setting "main" accessor to "protected" or
"private"?
The two are combined: the property name and the get or set, you
can't see them separated. So specifying 2 accessor operators on different
places is not that correct.
I think that a properties are so close to a methods that it
was no reason to treat them other than a methods.


They're not close to methods, if they were they'd accept
parameters. (Ok, the indexer property accepts a parameter). So they can't
be threated as methods, more like fields, or better: virtual fields.

FB

--
Get LLBLGen Pro, the new O/R mapper for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP
Nov 16 '05 #8
Frans Bouma [C# MVP] <pe************ ******@xs4all.n l> wrote:
I think that a properties are so close to a methods that it
was no reason to treat them other than a methods.


They're not close to methods, if they were they'd accept
parameters. (Ok, the indexer property accepts a parameter). So they can't
be threated as methods, more like fields, or better: virtual fields.


I think they're much more like methods than parameters. Consider:

o They end up as methods internally
o They can be virtual, overridden etc
o They *can* take parameters, as indexers - although C# is quite
restrictive in terms of its indexers, compared with other .NET
languages.
o They can't be used as ref/out parameters

So, in what way are they like fields *apart* from in access syntax?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
"Jon Skeet [C# MVP]" wrote...
I think they're much more like methods than parameters. Consider:
[snip]
o They *can* take parameters, as indexers - although C#
is quite restrictive in terms of its indexers, compared
with other .NET languages.


You can even extend that argument, as *all* properties with a setter can
make use of the default argument: value.

So it definitely is closer to methods than fields.

Furthermore, even Microsoft obviously consider properties in terms of
methods. If you reverse engineer a class into UML with the combination
VS.NET/Visio, properties show up in the operation section of the class!

// Bjorn A


Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
391
by: Marcin Grzębski | last post by:
I red MSDN article of C# 2.0 this week... and i found very strange syntax for properties e.g.: public int MyIntValue { get { // ... } protected set { // ... }
0
9471
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10302
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10136
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4036
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3631
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.