Friday, August 1, 2008

C# Anonymous Methods & The Action Object

Matthew is really great :-)

C# Anonymous Methods & The Action Object

Introduction to Functional Programming in C#.

Very Nice article from Matthew Cochran.

Introduction to Functional Programming in C#.

List<T>.foreach

Here is a sample code on how to use 'foreach' on 'generic list'
Notice the call to the anonymous function


class Employee
{
private string _nom;
private double _salary;

public Employee()
{
}

public Employee(string nom, double salary )
{
_nom = nom;
_salary = salary;

}

public string Nom
{
get {return _nom; }
set { _nom = value;}

}

public double Salary
{
get { return _salary;}
set { _salary = value; }
}
}



class Program
{

public static void incSalary(Employee p)
{
p.Salary += 100;
Console.WriteLine(p.Nom + Convert.ToString(p.Salary));
}

public static void Main(string[] args)
{
List lstEmployee = new List();

lstEmployee.Add(new Employee("Paul",3300));
lstEmployee.Add(new Employee("Raphael",2500));

lstEmployee.ForEach(incSalary);

lstEmployee.ForEach(delegate(Employee p){Console.WriteLine(p.Nom);});

Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}

Wednesday, July 30, 2008

LINQ - Accés aux données

Great article from Julien Corioland

Visual Studio 2008 : Les nouveautés de l’accès aux données avec LINQ !

LINQ - C#

This article explains compares
Lazy Loading et Agressive Loading
(in French)

Lazy Loading et Agressive Loading

LINQ - Select using Class

Beth Massi shows a way to project select data in class instances
Here the c# equivalent


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FileSystem
{
public partial class Form1 : Form
{
class MyFile
{
private System.DateTime _creationTime;
private string _name;

public System.DateTime CreationTime
{
get {return _creationTime;}
set { _creationTime = value; }
}

public string Name
{
get { return _name; }
set { _name = value; }
}
}

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
DirectoryInfo root = new DirectoryInfo(".");

var myFiles = from FileInfo f in root.GetFiles()
select new MyFile { Name = f.Name,
CreationTime = f.CreationTime
};

dataGridView1.DataSource = myFiles.ToList();

}
}
}

FileSystem in C#

Here is a way to retrieve file info in C#
using LINQ syntax.
Feel free to comment.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FileSystem
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
DirectoryInfo root = new DirectoryInfo(".");
var files = from FileInfo f in root.GetFiles()
select new { FileName = f.Name, LengthFile = f.Length };


dataGridView1.DataSource = files.ToList();

}
}
}

Tuesday, July 29, 2008

LINQ - Operators

Here is the main list of LINQ operators
Thanks >Kader Yildirim



Restriction operators
Where

Projection operators
Select
SelectMany

Partitioning operators
Take
Skip
TakeWhile
SkipWhile

Ordering operators
OrderBy
OrderByDescending
ThenBy
ThenByDescending
Reverse

Grouping operators
GroupBy
Joining operators
Join
GroupJoin

Set operators
Distinct
Union
Intersect
Except

Conversion operators
Cast
ToArray
ToList
ToDictionary
OfType
ToLookup
ToSequence

Element operators
DefaultEmpty
First
FirstOrDefault
Last
LastOrDefault
ElementAt
ElementAtOrDefault
Single
SingleOrDefault

Generation operators
Empty
Range
Repeat
Quantifiers
Any
All
Contains

Aggregate operators
Aggregate
Count
LongCount
Sum
Min
Max
Average
Fold

Miscellaneous operators
Concat
EqualAll
Custom Sequence operators
Combine
Query Execution
Deferred
Immediate
QueryReuse

LINQ - Articles in french

Some french articles using LINQ

LINQ à 360 degré

Functional programming - C#

Here is a series of articles concerning functional in C#

A C# library to write functional code