C# langage & syntaxe

27
C#

description

Visual Studio; .NET; C#; Syntaxe; Langage

Transcript of C# langage & syntaxe

C#

Dans ce module, nous allons voir :

Leçon 1 : Langage & Syntaxe.

Démonstration.

Leçon 1 : Langage & Syntaxe.

Leçon 1 : Langage & Syntaxe.

1. La casse.

2. La nomination.

3. Types de données.

4. Déclarations et assignations.

5. Fin d’instruction.

6. Blocs d’instructions.

7. Utilisation des parenthèses () et des crochets [].

8. Opérateurs.

9. Instructions conditionnelles.

10. Gestion des exceptions.

11. Envoi des paramètres.

12. Mots-clés.

Leçon 1 : Langage & Syntaxe.

1. La casse.

• C# respecte la casse, au contraire du VB.NET qui ne la respecte pas.

[C#]

Int16 int16;

[Vb.NET]

Dim integer As Integer

Déclaration

Erreur : Mot clé non valide en tant qu'identificateur.

Leçon 1 : Langage & Syntaxe.

2. La nomination.

Nom : Camion.

Attributs :1. Nombre de roue.

Opérations :1. Démarrer.

Nom : Camion.

Attributs :1. nombreRoue.

Opérations :1. demarrer.

Le cahier des charges ou autres. La classe.

Leçon 1 : Langage & Syntaxe.

2. La nomination.

• Utiliser la capitalisation (Pascal Case) pour le noms de classe:

• La première lettre de chaque mot en majuscule.

• Utiliser la capitalisation (Camel Case) pour les noms attributs et des opérations:

• La première lettre de chaque mot est en majuscule, à l’exception du premier mot qu’est en minuscule.

• Éliminer les espaces.

• N’utilisait jamais des caractères accentués (é, à, ç…etc).

• Utiliser les vrais noms.

Leçon 1 : Langage & Syntaxe.

3. Types de données.

.NET C# VB.NET Type Plage

System.Byte byte Byte Entier non signé 0 à 255

System.SByte sbyte - Entier signé - 128 à 127

System.Int32 int Integer Entier signé - 2 147 483 648 à 2 147 483 647

System.UInt32 uint - Entier non signé 0 à 4294967295

System.Int16 short Short Entier signé - 32 768 à 32 767

System.UInt16 ushort - Entier non signé 0 à 65535

System.Int64 long Long Entier signé- 922337203685477508 à 922337203685477507

System.UInt64 ulong - Entier non signé 0 à 18446744073709551615

System.Single float SingleType virgule flottante à simple précision

-3.402823e38 à 3.402823e38

Leçon 1 : Langage & Syntaxe.

3. Types de données.

.NET C# VB.NET Type Plage

System.Double double DoubleType virgule flottante à double précision

-1.79769313486232e308 à 1.79769313486232e308

System.Decimal decimal Decimal

Type fractionnaire ou intégral précis qui peut représenter des nombres décimaux avec 29 bits significatifs

±1,0 × 10e-28 à ±7,9 × 10e28

System.Char char Char Caractère Unicode uniqueSymboles Unicode utilisés dans le texte

System.Boolean bool Boolean Type booléen logique True ou false

System.Object object ObjectType de base de tous les autres types

System.String string String Séquence de caractères

Leçon 1 : Langage & Syntaxe.

4. Déclarations et assignations.

[C#]//Déclaration simple.Int16 i, j;

[Vb.NET]‘Déclaration simple.Dim i, j As Integer

Déclaration simple

[C#]//Déclaration et assignation.Int16 i = 15;

[Vb.NET]//Déclaration et assignation.Dim i As Integer = 15

Déclaration et assignation

Leçon 1 : Langage & Syntaxe.

4. Déclarations et assignations.

[C#]//Déclaration d’un tableau.int[] i = new int[15];

[Vb.NET]‘Déclaration d’un tableau.Dim i(6) As Integer‘Ou.Dim i() As Integer = New Integer(6) {}

Déclaration d’un tableau

[C#]//Déclaration d’objet.SqlConnection cnn;

[Vb.NET]//Déclaration et assignation.Dim cnn As SqlConnection

Déclaration d’objet

Leçon 1 : Langage & Syntaxe.

4. Déclarations et assignations.

[C#]//Déclaration et instanciation d’objet.SqlConnection cnn = new SqlConnection();

[Vb.NET]‘Déclaration d’un tableau.Dim cnn As SqlConnection = _New SqlConnection‘Ou.Dim cnn As New SqlConnection

Déclaration et instanciation d’objet

Leçon 1 : Langage & Syntaxe.

4. Déclarations et assignations.

[C#]//Déclaration simple.Int16 i, j;

[Vb.NET]‘Déclaration simple.Dim i, j As Integer

Déclaration simple

[C#]//Déclaration et assignation.Int16 i = 15;

[Vb.NET]//Déclaration et assignation.Dim i As Integer = 15

Déclaration et assignation

Leçon 1 : Langage & Syntaxe.

5. Fin d’instruction.

[C#]a = 5;b = 7; c = 9;votreProcedure(arg1,

arg2,arg3);

[Vb.NET]a = 5b = 7 : c = 9votreProcedure(arg1, _

arg2, _arg3)

Fin d’instruction

Leçon 1 : Langage & Syntaxe.

6. Blocs d’instructions.

[C#]if (a == 7){

premiereInstruction;deuxiemeInstruction;

}//Ou.if (a == 7)

premiereInstruction;deuxiemeInstruction;

[Vb.NET]If a = 5 Then

premiereInstructiondeuxiemeInstruction

End If

Blocs d’instructions

• Dans C#, les accolades {} sont utilisés pour délimiter un bloc d’instruction, sinon, une seule instruction est supposée.

La deuxième instruction ne fait partie du bloc (if).

Leçon 1 : Langage & Syntaxe.

7. Utilisation des parenthèses () et des crochets [].

• C # utilise des parenthèses () pour délimiter les arguments de fonction, et les crochets [] pour délimiter les éléments du tableau et les indices de propriété.

• VB.NET utilise des parenthèses () pour délimiter les éléments du tableau, les arguments de fonction et les indices de propriété.

Leçon 1 : Langage & Syntaxe.

7. Utilisation des parenthèses () et des crochets [].

[C#]//Déclaration et initialisation.Int16[] x = new Int16[5] { 1, 2, 3, 4, 5 };

[Vb.NET]//Déclaration et initialisation.Dim a() As Long = {3, 4, 5}

Déclaration et initialisation d’un tableau

[C#]//Déclaration d’un tableau.Int16[] i = new Int16[15];

[Vb.NET]‘Déclaration d’un tableau.Dim i(6) As Integer‘Ou.Dim i() As Integer = New Integer(6) {}

Déclaration d’un tableau

Leçon 1 : Langage & Syntaxe.

7. Utilisation des parenthèses () et des crochets [].

[C#]//Indices de propriété.id = maDataSet.Tables["Client"].Rows[5].Columns["idClient"]

[Vb.NET]//Déclaration et initialisation.id = maDataSet.Tables("Client").Rows(5).Columns("idClient")

Indices de propriété

[C#]//Paramètres.maFonction(parm1, param2);

[Vb.NET]‘Paramètres.maFonction(parm1, param2)

Paramètres

Leçon 1 : Langage & Syntaxe.

8. Opérateurs.

Opérateur C# VB.NET

Inférieur de < <

Inférieur ou égal à <= <=

Supérieur de > >

Supérieur ou égal à >= >=

Égal == =

différent != <>

Comparer deux objets == Is

Comparer deux types x is Class1 TypeOf x Is Class1

Comparer deux chaînes == ou String.Equals() =

Relationnel et égalité

Leçon 1 : Langage & Syntaxe.

8. Opérateurs.

Opérateur C# VB.NET

Et && And

Ou || Or

Logique

Opérateur C# VB.NET

Conditionnel ?: IIf

Conditionnel

Leçon 1 : Langage & Syntaxe.

8. Opérateurs.

Opérateur C# VB.NET

Addition + +

Soustraction - -

Multiplication * *

Division / /

Division entière \

Modulo % Mod

Affectation =, +=, -=, *=, /= =, +=, -=, *=, /=, \=

Enchaîner += &=

Concaténer deux chaînes + &

Autres

Leçon 1 : Langage & Syntaxe.

9. Instructions conditionnelles.

Instruction conditionnelle C# VB.NET

Structure de décision (Sélection) switch, case, default, Select Case …, Case, Case Else, End Select

Structure de décision (Conditionnel) if, elseIf … Then, ElseIf … Then, Else, End If

Structure de boucle (Conditionnel) do, while, continueWhile… End While, Do [While, Until] …, Loop [While, Until]

Structure de boucle (Itération) for, foreachFor …, [Exit For,] Next For Each …, [Exit For,] Next

Contrôlesbreak, continue, goto, return,throw

Exit, GoTo, Stop, End, Return

Leçon 1 : Langage & Syntaxe.

10. Gestion des exceptions.

• C# prend en uniquement en charge la gestion des exceptions structurées.

• Pour maintenir la migration des projets VB vers VB.NET, VB.NET en prend en charge à la fois la gestion des exceptions structurées et non structurées.

Leçon 1 : Langage & Syntaxe.

10. Gestion des exceptions.

C# VB.NET

Gestion structurée

try…Catch…Finally

throw

Try…Catch…Finally…End Try

Gestion non structuréeOn Error GoToOn Error Resume Next

Leçon 1 : Langage & Syntaxe.

11. Envoi des paramètres.

[C#]//La méthode.void maMethode(Int64 y){

}

//Appeler la méthode.maMethode(x);

[Vb.NET]‘La méthode.Public Sub maMethode(ByVal y As Long)

End Sub

//Appeler la méthode.maMethode(x)

Envoi par valeur

Leçon 1 : Langage & Syntaxe.

11. Envoi des paramètres.

[C#]//La méthode.void maMethode(ref Int64 y){

}

//Appeler la méthode.maMethode(ref x);

[Vb.NET]‘La méthode.Public Sub maMethode(ByRef y As Long)

End Sub

//Appeler la méthode.maMethode(x)

Envoi par référence

Leçon 1 : Langage & Syntaxe.

12. Mots-clés.

C# VB.NET

Fait référence à l’objet courant this Me

Indiquer un commentaire///* */ pour plusieurs lignes

‘Rem

Documentation XML /// ‘’’

Indiquer une constante const, readonly Const

Déclarer un espace de nomsnamespace votreNS{}

Namespace "votreNS" End Namespace

Référencer un espace de noms using Imports