Syntax Consistency among Class Members (Events, Properties, and Methods)

VB.Net wins hands-down for being consistent in its syntax. Consider these following examples.

First, let me quickly define my terminology. By implicit, I mean the compiler generates code behind the scenes. By explicit, I mean WYSIWYG. The implicit properties you see are actually a proposed C# language change called “Automatic Properties”. You can read about it here.

Consider the following C# event, property, and method cases.

// IMPLICIT
event <type> MyEvent;
<type> MyProperty { get; set; }
// implicit method is not possible

// ABSTRACT
abstract event <type> MyEvent;
abstract <type> MyProperty { get; set; }
abstract <type> MyMethod();

// EXPLICIT
event <type> MyEvent {add{} remove{}}
<type> MyProperty {get{} set{}}
<type> MyMethod() {}

Now consider these VB.Net cases. I have not found what the VB.Net syntax will be for automatic properties, so I am making a best-guess.

// IMPLICIT
Event MyEvent As <type>
Property MyProperty As <type>
// implicit method is not possible

// ABSTRACT
// abstract event not possible
MustOverride Property MyProperty As <type>
MustOverride Function MyMethod As <type>

// EXPLICIT
Custom Event <type> MyEvent : Add…End, Remove…End, Raise…End, End
Property MyProperty As <type> : Get…End, Set…End, End
Function MyMethod As <type> : End

C# Inconsistencies

  1. Event has a keyword, but method and property do not.
  2. Implicit properties has accessors. Implicit event does not.
  3. Abstract property has accessors. Abstract event does not.

VB.Net Inconsistencies

  1. Explicit event requires “Custom” keyword.

Proposal objections

One thing to note is that some have suggested the new Automatic Properties in C# be formatted like this:

property <type> MyProperty;
readonly property <type> MyProperty;

This would change the C# inconsistencies to this:

// IMPLICIT
event <type> MyEvent;
property <type> MyProperty;
// implicit method is not possible

// ABSTRACT
abstract event <type> MyEvent;
abstract <type> MyProperty { get; set; }
abstract <type> MyMethod();

// EXPLICIT
event <type> MyEvent {add{} remove{}}
<type> MyProperty {get{} set{}}
<type> MyMethod() {}

C# Inconsistencies

  1. Event and implicit property have keywords, but method, explicit property, and abstract property do not.
  2. Implicit properties has accessors. Implicit event does not.
  3. Abstract property has accessors. Abstract event does not.

My Suggested Automatic Property Format

I agree that automatic properties should introduce the keyword “property” into the C# language. However, with this change, property should also optionally be allowed in non-automatic property declarations. This would reduce the inconsistencies considerably.

This would change the C# inconsistencies to this:

// IMPLICIT
event <type> MyEvent;
property <type> MyProperty;
// implicit method is not possible

// ABSTRACT
abstract event <type> MyEvent;
abstract property <type> MyProperty;
abstract <type> MyMethod();

// EXPLICIT
event <type> MyEvent {add{} remove{}}
property <type> MyProperty {get{} set{}}
<type> MyMethod() {}

C# Inconsistencies

  1. Event and property have keywords, but method does not.

1 comment so far

  1. Darla on

    Mayy I simplyy just say what a comfort tto uncover someone whoo really knnows
    what thhey are talking aout on the internet. Yoou
    definhitely uderstand how too bring a problem too light and make it important.

    Morre aand morfe peiple have to checkk this out and understand thios siude of your story.

    It’s sujrprising you’re noot moree popular because youu definitely hazve the gift.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: