C# features through examples

17
Advanced C#

Transcript of C# features through examples

Page 1: C# features through examples

Advanced C#

Page 2: C# features through examples

Initializer StudentName student2 = new StudentName

{

FirstName = “Foulen",

LastName = “Benfoulen",

};

List<string> lst = new List<string>()

{“etd1“, “etd2“, “etd3“, “etd4“ };

Page 3: C# features through examples

Anonymous Types

var variableTypeAnonyme = new { FirstName = "Flavien", Age = 23};

var variableTypeAnonyme = new { DateOfBirth = new DateTime(1984, 11, 15) };

var variableTypeAnonyme = 12;

var variableTypeAnonyme = "Flavien";

Page 4: C# features through examples

Extension methods

public static class Encodage{

public static string Crypte(string chaine){

returnConvert.ToBase64String(Encoding.Default.GetBytes(chaine));

}

public static string Decrypte(string chaine){

returnEncoding.Default.GetString(Convert.FromBase64String(chaine));

}}

Page 5: C# features through examples

Extension methodsstatic void Main(string[] args){string chaineNormale = "Bonjour";

string chaineCryptee = Encodage.Crypte(chaineNormale)

Console.WriteLine(chaineCryptee);

chaineNormale = Encodage.Decrypte(chaineCryptee);

Console.WriteLine(chaineNormale);}

Page 6: C# features through examples

Extension methods 2

public static class Encodage{

public static string Crypte ( this string chaine){

returnConvert.ToBase64String(Encoding.Default.GetBytes(chaine));

}

public static string Decrypte ( this string chaine){

returnEncoding.Default.GetString(Convert.FromBase64String(chaine));

}}

Page 7: C# features through examples

Extension methods 2

Page 8: C# features through examples

Delegate

Page 9: C# features through examples

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);

}}

Page 10: C# features through examples

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

}}

Page 11: C# features through examples

Using delegates

static void Main(string[] args){

int[] tableau = new int[] { 4, 1,10, 8, 5 };

new TrieurDeTableau().DemoTri(tableau);}

Page 12: C# features through examples

Lambda Expression

Page 13: C# features through examples

Delegate to lambda

DelegateTri tri = delegate(int[] leTableau){

Array.Sort(leTableau);};

DelegateTri tri = (leTableau) =>{

Array.Sort(leTableau);};

Page 14: C# features through examples

Lambda

List<int> list = new List<int>(new int[] { 2, -5, 45, 5 });var positiveNumbers = list.FindAll((int i) => i > 0);

LINQ

From sourceWhere conditionSelect variable

Page 15: C# features through examples

LINQclass 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);

}}}

Page 16: C# features through examples

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();

Page 17: C# features through examples

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();