C# features through examples
-
Upload
zayen-chagra -
Category
Technology
-
view
69 -
download
0
Transcript of C# features through examples
Advanced C#
Initializer StudentName student2 = new StudentName
{
FirstName = “Foulen",
LastName = “Benfoulen",
};
List<string> lst = new List<string>()
{“etd1“, “etd2“, “etd3“, “etd4“ };
Anonymous Types
var variableTypeAnonyme = new { FirstName = "Flavien", Age = 23};
var variableTypeAnonyme = new { DateOfBirth = new DateTime(1984, 11, 15) };
var variableTypeAnonyme = 12;
var variableTypeAnonyme = "Flavien";
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));
}}
Extension methodsstatic void Main(string[] args){string chaineNormale = "Bonjour";
string chaineCryptee = Encodage.Crypte(chaineNormale)
Console.WriteLine(chaineCryptee);
chaineNormale = Encodage.Decrypte(chaineCryptee);
Console.WriteLine(chaineNormale);}
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));
}}
Extension methods 2
Delegate
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);
}}
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
}}
Using delegates
static void Main(string[] args){
int[] tableau = new int[] { 4, 1,10, 8, 5 };
new TrieurDeTableau().DemoTri(tableau);}
Lambda Expression
Delegate to lambda
DelegateTri tri = delegate(int[] leTableau){
Array.Sort(leTableau);};
DelegateTri tri = (leTableau) =>{
Array.Sort(leTableau);};
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
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);
}}}
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();
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();