Pages

C# Features Through the History - Version 3.0



The main features of the version 3.0 now!

The version 3.0 of the C# language was released in late 2007, coming with .NET Framework version 3.5.

The idea of this release is to provide lots of features that could, at least, differentiate from Java, and provide some unique features. Most of these new features turned the language more concise and provided a more straightforward approach for the developers. 

How to use the examples 

The examples are available here. You can open the solution and play around. Just uncomment the code and have fun! 

Auto-Implemented Properties 


The auto-implemented properties make the code concise and more readable. It's a way to simplify the development and rely on the compiler to create, internally, the private fields correspondent to these properties.


Anonymous Types 


With anonymous types, it's possible to define an object with a set of read-only properties. The compiler will infer the data types at compile time. This new types will be specially used in the new LINQ structures, implemented in this version of the language.


  
Query Expressions 


A query expression in a way to express a query using C#. Basically you have an approach to write a SQL-like language, and iterate between a collection object to return the desired results. These capacities are provided by the new namespace System.Linq 



Lambda Expressions 


Lambda expressions are an easy way to defining an anonymous function that can be passed as a variable or a parameter to a method call. All the lambda expressions can be converted into a delegate type (a pointer to a function). It's also provided by the System.Linq namespace.



One thing is that is important is that query expressions are translated into lambda ones before they're compiled. That means it's up to you choose the one that you think it's more convenient.



Expression Trees


In a LINQ query, the functions you write are transformed into a delegate for the compiler. For richer interaction, you need to use Expression Trees. This approach will provides a way to represent your code into a tree-like format. An expression tree is represented by a body, parameters, NodeType and Type. Different from lambda expressions, with an expression tree you can look inside these expressions, instead of just executing them. Namespace System.Linq.Expressions.


Extension Methods



Extension methods allow us to add methods to existing types, without the necessity to create a new type, recompile the entire code, or modify the original types. They are static types but, despite of this, they can be called for an instance method of the extended type.

In this example, we have a class Customer, and two different extension methods, one receiving a string, and another receiving the Customer class.
The consequences are that you can expose extension methods through a class or by a type.


Remember that the extension methods have lower priority than instance methods defined in the type. It means that, if you write one extension method with the same parameters to extend some behavior, you'll not be able to access it.


Implicit Typed Local Variable



This version of the C# language introduced the implicit type var. Every time a variable is defined with this type, the compiler will infer the type at compile time. 

Partial Methods



This was an extension of the feature regarding partial classes. Now, it's possible to have an implementation of one method in one part of the partial class, and the implementation in the other.

There are some rules to follow:
  • One of the partial methods must have no implementation;
  • It's not allowed access modifiers. Partial methods are private;
  • The private methods should return void;
  • A method defined as partial should have the equivalent in the other partial class;
  • Both signatures of partial methods should match.

Object and Collection Initializers

The object initializes simplifies the way you instantiate one object, passing the values directly in the moment of instance, rather than pass everything in the constructor, or attribute after, in the instance of the object.

Here we can see some examples of how to instantiate the Book class, using the normal way (new Book()), with a mixed strategy (using constructor to initialize one property) or with object initializers: 

---------------------------------------

Fabio Ono

No comments:

Post a Comment