Top 10 Changes in C# Beta 2, Part I

Top 10 Changes in C# Beta 2, Part I

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 “” 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 directive in your documentation comments.

Sample Code Snippet:   ///    class Test {...}

The 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.

#5?Events Can Now Be Marked as Virtual, Abstract, or Override
In Beta 1, modifiers recognized for declaring events were limited to:

  • New
  • Static
  • One of four access modifiers (public, protected, internal, private)

Beginning with Beta 2, the list of optional modifiers that the compiler recognizes for event delegation assignments is expanded to also include:

  • Abstract
  • Override
  • Virtual

The modifiers behave the same as when they are used in conjunction with classes or methods.

#6?Modifiers for Property Accessors (Get or Set) Must Be Placed Directly on the Property Declaration Itself
Beta 1 supported the application of modifiers (such as virtual or abstract) directly to the Get and/or Set accessors for a property declaration.

Sample Code Snippet:public string Name    {      virtual get // Error in beta 2, compiles in beta 1      {          return name;       }...}

Now modifiers applied in this manner produce a compiler warning: “Modifiers cannot be placed on property or event accessor declarations.” You must move modifiers to the property declaration itself.

#7?The Last Clause in a Switch Statement Must End with a Break, Return, or Similar Redirection Statement
The switch statement in C# adheres to a “no fall through” rule, whereby each case in the switch statement must end in a construct that renders the conclusion of the statement list unreachable. Typically, the construct used is throw, break, or return. Implementation of the “no fall through” rule in Beta 1 erroneously checked all clauses except for the last one.

Sample Code Snippet:   int i2 = 1;   switch (i2)    {      case 1:         MessageBox.Show("1");         break;      default:         MessageBox.Show("def");         // break;   }

In Beta 2, this is corrected so that all clauses are checked, and this sample code will generate a compiler error.

#8?The Index Variable of a Foreach Statement Is Now Read-Only
Assignment against the index variable of a foreach statement was permissible under Beta 1. The variable defined was writable, hence modifying the value of the index variable would produce no compiler errors, although the modified value existed only for the duration of the loop iteration in which the modification occurred.

Sample Code Snippet:   int[] arr = new int [] {0,1,2,5,7,8,11};   foreach (int i in arr)    {      MessageBox.Show(i.ToString());       i++; // Error: Cannot assign to 'i'       MessageBox.Show(i.ToString());          }

Beta 2 denies such assignments, indicating that ‘i’ is read-only.

#9?Method Signatures Cannot Be Differentiated Solely by Toggling a Parameter Mode Between Ref and Out
Under Beta 1, overloading methods could be accomplished by changing only the mode of a parameter between ref and out. This is no longer an acceptable method signature differentiator under Beta 2.

Sample Code Snippet:   public virtual string Method1(ref string s)   {      return s;   }   public virtual string Method1(out string s)   { // Generates error ..}

#10?Virtual Members Can Be Overridden by Abstract Members
Previously, attempts to override a virtual member with a member marked as new, virtual, or abstract in the derived class resulted in a compile error. Starting with Beta 2, override members of a virtual base member may now be marked as abstract. (Use of the new or virtual modifiers on an override member still produces a compiler error.)

Sample Code Snippet:   public class A   {   public virtual string Method1()         {return "ok";} // base member marked virtual   }   // derived class is abstract   public abstract class B:A    {   // Override member marked as abstract      public abstract override string Method1();   } // No compiler error in beta 2

This Top 10 list is only the beginning of the improvements to be found in C# Beta 2. Stay tuned for more in Part II.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist