BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News New C# Features Not Found in VB

New C# Features Not Found in VB

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

BT