var point = new { X = 3, Y = 5 };
var myCar = new { Color = "Red", Brand = "Tesla", TopSpeed = 270 };
Console.WriteLine("My car is a {0} {1}.", myCar.Color, myCar.Brand);
var p = new { X = 3, Y = 5 };
var q = new { X = 3, Y = 5 };
Console.WriteLine(p == q); // false
Console.WriteLine(p.Equals(q)); // true
var arr = new[]
{
new { X = 3, Y = 5 },
new { X = 1, Y = 2 },
new { X = 0, Y = 7 }
};
foreach (var item in arr)
{
Console.WriteLine("({0}, {1})", item.X, item.Y);
}
public delegate void SomeDelegate<T>(T item);
public static void Notify(int i) { … }
SomeDelegate<int> d = new SomeDelegate<int>(Notify);
SomeDelegate<int> d = Notify;
Func<string, int> predefinedIntParse = int.Parse;
int number = predefinedIntParse("50");
Action<object> predefinedAction = Console.WriteLine;
predefinedAction(1000);
class Counter {
public event EventHandler ThresholdReached;
protected virtual void OnThresholdReached(EventArgs e) {
if (this.ThresholdReached != null) {
this.ThresholdReached(this, e);
}
}
}
public class ThresholdReachedEventArgs : EventArgs
{
public int Threshold { get; set; }
public DateTime TimeReached { get; set; }
}
class Example {
static void Main() {
Counter counter = new Counter();
counter.ThresholdReached += CounterThresholdReached;
}
static void CounterThresholdReached(object sender, EventArgs e) {
Console.WriteLine("The threshold was reached.");
}
}
Func<bool> boolFunc = () => true;
Func<int, bool> intFunc = (x) => x < 10;
if (boolFunc() && intFunc(5))
{
Console.WriteLine("5 < 10");
}
public delegate bool Predicate <T>(T obj)
Action<int> act = (number) =>
{
Console.WrileLine(number);
}
act(10); // logs 10
Func<string, int, string> greet = (name, age) =>
{
return "Name: " + name + "Age: " + age;
}
Console.WriteLine(greet("Ivaylo", 10));
Book[] books = {
new Book { Title="LINQ in Action" },
new Book { Title="Extreme LINQ" } };
var titles = books
.Where(book => book.Title.Contains("Action"))
.Select(book => book.Title);
var titles = b in books
where b.Title.Contains("Action")
select b.Title;
List<Book> books = new List<Book>() {
new Book { Title="LINQ in Action" },
new Book { Title="Extreme LINQ" } };
var titles = books
.Where(book => book.Title.Contains("Action"))
.Select(book => book.Title);
var count = "Non-letter characters in this string: 8"
.Where(c => !Char.IsLetter(c))
.Count();
var count = c in "Non-letter characters in this string: 8"
where !Char.IsLetter(c)
select c.Count();
Console.WriteLine(count); // 8
double[] temperatures = { 28.0, 19.5, 32.3, 33.6, 26.5, 29.7 };
var highTempCount = temperatures.Count(p => p > 30);
var highTempCount =
(from p in temperatures
where p > 30
select p).Count();
Console.WriteLine(highTempCount); // 2
double[] temperatures = { 28.0, 19.5, 32.3, 33.6, 26.5, 29.7 };
var maxTemp = temperatures.Max();
var maxTemp =
(from p in temperatures
select p).Max();
Console.WriteLine(maxTemp); // 33.6