
s the first new commercial language Microsoft has introduced since Visual Basic 1.0 in 1991 (FoxPro was acquired in June 1992), C# is quickly garnering heavy attention from interested spectators and hardcore developers alike. Beta 2 (released on June 18) incorporates a number of changes as Microsoft polishes the language for a projected Release-To-Manufacturing date near the end of 2001.
In this second article of a two-part series, I introduce another ten important changes found in C# Beta 2. If you haven't read Part I of the Top 10 Changes in C# Beta 2 series yet, don't worry; you can read the articles in any order. Developers already familiar with Beta 1 should be aware of the changes detailed in both articles.
#1Use of Backtick and $ for Identifying Verbatim Strings No Longer Supported
Although Beta 1 C# (§2.5.3.5) and Beta 2 (§2.4.4.5) documentation for string literals both correctly specify a verbatim string literal as consisting of a @ character followed by zero or more characters enclosed in double-quotes, Beta 1 supported the use of the backtick (`) in place of the @ character. Beta 1 also supported the use of the $ character for verbatim string literals as a substitute for the double-quote, when used in conjunction with the backtick. Beta 2 no longer supports the use of the backtick or the $ character when specifying a verbatim string literal.
Sample Code Snippet:
string s1 = `$hello\tworld$; // Unsuppported in Beta 2
string s2 = `"hello\tworld"; // Unsuppported in Beta 2
string s3 = @"hello\tworld"; // Beta 1 and 2 support
string s4 = @$hello\tworld$; // Neither beta 1 or 2
#2Arrays with Unknown Ranks Are Not Supported by the Compiler
In Beta 1, use of the feature 'array of unknown rank' generated a compiler warning regarding deprecation, but it compiled and functioned. In Beta 2, this feature has been eliminated.
Sample Code Snippet:
int[] arr1 = {1,2};
int[,] arr2 = {{1,1},{2,3}};
int[?] arrTemp; // Array of unknown rank not allowed
arrTemp = arr1;
MessageBox.Show(arrTemp.Rank.ToString());
arrTemp = arr2; // Implicit convert error if known rank used
MessageBox.Show(arrTemp.Rank.ToString());
#3Assemblies Now Assumed to Be CLS Non-Compliant
Prior to Beta 2, the C# compiler assumed that all assemblies not explicitly marked with the [CLSCompliant(false)] attribute were compliant with the Common Language Specification (CLS). The C# language specification indicates that a library without an explicit CLSCompliant attribute setting of True should be assumed non-compliant. This C# compiler behavior has been corrected in Beta 2. To mark an assembly as being CLS-compliant now requires an explicit use of the attribute.
#4Syntax for Event Accessors Now Consists of Add and Remove Accessor Declarations
Previously, explicit event accessor declarations followed the same syntax as property declarations. A Get and Set accessor was used to obtain and set the delegate value with an object key. Beta 2 modified the behavior of event accessor declarations in C# to map more intuitively to the behavior of adding and removing delegates to the event sink list.
Sample Code Snippet 1 (old code):
public event MouseEventHandler MouseUp {
get {return (MouseEventHandler)GetEventHandler(mouseUpKey);}
set {SetEventHandler(mouseUpKey, value);}
}
Sample Code Snippet 2 (new code):
public event MouseEventHandler MouseUp {
add {AddEventHandler(mouseUpEventKey,value); }
remove {RemoveEventHandler(mouseUpEventKey,value); }
}