BT

New C# Features Not Found in VB

| by Jonathan Allen Follow 577 Followers on Jul 31, 2007. Estimated reading time: 1 minute |

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

Tell us what you think

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

Email me replies to any of my messages in this thread

Lazy Question by John DeHope

"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

> 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

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

Email me replies to any of my messages in this thread

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

Email me replies to any of my messages in this thread

3 Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT