GL3. ProgrammationII. Chap1. Introduction À .NET Framework Et À C#
GL3. ProgrammationII. Chap1. Introduction À .NET Framework Et À C#
GL3. ProgrammationII. Chap1. Introduction À .NET Framework Et À C#
Chapitre 1
Mohamed Romdhani
MCSD Web Applications .NET 4.5
Contenu
Le .NET Framework
Prsentation de Visual Studio
Introduction au langage C#
Le .NET Framework
Architecture
Application
.NET Framework
OS
5
Frameworks
WinForms
WPF
WCF
etc.
VB
C#
Ruby /
Python
etc.
Visual Studio
ASP.NET
Languages
Version Number
Release Date
Visual Studio
1.0
1.0.3705.0
13.02.2002
1.1
1.1.4322.573
24.04.2003
2.0
2.0.50727.42
07.11.2005
3.0
3.0.4506.30
06.11.2006
3.5
3.5.21022.8
19.11.2007
4.0
4.0.30319.1
12.04.2010
4.5
4.5.40805
12.09.2012
SP1
3.5
3.0
.NET 1.0
.NET 1.1
.NET 2.0
4.5
.NET 4
.NET 4
2002
2003
2005-08
2008 CTP!
2008 CTP!
CLR 1.0
CLR
1.1
CLR 2.0
CLR 4
CLR 4.5
Caractristiques de la plateforme
.Net
Les langages gnrent
dsormais un langage
commun, bas sur des types
communs : le MSIL
(Microsoft Intermediate
Language)
Ces modules en MSIL sont
compils en code machine
la vole ( just in time )
Astuce : il est possible de
lire le MSIL avec ildasm
Astuce : il est possible de
dcompiler avec reflector
10
Source
Code
Compiler
.cs
csc.exe
(Compile time)
MSIL
MSIL
containing
.NET
Assembly
Run time
JITter
CLR
En ter Title Here
Memory
11
MSIL
.NET Languages JITting MSIL
Not an executable
Needs CLR
After JIT compilation (Just-In-Time Compilation)
CPU independent
Advantages
Platform independent / any client (that has a suitable framework)
Optimization (even smart compilation)
12
13
14
What is an assembly ?
Building blocks of .NET Framework applications
Collection of types and resources that form a logical unit of functionality
MyClassA
MyResource
MyClassB
V 1.1.254.1
Assembly version
<major>.<minor>.<build>.<revision>
15
System
System.IO
Entres/Sorties
System.Security
System.Collections
Listes, tableaux, etc.
System.Resources
Traduction automatique
System.Text
StringBuilder
System.Text.RegularExpressions
System.Configuration
Accs aux fichiers de config XML.
System.Xml
System.Xml.Schema
Pour les XSD
System.Xml.XPath
System.Xml.Xsl
System.Data
ADO.NET
System.Data.OracleClient
System.Xml.Serialization
System.Data.Sql
16
Caspol.exe
Makecert.exe
Ngen.exe
Gacutil.exe
Ildasm.exe
Sn.exe
17
18
19
Project templates
Wizards
Debugging
IntelliSense
20
10
ASP.NET project
WPF project
.aspx
.xaml
.csproj
.aspx.cs .config
.csproj
.xaml.cs .config
Console project
.cs
.csproj
.config
21
22
11
(cont.)
Form (Windows
Forms application)
Properties
window
23
The set of menus displayed depends on what you are currently doing in the IDE
24
12
25
Introduction au langage C#
26
13
Purpose of C#
C#
C# is the language of choice for many developers who build
.NET Framework applications
C# uses a very similar syntax to C, C++, and Java
C# has been standardized and is described by the ECMA334 C# Language Specification
27
C# au fil de lhistoire
[http://jlambert.developpez.com/tutoriels/dotnet/nouveautes-csharp-5/]
28
14
}
}
29
Single inheritance
Interfaces
Specify methods & interfaces, but no implementation
Structs
A restricted, lightweight (efficient) type
Delegates
Expressive typesafe function pointer
Useful for strategy and observer design patterns
30
15
31
Syntax
Case-sensitive
Whitespace has no meaning
Sequences of space, tab, linefeed, carriage return
32
16
Behavior
Methods
33
Inheritance
Operationally
If class B inherits from base class A, it gains all of the variables and
methods of A
Class B can optionally add more variables and methods
Class B can optionally change the methods of A
Uses
Reuse of class by specializing it for a specific context
Extending a general class for more specific uses
Interfaces
Allow reuse of method definitions of
interface
Subclass must implement method
definitions
34
17
Inheritance Example
class A
{
public void display_one()
{
System.Console.WriteLine("I come from A");
}
}
class B : A
{
public void display_two()
{
System.Console.WriteLine("I come from B, child of A");
}
}
class App
{
static void Main()
{
A a = new A(); // Create instance of A
B b = new B(); // Create instance of B
a.display_one(); // I come from A
b.display_one(); // I come from A
b.display_two(); // I come from B, child of A
}
}
35
Visibility
A class is a container for data and behavior
Often want to control over which code:
Can read & write data
Can call methods
Access modifiers:
Public
No restrictions. Members visible to any method of any class
Private
Members in class A marked private only accessible to methods of class A
Default visibility of class variables (but is good to state this explicitly)
Protected
Members in class A marked protected accessible to methods of class A and subclasses of
A.
36
18
Constructors
Use new to create a new object instance
This causes the constructor to be called
A constructor is a method called when an object is created
C# provides a default constructor for every class
Creates object but takes no other action
37
Predefined Types
C# predefined types
Reference object, string
Signed
Logical
bool
38
19
Delegates
An objectified function
inherits from System.Delegate
sealed implicitly
looks much like C/C++ style function pointer
eg. delegate int Func(ref int x)
defines a new type: Func: takes int, returns int
declared like a function with an extra keyword
stores a list of methods to call
Example
delegate int Func(ref int x);
int Increment(ref int x) { return x++; }
int Decrement(ref int x) { return x--; }
Func F1 = new Func(Increment);
F1 += Decrement;
x = 10;
Console.WriteLine(F1(ref x));
Console.WriteLine(x);
39
Events
Special class of delegates
given the event keyword
class Room {
public event EventHandler Enter;
public void RegisterGuest(object source, EventArgs e) { }
public static void Main(string[] args) {
Enter += new EventHandler(RegisterGuest);
if (Enter != null) {
Enter(this, new EventArgs());
}
}
}
40
20
C# 3.0 Features
Implicitly Typed Local Variables
Extension Methods
Lambda Expressions
Object Initializers
Collection Initializers
41
i = 5;
s = "Hello";
d = 1.0;
numbers = new int[] {1, 2, 3};
orders = new Dictionary<int,Order>();
Errors:
var x;
var y = {1, 2, 3};
var z = null;
42
21
Extension Methods
Declaration:
public static partial class Extensions {
public static int ToInt32(this string s) {
return Int32.Parse(s);
}
}
Usage:
string s = "1234";
int i = s.ToInt32();
// Same as Extensions.ToInt32(s)
43
Usage:
int[] digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int[] a = digits.Slice(4, 3);
// Same as Extensions.Slice(digits, 4, 3)
44
22
Lambda Expressions
Generalized function syntax
x.x+1
in C# 3.0, have x => x + 1
From anonymous delegate syntax:
delegate(int x) { return x + 1;}
Can have implicitly typed variables
Can have more than one variable
Can have expression or statement body
NB: no statement bodies in preview compiler
45
46
23
47
Object Initializers
Class representing a point:
public class Point {
private int x, y;
48
24
49
Collection Initializers
Example:
List<int> digits = new List<int> { 0, 1, 2};
Is equivalent to:
List<int> digits = new List<int>();
digits.Add(0);
digits.Add(1);
digits.Add(2);
50
25
Auto-implemented Properties
Backed up by a private field normally inaccessible to programmer (only
via the property):
class LightweightCustomer {
public double TotalPurchases { get; set; }
public string Name { get; private set; }
// read-only
// read-only
51
Anonymous Types
Following expression:
new { p1 = e1 , p2 = e2 , pn = en }
private Tn fn ;
public T1 p1 { get { return f1 ; } set { f1 = value ; } }
public T2 p2 { get { return f2 ; } set { f2 = value ; } }
52
26
Partial Methods
Can appear only in partial classes or structs, and must be void,
private and without out parameters:
partial class A {
string _name;
partial void OnNameChanged();
public string Name {
set {
_name = value;
OnNameChanged();
}
}
}
partial class A {
partial void OnNameChanged() {
// Do something
}
}
53
LINQ to Objects
Set of generic extension methods (Select, Where, OrderBy, + others)
implemented for IEnumerable<T> interface, example:
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var numberGroups = from n in numbers group n by n % 5 into g
select new {Remainder = g.Key, Numbers = g.Group};
foreach (var g in numberGroups) {
Console.WriteLine(
"Numbers with a remainder of {0} when divided by 5:", g.Remainder);
);
foreach (int n in g.Numbers) {
Console.WriteLine(n);
}
}
54
27
C# 4.0 Features
Dynamic Typed objects
Named and optional Parameters
55
Statically typed
to be dynamic
Dynamic
conversion
M. Romdhani, Janvier 2014
Dynamic method
invocation
56
28
57
58
29
59
Delegates
System.Func<in T, , out R>
System.Action<in T, >
System.Predicate<in T>
System.Comparison<in T>
System.EventHandler<in T>
M. Romdhani, Janvier 2014
60
30