|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionThe C# language is getting more good looking than ever before. C# 3.0 is simple the definition of beauty. The new C# compiler is doing a lot of work for us so let us see how we can use it start using these beautiful features. C# 3.0 Feature ListLet us start with a very handy feature, "Automatic Properties" (I am not sure how this is called but this is how I like to call it :) ). Basically you can declare a property in C# without creating a data member explicitly, the compiler will go ahead and create it for us.
C# 3.0
As you can see I am not declaring a member; all you have to do is specify what you want with the property (readonly or not). Object Initializer In C# 2.0 whenever you had to construct an object and set it's initial state we used to overload the constructor and pass the property values from the constructor. C# 3.0 introduces a new way of doing this via Object Initializers. So if I had this class
C# 2.0
C# 3.0
Collection Initializers Collection Initializers are similar to Object Initializers yet these allow you to add items inside your collection. C# 2.0
C# 3.0
Dictionary Initializers As the name suggest this is a new way how you can add items in a IDictionary C# 2.0
C# 3.0
Extenstion Methods This is quite a cool new feature. Basically you can attach methods at compile time to specific types. Lets say for example you want to add a method to the string type. The string class is inside the System.dll and you do not have access to it's source code. What you can do is declare an extenstion method for the specific type (you can also create an extenstion method for an interface and every class that implements that interface will have the method). An extenstion method must be declared as a static method and inside a static class. You have to pass "this" in front of the argument for the method(the type of the argument must be the type you want to extend). Lets try it out
Now I can do the following code.
and the result will be "Hello Marlon" LINQ I will not go into much detail on this topic because it's quite a large subject. If you are interested, visit this url to get more info on LINQ. Basically LINQ is a declerative way how to query data. So instead of being imperative and using loops and if statments you can tell C# what you want and LINQ will go ahead and build that data for you. Let's take for example you have a list of strings and you want to get all strings that start with the letter "s".
C# 2.0
C# 3.0
The above code is equivalent to this
The Where and Select methods are Extenstion Method for the IEnumerable<T> interface that are declared in the Enumerable static class. The a => a.StartWith("t") syntax are lambda functions, we will explain this new feature next. Anonymous Types and implicit type variables. Anonymous types are basically types that you do not declare as classes in your code. Basically when you create an anonymous type the compiler will create a class for you (with a very strange name :) ). So you might be saying but if I do not know the name of the type how can I reference it and create a variable of that type. The answer is the implicit type variable aka var. The var is a new keyword in C# that the compiler will change into the name of the type. So YES you will not loose the static typing! You can also use the var in other scenarios to type in less characters when declaring a variable. The anonymous types are really handy when using linq queries to get a data structure out of the query as shown in the demo project (and even later on when discussing grouping in LINQ). So let us create an anonymous type.
Now let's use the anonymous types in a LINQ query to see it's potential
Quite cool ee.. :) Lambda Expressions In C# 2.0 anonymous methods where given birth. This was quite a cool feature because you do not have to create a method in your class if you are going to use it just once for an event handler or something similar(like a Predicate<T> for instance). Besides you can use any (there are some implications with this yet I will not talk about this today) variable in context of the anonymous method declaration. C# 2.0
C# 3.0 As you can see in the above example the lambda syntax is much cleaner. Basically with lambda you can specify what parameters you want to pass by saying x=> now you can do anything with x since x is the parameter that is being passed to you. If you need to pass more than one variable to the lambda you can do so by saying (x, y)=> and if you need to do more than one line of code in the body you can use the { } to do so. Partial Methods C# 3.0 introduces partial methods. Basically this is a way how you can define a method in a partial class and implement it in another partial class. This feature is more for designers. For instance the LINQ to SQL designer uses these feature a lot.
So I guess these are the new features (that I know of!!!) in C# 3.0. I did not go into much details on each feature since every feature deserves a post on itself. Yet as promised I will go into some more LINQ. Again I will do only basic things and I will not go into much details, I will only show how you can use these features. Sorting with LINQ You can sort a collection using LINQ very easily
As you can see the orderby keyword forces the collection to sort. (The sorting feature needs to be implemented by the LINQ provider. This sorting for LINQ to object is implemented for all collection) Grouping in LINQ Grouping is another cool feature in LINQ to Objects (I say LINQ to objects since there are more than one LINQ provider. There are LINQ to XML, LINQ to SQL and others... Yet I will only use LINQ to Objects in this post). For example imagine that you have a list of numbers and you want to check which one have a remainder when divided by 2.
As you can see here I am using an anonymous type to create a group. the g.Key will be the different results of the group predicate ( Joins in LINQ Joins are very powerful for when you have relational data. For example imagine that you have a list of customers and a list of orders for those customers. You want to find out the total bill for a customer by summing up all orders for the customer. You can do this very easily with LINQ Let us prepare some sample data Now let us query this data to join the 2 lists by their relation
Let us print the result....
f ConclusionC# 3.0 is really powerful and makes our life much easier (specially with LINQ). When you combine all these cool features together you get quite a neat way how to implement nice code that is readable and easy to understand. Again I want to point out that I did not go into much detail since the post would have been to large. Hope you enjoyed it...
|
||||||||||||||||||||||