Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Top 10 Changes in C# Beta 2, Part I : Page 2

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.


advertisement
#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.





Michael Lane Thomas, also known as the .NET Cowboy for his sometimes untamed, Wild West-style passion for .NET, has been a fixture in the development community for many years. A speaker at professional, academic, and Microsoft-internal technical conferences, Michael has been a primary contributor to 26 books, including a multi-year stint as .NET Series Editor for Wiley's/Hungry Minds, going back to the Beta days of .NET. Michael has spent time as industry analyst, commentator, and co-host of a weekly radio talk show. As an exam-junkie, Michael is currently the eighth most certified MCP in the world, passing a total of 62 exams to date. Michael is currently a Developer Evangelist for Microsoft and greatly enjoys exploring the Alpha bits for Whidbey as a Microsoft VS.NET Insider, and his role of daddy to his 1-year-old son Noah, also known as "Mr. Pinchy Cheeks."
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap