Anúncio
Anúncio

Mais conteúdo relacionado

Anúncio

Último(20)

Anúncio

C# features through examples

  1. Advanced C#
  2. Initializer StudentName student2 = new StudentName { FirstName = “Foulen", LastName = “Benfoulen", }; List<string> lst = new List<string>() {“etd1“, “etd2“, “etd3“, “etd4“ };
  3. Anonymous Types var variableTypeAnonyme = new { FirstName = "Flavien", Age = 23}; var variableTypeAnonyme = new { DateOfBirth = new DateTime(1984, 11, 15) }; var variableTypeAnonyme = 12; var variableTypeAnonyme = "Flavien";
  4. Extension methods public static class Encodage { public static string Crypte(string chaine) { return Convert.ToBase64String(Encoding.Default.GetBytes(chaine)); } public static string Decrypte(string chaine) { return Encoding.Default.GetString(Convert.FromBase64String(chaine)); } }
  5. Extension methods static void Main(string[] args) { string chaineNormale = "Bonjour"; string chaineCryptee = Encodage.Crypte(chaineNormale) Console.WriteLine(chaineCryptee); chaineNormale = Encodage.Decrypte(chaineCryptee); Console.WriteLine(chaineNormale); }
  6. Extension methods 2 public static class Encodage { public static string Crypte ( this string chaine) { return Convert.ToBase64String(Encoding.Default.GetBytes(chaine)); } public static string Decrypte ( this string chaine) { return Encoding.Default.GetString(Convert.FromBase64String(chaine)); } }
  7. Extension methods 2
  8. Delegate
  9. public class TrieurDeTableau { private delegate void DelegateTri(int[] tableau); private void TriAscendant (int[] tableau) { Array.Sort(tableau); } private void TriDescendant (int[] tableau) { Array.Sort(tableau); Array.Reverse(tableau); } }
  10. public class TrieurDeTableau { […Code supprimé pour plus de clarté…] public void DemoTri(int[] tableau) { DelegateTri tri = TriAscendant; tri(tableau); //affichage tri = TriDescendant; tri(tableau); //affichage }}
  11. Using delegates static void Main(string[] args) { int[] tableau = new int[] { 4, 1,10, 8, 5 }; new TrieurDeTableau().DemoTri(tableau); }
  12. Lambda Expression
  13. Delegate to lambda DelegateTri tri = delegate(int[] leTableau) { Array.Sort(leTableau); }; DelegateTri tri = (leTableau) => { Array.Sort(leTableau); };
  14. Lambda List<int> list = new List<int>(new int[] { 2, -5, 45, 5 }); var positiveNumbers = list.FindAll((int i) => i > 0); LINQ From source Where condition Select variable
  15. LINQ class IntroToLINQ{ static void Main() { // The Three Parts of a LINQ Query: // 1. Data source. int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 }; // 2. Query creation. // numQuery is an IEnumerable<int> var numQuery = from num in numbers where (num % 2) == 0 select num; // 3. Query execution. foreach (int num in numQuery) { Console.Write("{0,1} ", num); }}}
  16. int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 }; var evenNumQuery = from num in numbers where (num % 2) == 0 select num; int evenNumCount = evenNumQuery.Count();
  17. List<int> numQuery2 = (from num in numbers where (num % 2) == 0 select num).ToList(); // or like this: // numQuery3 is still an int[] var numQuery3 = (from num in numbers where (num % 2) == 0 select num).ToArray();
Anúncio