using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
class Person {
int _id;
int _idRole;
string _lastName;
string _firstName;
public int ID {
get { return _id; }
set { _id = value; }
}
public int IDRole {
get { return _idRole; }
set { _idRole = value; }
}
public string LastName {
get { return _lastName; }
set { _lastName = value; }
}
public string FirstName {
get { return _firstName; }
set { _firstName = value; }
}
}
class Program {
static void Main(string[] args) {
List<Person> people = new List<Person> {
new Person { ID = 1, IDRole = 1, LastName = "A", FirstName = "B"},
new Person { ID = 2, IDRole = 2, LastName = "G", FirstName = "T"}
};
Func<Person, bool> filter = delegate(Person p) { return p.ID == 1; };
var query = people
.Where(filter)
.Select(p => new { p.FirstName, p.LastName });
}
}
LINQ
Object Initializer
using System;
using System.Collections.Generic;
using System.Diagnostics;
class MyPC {
public Int32 Id { get; set; }
public Int64 Memory { get; set; }
public String Name { get; set; }
}
public class MainClass {
static void Main(string[] args) {
var processes = new List<MyPC>();
foreach (var process in Process.GetProcesses()) {
processes.Add(new MyPC {
Id = process.Id,
Name = process.ProcessName,
Memory = process.WorkingSet64
});
}
}
}
Extension Methods Discoverability
using System;
class Class1 {
}
class Class2 {
public void Method1(string s) {
Console.WriteLine("Class2.Method1");
}
}
class Class3 {
public void Method1(object o) {
Console.WriteLine("Class3.Method1");
}
}
class Class4 {
public void Method1(int i) {
Console.WriteLine("Class4.Method1");
}
}
static class Extensions {
static public void Method1(this object o, int i) {
Console.WriteLine("Extensions.Method1");
}
static void Main() {
new Class1().Method1(12); // Extensions.Method1 is called
new Class2().Method1(12); // Extensions.Method1 is called
new Class3().Method1(12); // Class3.Method1 is called
new Class4().Method1(12); // Class4.Method1 is called
}
}
Extension method for integer
using System;
static class MainClass {
public static int ConvertToBase(this int i, int baseToConvertTo) {
if (baseToConvertTo < 2 || baseToConvertTo > 10)
throw new ArgumentException(“Value cannot be converted to base ” + baseToConvertTo.ToString());
int result = 0;
int iterations = 0;
do {
int nextDigit = i % baseToConvertTo;
result += nextDigit * (int)Math.Pow(10, iterations);
iterations++;
i /= baseToConvertTo;
}
while (i != 0);
return result;
}
static void Main() {
try {
int x = 591;
for (int i = 2; i <= 10; i++) {
Console.WriteLine("{0} in base {1} is {2}", x, i, x.ConvertToBase(i));
}
} catch (Exception ex) {
Console.WriteLine("Exception: {0}", ex.Message);
}
}
}
[/csharp]
Add Extension method
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
public static class Extensions {
public static string SpaceToUnderscore(this string source) {
char[] cArray = source.ToCharArray();
string result = null;
foreach (char c in cArray) {
if (Char.IsWhiteSpace(c))
result += "_";
else
result += c;
}
return result;
}
}
class Program {
static void Main(string[] args) {
string s = "This is a test";
Console.WriteLine(s.SpaceToUnderscore());
}
}
Except Prototype
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
public static void Main() {
string[] presidents = {"G", "H", "a", "H", "over", "Jack"};
IEnumerable<string> processed = presidents.Take(4);
IEnumerable<string> exceptions = presidents.Except(processed);
foreach (string name in exceptions)
Console.WriteLine(name);
}
}
Use Except to print the first character of product names that aren't also the first character of customer names.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MainClass {
public static void Main() {
List
List
var productFirstChars =
from p in products
select p.ProductName[0];
var customerFirstChars =
from c in customers
select c.CompanyName[0];
var productOnlyFirstChars = productFirstChars.Except(customerFirstChars);
Console.WriteLine(“First letters from Product names, but not from Customer names:”);
foreach (var ch in productOnlyFirstChars) {
Console.WriteLine(ch);
}
}
static List
List
empTree.Add(new Product { ProductName = “A”, Category = “O”, UnitPrice = 12, UnitsInStock = 5, Total = 36, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
empTree.Add(new Product { ProductName = “B”, Category = “O”, UnitPrice = 2, UnitsInStock = 4, Total = 35, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
empTree.Add(new Product { ProductName = “C”, Category = “O”, UnitPrice = 112, UnitsInStock = 3, Total = 34, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
empTree.Add(new Product { ProductName = “D”, Category = “O”, UnitPrice = 112, UnitsInStock = 0, Total = 33, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
empTree.Add(new Product { ProductName = “E”, Category = “O”, UnitPrice = 1112, UnitsInStock = 2, Total = 32, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
empTree.Add(new Product { ProductName = “F”, Category = “O”, UnitPrice = 11112, UnitsInStock = 0, Total = 31, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
return empTree;
}
static List
List
empTree.Add(new Product { ProductName = “A”, Category = “O”, UnitPrice = 12, UnitsInStock = 5, Total = 36, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
empTree.Add(new Product { ProductName = “B”, Category = “O”, UnitPrice = 2, UnitsInStock = 4, Total = 35, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
empTree.Add(new Product { ProductName = “C”, Category = “O”, UnitPrice = 112, UnitsInStock = 3, Total = 34, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
empTree.Add(new Product { ProductName = “D”, Category = “O”, UnitPrice = 112, UnitsInStock = 0, Total = 33, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
empTree.Add(new Product { ProductName = “E”, Category = “O”, UnitPrice = 1112, UnitsInStock = 2, Total = 32, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
empTree.Add(new Product { ProductName = “F”, Category = “O”, UnitPrice = 11112, UnitsInStock = 0, Total = 31, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
List
l.Add(new Customer { CompanyName = “A”, Region = “R1”, UnitsInStock = 1, Orders = empTree, CustomerId = 0 });
l.Add(new Customer { CompanyName = “B”, Region = “R2”, UnitsInStock = 2, Orders = empTree, CustomerId = 1 });
l.Add(new Customer { CompanyName = “C”, Region = “R3”, UnitsInStock = 3, Orders = empTree, CustomerId = 2 });
l.Add(new Customer { CompanyName = “D”, Region = “R4”, UnitsInStock = 4, Orders = empTree, CustomerId = 3 });
l.Add(new Customer { CompanyName = “E”, Region = “R5”, UnitsInStock = 5, Orders = empTree, CustomerId = 4 });
return l;
}
}
class Customer : IComparable
public string CompanyName { get; set; }
public string Region { get; set; }
public List
public int UnitsInStock { get; set; }
public int CustomerId { get; set; }
public override string ToString() {
return String.Format(“Id: {0}, Name: {1}, Region: {3}”, this.CustomerId, this.CompanyName, this.Region);
}
int IComparable
if (other == null)
return 1;
if (this.CustomerId > other.CustomerId)
return 1;
if (this.CustomerId < other.CustomerId)
return -1;
return 0;
}
}
class Product : IComparable
public string ProductName { get; set; }
public string Category { get; set; }
public int UnitPrice { get; set; }
public int UnitsInStock { get; set; }
public int Total { get; set; }
public DateTime OrderDate { get; set; }
public int Id { get; set; }
public override string ToString() {
return String.Format(“Id: {0}, Name: {1} , Category: {3}”, this.Id, this.ProductName, this.Category);
}
int IComparable
if (other == null)
return 1;
if (this.Id > other.Id)
return 1;
if (this.Id < other.Id) return -1; return 0; } } [/csharp]