![]() |
От: |
AndrewVK
|
http://blogs.rsdn.org/avk |
Дата: | 14.09.05 16:24 | ||
Оценка: | 400 (43) +1 |
var i = 5;
var s = "Hello";
var d = 1.0;
var numbers = new int[] {1, 2, 3};
var orders = new Dictionary<int,Order>();
var x; // Error, no initializer to infer type from
var y = {1, 2, 3}; // Error, collection initializer not permitted
var z = null; // Error, null type not permitted
int[] numbers = { 1, 3, 5, 7, 9 };
foreach (var n in numbers) Console.WriteLine(n);
namespace Acme.Utilities
{
public static class Extensions
{
public static int ToInt32(this string s) {
return Int32.Parse(s);
}
public static T[] Slice<T>(this T[] source, int index, int count) {
if (index < 0 || count < 0 || source.Length – index < count)
throw new ArgumentException();
T[] result = new T[count];
Array.Copy(source, index, result, 0, count);
return result;
}
}
}
using Acme.Utilities;
...
string s = "1234";
int i = s.ToInt32(); // Same as Extensions.ToInt32(s)
int[] digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int[] a = digits.Slice(4, 3); // Same as Extensions.Slice(digits, 4, 3)
delegate R Func<A,R>(A arg);
...
Func<int,int> f1 = x => x + 1; // Ok
Func<int,double> f2 = x => x + 1; // Ok
Func<double,int> f3 = x => x + 1; // Error
var a = new Point { X = 0, Y = 1 };
// which has the same effect as
// var a = new Point();
// a.X = 0;
// a.Y = 1;
var r = new Rectangle {
P1 = new Point { X = 0, Y = 1 },
P2 = new Point { X = 2, Y = 3 }
};
// which has the same effect as
// var r = new Rectangle();
// var __p1 = new Point();
// __p1.X = 0;
// __p1.Y = 1;
// r.P1 = __p1;
// var __p2 = new Point();
// __p2.X = 2;
// __p2.Y = 3;
// r.P2 = __p2;
var contacts = new List<Contact> {
new Contact {
Name = "Chris Smith",
PhoneNumbers = { "206-555-0101", "425-882-8080" }
},
new Contact {
Name = "Bob Harris",
PhoneNumbers = { "650-555-0199" }
}
};
// which has the same effect as
// var contacts = new List<Contact>();
// var __c1 = new Contact();
// __c1.Name = "Chris Smith";
// __c1.PhoneNumbers.Add("206-555-0101");
// __c1.PhoneNumbers.Add("425-882-8080");
// contacts.Add(__c1);
// var __c2 = new Contact();
// __c2.Name = "Bob Harris";
// __c2.PhoneNumbers.Add("650-555-0199");
// contacts.Add(__c2);
var dict = new Dictionary<string, int> {
new KeyValuePair<string, int>("one", 1),
new KeyValuePair<string, int>("two", 2),
new KeyValuePair<string, int>("three", 3)
}
var p1 = new { Name = "Lawnmower", Price = 495.00 };
var p2 = new { Name = "Shovel", Price = 26.95 };
p1 = p2;
// Компилятор неявно создает следующий класс:
// class __Anonymous1
{
private string _name;
private double _price;
public string Name { get {return _name;} set {_name = value;}}
public double Price { get {return _price;} set {_price = value;}}
}
var a = new[] { 1, 10, 100, 1000 }; // int[]
var b = new[] { 1, 1.5, 2, 2.5 }; // double[]
var c = new[] { "hello", null, "world” }; // string[]
var d = new[] { 1, "one", 2, "two" }; // Error
from c in customers
where c.City == "London"
from o in c.Orders
where o.OrderDate.Year == 2005
select new { c.Name, o.OrderID, o.Total }
customers.
Where(c => c.City == "London").
SelectMany(c =>
c.Orders.
Where(o => o.OrderDate.Year == 2005).
Select(o => new { c.Name, o.OrderID, o.Total })
)
delegate R Func<A,R>(A arg);
class C<T>
{
public C<T> Where(Func<T,bool> predicate);
public C<S> Select<S>(Func<T,S> selector);
public C<S> SelectMany<S>(Func<T,C<S>> selector);
public O<T> OrderBy<K>(Func<T,K> keyExpr);
public O<T> OrderByDescending<K>(Func<T,K> keyExpr);
public C<G<K,T>> GroupBy<K>(Func<T,K> keyExpr);
public C<G<K,E>> GroupBy<K,E>(Func<T,K> keyExpr, Func<T,E> elemExpr);
}
class O<T> : C<T>
{
public O<T> ThenBy<K>(Func<T,K> keySelector);
public O<T> ThenByDescending<K>(Func<T,K> keySelector);
}
class G<K,T>
{
public K Key { get; }
public C<T> Group { get; }
}
Func<int,int> f = x => x + 1; // Code
Expression<Func<int,int>> e = x => x + 1; // Data
Вот так — прозрачные и явные. Попой чую, что для каких то особо хитрых извратств в рантайме (скорее всего связанных с п.7), но чего то вечером пример придумать не могу.Expression trees are efficient in-memory data representations of lambda expressions and make the structure of the expression transparent and explicit.