note: part I of this posts located in
http://mstarek.blogspot.com/2008/10/whats-new-in-visual-c.html
In part one on this posts series we tried to summaries the new features of C# 2008, as (implicitly typed (var), Collection initialization, Extension Methods, Anonymous Types), we will continue in this post ISA,
5- Lambda Expressions:
Lambda expressions is a simple way to make delegate function , with simple and easy to read way and let the compiler do the work for you,
in the past (and still now if u want) we have to use delegate functions in the methods like
List<String> stLst = new List<String> stl = stLst.FindAll(delegate(string s) { returns.ToLower().StartsWith("a"); }); |
But in C# 2008, you can now type it simple as:
List<String> stLst = new List<String> stl = stLst.FindAll(x => (x.ToLower().StartsWith("a") && IsCorrect(x))); |
This is called Lambda Expressions, just another easy syntax to delegates, and has some useful usages.
The Syntax of lambda must contains "=>", and as shown
(input parameters) => expression |
This expression may have input parameter or parameters, or can have no parameters, the right expression makes the logic and return value of any type, for example the below statement returns Boolean value as:
(x, y) => x == y //1 (int x, string s) => s.Length > x //2 |
The above statements, in the first one the compiler detect automatically the type of input parameters, its inspect it automatically here, the second statement we add the type of parameters, if we know that the compiler can't detect it, and the third one with no input parameters, and the expression uses another function.
We can use lambda expression to make delegate as:
delegate … TestDelegate myDel = n => { string s = n + " " + "World";Console.WriteLine(s); }; |
Many Standard query operators have an input parameter whose type is one of the Func<(Of <(T, TResult>)>) family of generic delegates. The Func<(Of <(T, TResult>)>) delegates use type parameters to define the number and type of input parameters, and the return type of the delegate. Func delegates are very useful for encapsulating user-defined expressions that are applied to each element in a set of source data. For example, consider the following delegate type:
The delegate can be instantiated as Func<int,bool> myFunc where int is an input parameter and bool is the return value. The return value is always specified in the last type parameter. Func<int, string, bool> defines a delegate with two input parameters, int and string, and a return type of bool. The following Func delegate, when it is invoked, will return true or false to indicate whether the input parameter is equal to 5:
Func<int,bool> myFunc = x => x == 5; |
LINQ Usage:
|
6-Query Keywords (C# Reference)
This section contains the contextual keywords used in query expressions.
from : Specifies a data source and a range variable (similar to an iteration variable).
where : Filters source elements based on one or more Boolean expressions separated by logical AND and OR operators ( && or || ).
select : Specifies the type and shape that the elements in the returned sequence will have when the query is executed.
group : Groups query results according to a specified key value.
into : Provides an identifier that can serve as a reference to the results of a join, group or select clause.
orderby : Sorts query results in ascending or descending order based on the default comparer for the element type.
join : Joins two data sources based on an equality comparison between two specified matching criteria.
let : Introduces a range variable to store sub-expression results in a query expression.
in : Contextual keyword in a join clause.
on : Contextual keyword in a join clause.
equals : Contextual keyword in a join clause.
by : Contextual keyword in a group clause.
ascending : Contextual keyword in an orderby clause.
descending : Contextual keyword in an orderby clause.
7-Auto-Implement properties:
This is new feature in C# , this is just to make it easy coding properties,
string _thePropert; public string ThePropert { get { return_thePropert; } set { _thePropert =value; } } |
But with C# 2008 we make it as:
Public doubleTotalPurchases {get; set; }
{ get;private
{get; private set; } // read-only |
This is an easy way to code properties.
8-Partial classes and methods
It is possible to split the definition of a class or a struct, an interface or a method over two or more source files. Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled.
public partial class Employee { public void DoWork() { } } public partial class Employee { public void GoToLunch() { } } class Container { partial class Nested { void Test() { } } partial class Nested { void Test2() { } } } // Definition in file1.cs partial void onNameChanged(); // Implementation in file2.cs partial void onNameChanged() { // method body } |
It, just a summary for the C# new features, for more information click this linkhttp://msdn.microsoft.com/en-us/library/bb383815.aspx
Thank you,
note: part I of this posts located in
http://mstarek.blogspot.com/2008/10/whats-new-in-visual-c.html
No comments:
Post a Comment