This morning I was in the Israeli office of some small start-up company from Seattle to listen to Pavel Yosifovich from "Hi-Tech College". The guy showed some interesting stuff about C# 3. Some of them are well known, the others are less known and very useful.
· Implicitly Typed Local Variables
Just like in javascript :
var keys = data.Keys; // Dictionary
public class Person { // C# 3.0
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
· Object Initializers
Person p = new Person() {
FirstName = "Bart", LastName = "Simpson", Age = 12
};
var people = new List
new Person() { FirstName = "Bart", LastName = "Simpson", Age = 12 },
new Person() { FirstName = "Clark", LastName = "Kent", Age = 35 },
new Person() { FirstName = "Peter", LastName = "Parker", Age = 30 }
};
ar people = new[] {
new { FirstName = "Clark", LastName = "Kent", Age = 36 },
new { FirstName = "Peter", LastName = "parker", Age = 26 },
new { FirstName = "Bart", LastName = "Simpson", Age = 11 }
};
foreach (var i in people)
Console.WriteLine("{0} {1} ({2})", i.FirstName, i.LastName, i.Age);
· And my favorite : Extension Methods
public static class MyExtensions {
public static string UpperLower(this string str, bool upperFirst) {
StringBuilder newString = new StringBuilder(str.Length);
for (int i = 0; i <>
newString.Append(upperFirst ? char.ToUpper(str[i]) :
char.ToLower(str[i]));
upperFirst = !upperFirst;
}
return newString.ToString();
}
}
string input = Console.ReadLine();
Console.WriteLine("calling extension method for {0}: {1}", input,
input.UpperLower(true));
The LINQ Project is a codename for a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. It extends C# and Visual Basic with native language syntax for queries and provides class libraries to take advantage of these capabilities.
.. and what does it mean ?
Does it mean that we can finally quit using SQL. We are programmes! We know objects and hate sql!!!
This is how the famous Northwind database looks like
..each box ,is translated to a class and then translated to something like this
[Table(Name="dbo.Categories")]
public partial class Category : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _CategoryID;
private string _CategoryName;
private string _Description;
private System.Data.Linq.Binary _Picture;
private EntitySet
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnCategoryIDChanging(int value);
partial void OnCategoryIDChanged();
partial void OnCategoryNameChanging(string value);
partial void OnCategoryNameChanged();
partial void OnDescriptionChanging(string value);
partial void OnDescriptionChanged();
partial void OnPictureChanging(System.Data.Linq.Binary value);
partial void OnPictureChanged();
#endregion
public Category()
{
this._Products = new EntitySet
OnCreated();
}
and finally might be used like this
NorthwindDataContext ctx = new NorthwindDataContext();
ctx.DeferredLoadingEnabled = false;
DataLoadOptions opts = new DataLoadOptions();
opts.LoadWith
ctx.LoadOptions = opts;
-------
var categories = from c in ctx.Categories
select c;
foreach (var category in categories) {
Console.WriteLine(category.Products.Count());
}
var result = ctx.SalesByCategory("Beverages", "1998");
foreach(var r in result)
Console.WriteLine("{0}, {1}", r.ProductName, r.TotalPurchase);
No comments:
Post a Comment