Cours C#
34 pages
English

Cours C#

-

Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres
34 pages
English
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres

Description

Nicolas PastorellyIntroduction à C #Principaux élément différentiablesPar rapport à Java1Modèle Objet • Les types de bases ne peuvent pas être utilisés comme des objets en JavaInteger i_ref = new Integer(7);List l = ...l.add( i_ref );…… i_ref.intValue();• C# : boxing & unboxingint i = 123; Heapobject o = i;int j = (int)o;123iSystem.Int32o123123jStack2Les propriétésPublic class Test{// donnée privée.private int m_number = 0;[Description(“Un entier non négatif !")]public int Number{// accesseurs du champ privéTest T = new Test();get {T.Number= -2;if (m_number < 0) return 0;return m_number;}set {Int I = T.Number + 1;if (value < 0) m_number = 0;else m_number = value;}}}3Les indexerspublic class ListBox: Control{private string[] items;public string this[int index] {get { ListBox listBox = new ListBox();return items[index];}set { listBox[0] = "hello";items[index] = value;Repaint(); Console.WriteLine(listBox[0]);}}}4Les classes non extensiblessealed class Student{string fname;string lname;int uid;void attendClass() {}}? (final Java)5Les NameSpacesusing System;namespace Company{public class MyClass{ /* Company. */int x;void doStuff(){}}namespace Carnage4life{public class MyOtherClass{ /* Company.Carnage4life.MyOtherClass */int y;void doOtherStuff(){}public static void Main(string[] args){Console.WriteLine("Hey, I can nestnamespaces");}}// class MyOtherClass}// namespace Carnage4life}// ...

Informations

Publié par
Nombre de lectures 20
Langue English

Extrait

Nicolas Pastorelly
Introduction à C #
Principaux élément différentiables
Par rapport à Java
1
Modèle Objet
Les types de bases ne peuvent pas être utilisés comme des objets en Java
Integer i ref = new Integer(7); _ List l = ... l.add( i ref ); _ ……i ref.intValue(); _
C# : boxing & unboxing
Stack
i123
o j123
int i = 123; object o = i; int j = ( int)o ;
123
Heap
System.Int32
2
Public class Test{
}
Les propriétés
// donnée privée. private int m_number = 0;
[Description(“Un entier non négatif !")] public int Number{
// accesseurs du champ privé
get { if (m_number < 0) return 0; return m number; _ } set { if (value < 0) m_number = 0; else m number = value; _ } }
Test T = new Test();
T.rebmuN= -2;
Int I =T.Number + 1;
3
public class ListBox: Control { private
tsirng[] time;s
public stringthis[int index] { get { return items[index]; } set { items[index] = value; Repaint(); } } }
Les indexers
ListBox listBox = new ListBox();
listBox[0]
 ="hello";
Console.WriteLine(listBox[0]);
4
sealed
{
lcsa
string fname;
string lname;
int uid;
 sS
Les classes non extensibles
utde
void attendClass() {}
}
?
(final Java)
tn
5
Les NameSpaces
using System; namespace Company {
public class MyClass { /* Company.MyClass */ int x; void doStuff(){} } namespace Carnage4life {
public class MyOtherClass { /* Company.Carnage4life.MyOtherClass */ int y; void doOtherStuff(){} public static void Main(string[] args) { Console.WriteLine("Hey, I can nest namespaces"); } }// class MyOtherClass }// namespace Carnage4life }// namespace Company
6
Constructeurs, Destructeurs
public class MyClass { static int num_created = 0; int i = 0; MyClass() { i = ++num_created; Console.WriteLine("Created object #" + i); } ~MyClass() { Console.WriteLine("Object #" + i + " is being finalized"); } public static void Main(string[] args) { for(int i=0; i < 10000; i++) new MyClass(); }
7
Constructeur de classe
using System; class SomeClass { private Int32 member1; private static Int32 member2; //Type constructor staticSomeClass(){} //Instance constructor publicSomeClass(){} //default instance Constructor publicSomeClass(Int32 memberval) { member1 = memberval;} //Other class operations ...
}
8
Constructeurs de classe
class StaticInitTest { string instMember = InitInstance(); string staMember = InitStatic(); StaticInitTest() {6 Console.WriteLine stance constructor"); } static StaticInitTest() {Console.WriteLine("In static1uctor"); }
static String InitInstance() {4 Console.WriteLine(" Initializ stance variable"); return "instance"; } static String InitStatic() {5c variable"); Console.WriteLine(" Initi stati return "static"; }
static void DoStuff() { Console.WriteLine(" Invo3tatic DoStuff() method"); } public static void Main(string[] args) { Console.WriteLine("Beginning main()"2 StaticInitTest.DoStuff(); StaticInitTest sti = new StaticInitTest(); Console.WriteLine("Completed main()"); } }
In static constructor Beginning main() Invoking static DoStuff() method Initializing instance variable Initializing static variable In instance constructor Completed main()
9
Constructeurs de classe
class Bidon {}/* juste pour avoir un objet à créer et illustrer le readonly */
class ClassLoaderTest{ private static int compteur; private staticreadonlyint constante;//équivalent à une constante pas initialisée private staticreadonlyBidon bidon;
/* CONSTRUCTEUR DE CLASSE : remarquez lestatic!!*/ staticsalCoasLrTdet(es){ Console.WriteLine("Constructeur de classe (compteur <-- 1)"); constante = 12345; compteur = 1; bidon = new Bidon(); }
/* UN CONSTRUCTEUR D'INSTANCE */ public ClassLoaderTest(){ Console.WriteLine(constante +": Constructeur d'instances #"+ compteur++); }
public static void Main (String[] args){ Console.WriteLine ("Dans le main");
ClassLoaderTest clt01 = new ClassLoaderTest(); ClassLoaderTest clt02 = new ClassLoaderTest(); }d:\C#Work\classloader>Constructeur de classe (compteur <-- 1) }de classe n'est appelé qu'une fois !##==> Le constructeur Dans le main 12345: Constructeur d'instances #1 12345: Constructeur d'instances #2
Constantes
public class ConstantTest { /* Compile time constants */ const int i1 = 10; //implicitly a static variable // code below won't compile because of 'static' keyword // public static const int i2 = 20; /* run time constants */ public static readonly uint l1 = (uint) DateTime.Now.Ticks; /* object reference as constant */ readonly Object o = new Object(); /* uninitialized readonly variable */ readonly float f; ConstantTest() { // unitialized readonly variable must be initialized in constructor f = 17.21; } }
11
  • Univers Univers
  • Ebooks Ebooks
  • Livres audio Livres audio
  • Presse Presse
  • Podcasts Podcasts
  • BD BD
  • Documents Documents