[Syntax]

A .NET language syntax wish list

Saturday, June 28, 2003

Syntax issues in VB.NET

Inline variable declarations

For Each s As String In strings

Next s

Fixed in version 7.1 (2003).

DynamicCast statement

Dim fi As FileInfo = DynamicCast(lvi.Tag, FileInfo)

To be fixed in version 8 (2005/Whidbey) - "TryCast".

Delegate combining syntax not just for events

Dim h1 As EventHandler = AddressOf Handler1
Dim h2 As EventHandler = AddressOf Handler2
Dim h3 As EventHandler = CombineHandler(h1, h2)
' Dim h3 As EventHandler = DirectCast(System.Delegate.Combine(h1, h2), EventHandler)

Reference inequality operator

If obj IsNot Nothing Then ' If Not (obj Is Nothing) Then

To be fixed in version 8 (2005/Whidbey).

Ternary operator (type-safe, short-circuited)

Dim s As String = ShortIf(f, Foo(), Bar())

Procedure declaration consistency

ReadOnly Property Count() As Integer
' basta (in an interface)

Protected MustOverride Function StateRule() As Boolean
' basta (in an abstract class)

Private Declare Function Beep Lib "kernel32.dll" (ByVal dwFreq As Int32, ByVal dwDuration As Int32) As Boolean
' basta (traditional external function declaration)

[DllImport("kernel32.dll")]
Private Function Beep(ByVal dwFreq As Int32, ByVal dwDuration As Int32) As Boolean
' no basta: pure declaration <> block, so the next line shouldn't be there:
End Function

Block comments

Rem
This is a comment
End Rem

Reducing type name redundancy

Addressed in version 9 (Orcas) through implicit static typing of locals.

To put it in a general way: whenever a variable is initialized from an expression (initialization expression), and the type of the initialization expression is directly specified in the initialization expression itself, the initialization expression can be used in place of the type name in the "As" clause:

Dim frm As New Form()            ' Dim frm As Form = New Form()
Dim frm As DirectCast(obj, Form) ' Dim frm As Form = DirectCast(obj, Form)

Expressing enums as parameterized types

' enums as a type are not by themselves data items that have
' types (like variables); rather, there is an underlying type
Enum(Of Byte) Kulerz ' Enum Kulerz As Byte

Syntax issues in C#

Multi-resource using blocks

using(Brush b = new WaterBrush(Color.Aqua),
      Font fnt = new Font("Gregoria", 7),
      StringFormat sf = new StringFormat())
{
    e.Graphics.DrawString("Sea Shark", b, fnt, rect, sf);
}

Reducing type name redundancy

Addressed in version 3 (Orcas) through implicit static typing of locals.

new StringBuilder(256) sb; // StringBuilder sb = new StringBuilder(256);

Expressing enums as parameterized types

// enums are not *derived* from their underlying type;
// rather, they are derived from System.Enum
enum<Byte> Kulerz //enum Kulerz : Byte