New features provided in C # that are not supported by VB

Source: Internet
Author: User
Tags anonymous new features

In Beta 2,. The two flagship languages in net--c# and Visual Basic have their own set of capabilities. Previously we have introduced features that only appear in VB, such as variable anonymous types and XML constants. Today, however, this article describes some of the features that only appear in C #.

The first feature is the convenient syntax for creating a data class. With the help of automatic properties (Automatic properties) syntax, C # developers can easily create attributes without writing any duplicate code.

public int × {get; private set;}

The above code creates a property called X, the getter of which is public and the setter is private. When you use the above syntax, the compiler automatically generates a private field and associates it with the getter and setter above.

Although such an automatic property syntax might look similar to a public field, it has some notable advantages. First, many of the built-in types (such as lists and data grids) can only be bound to properties. In the long run, using attributes also allows developers to add more complex logic when they need it, while at the same time not needing to change the type of public interface.

Another feature in C # is the collection initializer.

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

With the syntax of a similar array initializer, the preceding code automatically calls the Add method of the collection to add each entry in parentheses to the collection. Of course, this feature cannot be applied to any object, and an object that can use the attribute should provide an Add method, and the Add method accepts a parameter.

The above sample programs are all from Jomo Fisher's article The least you Need to Know about C # 3.0.

It should be noted that Visual basic can only support a single line of anonymous methods for exciting features such as closures and Lambada expressions. C # is even more powerful--with the new delegate syntax, C # will be able to support multiple lines of anonymous methods.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.