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
- Event has a keyword, but method and property do not.
- Implicit properties has accessors. Implicit event does not.
- Abstract property has accessors. Abstract event does not.
VB.Net Inconsistencies
- 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
- Event and implicit property have keywords, but method, explicit property, and abstract property do not.
- Implicit properties has accessors. Implicit event does not.
- 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
- Event and property have keywords, but method does not.
No comments yet
Leave a reply