BT

InfoQ Homepage News New C# Features Not Found in VB

New C# Features Not Found in VB

Bookmarks

With the release of Beta 2, the feature set for the flagship .NET languages C# and Visual Basic have been solidified. In the past we have covered VB-only features like mutable anonymous types and XML Literals. Today we cover a couple of the C# only features.

The first feature is some syntactic sugar for creating data classes. Using the Automatic Properties syntax, C# developers can expose properties without writing the associated boilerplate code.

public int X { get; private set; }

In this example, a property called X is created with a public getter and a private setter. When using this sytnax, the compiler will automatically generate a private member variable and the associated getter and setter.

While an automatic property looks like a public member variable, it has some notable advantages. First of all, a lot of the built-in classes such as lists and grids only bind to properties. Over the long term, this allows developers to later add more complex logic without changing the public interface.

Another feature one will see in C# is collection initializers.

var mystrings = new List<string> { "Alice", "Bob", "Charlie" };.

Using a syntax similar to that of array initializers, this code will call the objects Add method for each item in the braces. This is not based on any particular interface, the type just needs to expose a method named Add that takes one variable.

These examples were pulled from Jomo Fisher's The Least You Need to Know about C# 3.0.

With all the excitement about closures and lambda expressions, it should be noted that Visual Basic will only be supporting one-line anonymous functions. C#, on the other hand, will continue to support multi-line line anonymous functions using the new delegate syntax.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • Lazy Question

    by John DeHope /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    "In this example, a property called X is created with a public getter and a private setter. When using this sytnax, the compiler will automatically generate a private member variable and the associated getter and setter."

    What will the private field's name be? I am hoping you say "the public property's name, with the first letter lowercase, and an underscore prepended". In this case that would mean "_x".

    This reminds me of a desire for a new accessor, something like "super private", where the field is only accessible to a single property, and nowhere else, including methods and properties of the same class.

  • New C# Features Not Found in VB

    by Michael Campbell /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    > New C# Features Not Found in VB

    One it's always had is some modicum of respect by the community for those who use it...

  • Re: New C# Features Not Found in VB

    by John DeHope /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    The community of language snobs? Yeah the lack of their respect is something that kept me up at night, when I was a well paid and very successful VB programmer.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.