| Décembre 2009 | ||||||||||
| L | M | M | J | V | S | D | ||||
| 1 | 2 | 3 | 4 | 5 | 6 | |||||
| 7 | 8 | 9 | 10 | 11 | 12 | 13 | ||||
| 14 | 15 | 16 | 17 | 18 | 19 | 20 | ||||
| 21 | 22 | 23 | 24 | 25 | 26 | 27 | ||||
| 28 | 29 | 30 | 31 | |||||||
|
||||||||||
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ExtensionMethodsTest
{
public static class ExtensionMethods
{
public static string MySubstring(this string
s, int nStart, int nEnd)
{
string sResult = string.Empty;
if (nEnd <= nStart || nStart > s.Length || nEnd
> s.Length)
{ }
else
{
int nLength = nEnd -
nStart;
sResult = s.Substring(nStart, nLength);
}
return sResult;
}
}
}
|
|
string s = "bonjour à tous";
MessageBox.Show( s.MySubstring(10,
14));
|
|
// Initialisateur d'objet
Contact oContact = new Contact {
ContactName = "Romagny", ContactFirstName = "Jérôme" };
List<Contact> oContactList = new List<Contact> {
new Contact {
ContactName = "Romagny", ContactFirstName = "Jérôme" },
new Contact {
ContactName = "Bellin", ContactFirstName = "Marie" },
new Contact {
ContactName = "While", ContactFirstName = "Dany" },
};
|
|
Contact oSelectedContact = oContactList.First((Contact c) =>
c.ContactName.StartsWith("B"));
|
|
// point
particulier
// il faut créer un objet à chaque fois
// ainsi si je réutilise l'object Contact
// 1 seul contact sera ajouté en base de données
Contact oContact = new Contact();
oContact.ContactFirstName = "Dany";
oContact.ContactName = "While";
oContacts.Add(oContact);
oContact.ContactFirstName = "Lord Bret";
oContact.ContactName = "Sainclair";
oContacts.Add(oContact);
oContactDataContext.SubmitChanges();
|
|
// Par contre en créant 2 objets
// la mise à jour ajoutera bien des lignes à la table
contact
Contact oContact = new Contact();
Contact oContact2 = new Contact();
oContact.ContactFirstName = "Dany";
oContact.ContactName = "While";
oContacts.Add(oContact);
oContactDataContext.Contacts.Add(oContact);
oContact2.ContactFirstName = "Lord Bret";
oContact2.ContactName = "Sainclair";
oContacts.Add(oContact2);
oContactDataContext.SubmitChanges();
|
|
// une autre écriture tirant partie de C# 3.0
oContacts.Add(new Contact { ContactName = "Romagny",
ContactFirstName = "Jérôme" });
oContacts.Add(new Contact { ContactName = "Bellin",
ContactFirstName = "Marie" });
oContactDataContext.SubmitChanges();
|
|
// OU
// Supprimera 3 contacts
var Query =
from oContact in oContacts
where oContact.ContactID == 13 ||
oContact.ContactID == 14 || oContact.ContactID == 17
select
oContact;
|
|
// ET
// Query est une collection de collection
// tous les contacts respectant la condition seront
supprimés
var Query =
from oContact in oContacts
where
oContact.ContactFirstName == "Lord Bret" && oContact.ContactName == "Sainclair"
select oContact;
oContacts.RemoveAll(Query.ToList<Contact>());
oContactDataContext.SubmitChanges();
|
|
var Query =
(from oContact in oContacts
where oContact.ContactID == Convert.ToInt32(dataGridView1.CurrentRow.Cells["ContactID"].Value)
select oContact).First();
|
|
Contact oContact = new Contact();
oContact.ContactName = "Romagny";
MessageBox.Show(oContact.ContactName);
|
|
private string _ContactName;
public string ContactName
{
get;
set;
}
|
Un webcast C# 3.0
Encore un petit webcast sur C# 3.0 à suivre :p
certes un peu ancien mais bon il y a toujours des petits détails à apprendre
http://www.microsoft.com/france/events/event.aspx?EventID=1032300126
C# 3.0 et Linq
Deux articles de Thomas Lebrun sur C# 3.0 et Linq (excellents tant au niveau de la clarté des explications que de l'approche)
[C#] Rappel: Les nouveautés de C# 3
http://blogs.codes-sources.com/tom/archive/2007/04/12/c-rappel-les-nouveaut-s-de-c-3.aspx
C# 3 et LINQ
http://morpheus.developpez.com/linq/
et pendant que j'y suis >
Focus sur l'ObjectDataProvider de WPF