wip
This commit is contained in:
@@ -1,39 +1,20 @@
|
|||||||
# Petite liste d’exemples de styles modernes en C# (C# 8 → C# 12)
|
# Petite liste d’exemples de styles modernes en C# (C# 8 → C# 12)
|
||||||
|
|
||||||
## 🔹 1. Opérateur ternaire (version concise)
|
## 🔹 1. Opérateur ternaire (version concise) (if/else)
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
string status = string.Empty;
|
|
||||||
If(age >= 18)
|
|
||||||
{
|
|
||||||
statut = "Majeur"
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
statut = "Mineur"
|
|
||||||
}
|
|
||||||
|
|
||||||
// ou
|
|
||||||
var statut = age >= 18 ? "Majeur" : "Mineur";
|
var statut = age >= 18 ? "Majeur" : "Mineur";
|
||||||
```
|
```
|
||||||
|
|
||||||
Avec expression conditionnelle imbriquée :
|
|
||||||
|
|
||||||
```csharp
|
|
||||||
var note = score switch
|
|
||||||
{
|
|
||||||
>= 16 => "Très bien",
|
|
||||||
>= 12 => "Bien",
|
|
||||||
>= 10 => "Passable",
|
|
||||||
_ => "Insuffisant"
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 🔹 2. `switch` expression (moderne)
|
## 🔹 2. `switch` expression (moderne)
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
|
// Le _ remplace default.
|
||||||
|
|
||||||
|
// Exemple simple
|
||||||
|
var codeErreur = 500;
|
||||||
string message = codeErreur switch
|
string message = codeErreur switch
|
||||||
{
|
{
|
||||||
404 => "Introuvable",
|
404 => "Introuvable",
|
||||||
@@ -41,8 +22,81 @@ string message = codeErreur switch
|
|||||||
500 => "Erreur serveur",
|
500 => "Erreur serveur",
|
||||||
_ => "Erreur inconnue"
|
_ => "Erreur inconnue"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Avec enum
|
||||||
|
enum Statut { Actif, Inactif, Suspendu }
|
||||||
|
Statut statut = Statut.Actif;
|
||||||
|
string message = statut switch
|
||||||
|
{
|
||||||
|
Statut.Actif => "Utilisateur actif",
|
||||||
|
Statut.Inactif => "Utilisateur inactif",
|
||||||
|
Statut.Suspendu => "Compte suspendu",
|
||||||
|
_ => "Statut inconnu"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Avec conditions (patterns relationnels)
|
||||||
|
int age = 20;
|
||||||
|
string categorie = age switch
|
||||||
|
{
|
||||||
|
< 12 => "Enfant",
|
||||||
|
>= 12 and < 18 => "Adolescent",
|
||||||
|
>= 18 => "Adulte"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Avec expression conditionnelle imbriquée :
|
||||||
|
var note = score switch
|
||||||
|
{
|
||||||
|
>= 16 => "Très bien",
|
||||||
|
>= 12 => "Bien",
|
||||||
|
>= 10 => "Passable",
|
||||||
|
_ => "Insuffisant"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Avec when (pattern guard)
|
||||||
|
int note = 15;
|
||||||
|
string resultat = note switch
|
||||||
|
{
|
||||||
|
int n when n >= 16 => "Très bien",
|
||||||
|
int n when n >= 12 => "Bien",
|
||||||
|
int n when n >= 10 => "Passable",
|
||||||
|
_ => "Échec"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Switch sur string
|
||||||
|
string pays = "FR";
|
||||||
|
string langue = pays switch
|
||||||
|
{
|
||||||
|
"FR" => "Français",
|
||||||
|
"EN" => "Anglais",
|
||||||
|
"ES" => "Espagnol",
|
||||||
|
_ => "Inconnue"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Switch avec tuple
|
||||||
|
int x = 0, y = 5;
|
||||||
|
string position = (x, y) switch
|
||||||
|
{
|
||||||
|
(0, 0) => "Origine",
|
||||||
|
(0, _) => "Axe Y",
|
||||||
|
(_, 0) => "Axe X",
|
||||||
|
_ => "Plan"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Switch avec type pattern
|
||||||
|
object obj = "Bonjour";
|
||||||
|
string description = obj switch
|
||||||
|
{
|
||||||
|
int i => $"Entier : {i}",
|
||||||
|
string s => $"Chaîne : {s}",
|
||||||
|
null => "Null",
|
||||||
|
_ => "Autre type"
|
||||||
|
};
|
||||||
|
|
||||||
|
// etc
|
||||||
|
// ...
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 🔹 3. Parcourir un tableau / liste
|
## 🔹 3. Parcourir un tableau / liste
|
||||||
|
|||||||
Reference in New Issue
Block a user