Browse DevX
Sign up for e-mail newsletters from DevX


Top 10 Changes in C# Beta 2, Part I

As one would expect to find in the second public beta of Microsoft's first new commercial language in 10 years, this version of C# contains a large number of syntactical, compiler, and behavioral changes from Beta 1.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

rue to its word, on June 18, Microsoft released Visual Studio.NET Beta 2 to the eagerly awaiting attendees of Tech·Ed 2001 in Atlanta. The availability of this second (and last?) pre-RTM public beta of the new Visual Studio.NET development suite offers a glimpse of the new suite, including the much hyped C# language, in a flavor that is billed as significantly closer to the RTM version than Beta 1 was.

As one would expect to find in the second public beta of Microsoft's first new commercial language in 10 years, this version of C# contains a large number of syntactical, compiler, and behavioral changes from Beta 1. The complete inventory of improvements is too extensive to list here.

With this in mind, I've condensed the list down to manageable chunks, prioritized roughly by breadth of impact on the average developer. The changes are organized into two separate "Top 10" lists. These articles should assist future C# developers to adjust to the new beta with a minimal amount of effort.

This article begins the series with the Top 10 most significant new C# features.

#1–The Using Statement Is Now Supported
The using statement in C# (§8.13) automatically calls the Dispose method of target objects upon executing the final line of code contained within the using statement. Beta 1 did not support this functionality and returned a series of seemingly unrelated compiler errors.

Sample Code Snippet 1: using (Font Ft = new Font("Arial",10)) { MessageBox.Show(Ft.Name); }; // Ft.Dispose() called automatically MessageBox.Show(Ft.Name); // Error: type or namespace not found Sample Code Snippet 2: Font Ft = new Font("Arial",10); try { MessageBox.Show(Ft.Name); } finally { if (Ft != null) ((IDisposable)Ft).Dispose(); }

Beta 1 now supports this portion of the C# language specification, allowing a convenient method for replacing convoluted try-finally clauses for object disposal with an easy-to-read substitute. Note that the target resources must be structs or classes that implement System.IDisposable.

#2–External XML Comments Can Be Added to Code Documentation Through the Use of "<include>" Directives
One of the advantages of the C# language is its support for embedding XML-based documentation comments in your code. Beta 2 now supports the use of an <include> directive in your documentation comments.

Sample Code Snippet: /// <include file='code_docs.xml' /// path='MyCode/MyClass[@name="Test"]/*' /> class Test {...}

The <include> directive allows full separation of documentation from actual code, merging the two only at compile-time. It also allows multiple authors to work on documenting the features of separate code portions, even down to the member-level. Note that the path value is expressed using XML Xpath syntax.

#3–Arrays of Arrays Are No Longer Considered a CLS-Compliant Type
Arrays of arrays, also known as "jagged" arrays, formerly were considered CLS-compliant by the C# compiler. As of Beta 2, this is no longer the case. While no immediate compiler errors or warnings are produced, the resulting code may generate problems when inherited or called from other languages. It is unclear whether a compiler warning will be added in the RTM version to notify of the non-compliancy.

#4–The Sealed Modifier Can Now Be Used with the Override Modifier to Prevent Further Method Overrides in Derived Classes
In Beta 1, the Sealed modifier, when used on method overrides, yielded a compiler error: "The modifier 'sealed' is not valid for this item." In Beta 2, the compiler recognizes the Sealed modifier.

Sample Code Snippet: public class B : A { public B() { } sealed public override string Method1() { return "Override"; } }

Use of the Sealed modifier, in conjunction with the override method, will prevent anything classes derive from class B from further overriding Method1(), which originally was provided by the base class A.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date