it
LEZIONE 03
.NET Framework Class Library
File I/O
Exception Handling
Collections
Generics
… and much more!
2
Collezioni di oggetti
3
Collezioni di oggetti
4
Hashtable
5
Stack e Queue
enqueue dequeue
6
BitArray
7
BitArray: un esempio
8
IEnumerable e ciclo foreach
9
IEnumerable e IEnumerator
• Definire un enumeratore personalizzato:
10
Generics
11
Dichiarare un tipo generico
12
Collezioni “generiche”
• Collezioni in System.Collections.Generic:
Tipo generico Controparte non generica
List<T> ArrayList
(nessuna) BitArray
LinkedList<T> (nessuna)
Stack<T> Stack
Queue<T> Queue
13
Nullable<T>
int?
Do you remember?
14
Benefici dei generics
15
Verifichiamo le performance
16
Verifica su T
17
Vincoli sul tipo T
18
Generic Methods
19
Ereditarietà con i generics
20
Ereditarietà con i generics
21
Covarianza e Controvarianza
23
Esercizi
24
Gestione delle eccezioni
25
Catturare una eccezione
Blocco di codice
sotto controllo
delle eccezioni
Cattura l’eccezione
specifica Gestione dell’errore
specifico
27
“Pulizia finale” con Finally
28
System.Exception
StackTrace String Contiene nomi e firme delle chiamate ai metodi dal punto in
cui è stato generato l’errore
29
“Sollevare” una eccezione
30
Definire una nuova eccezione
31
Gestione delle eccezioni – Tips
32
System.IO: lavorare con i file
You use these classes to manipulate a machine’s set of files. The File
File
type exposes functionality using static members, while the FileInfo
FileInfo
type exposes similar functionality from a valid object reference.
33
System.IO: lavorare con i file
Tipo Descrizione
This class gives you random file access (e.g., seeking capabilities) with
FileStream
data represented as a stream of bytes.
This class allows you to monitor the modification of external files in a
FileSystemWatcher
specified directory.
This class provides random access to streamed data stored in memory
MemoryStream
rather than a physical file.
This class performs operations on System.String types that contain file
Path
or directory path information in a platform-neutral manner.
StreamWriter You use these classes to store (and retrieve) textual information to (or
StreamReader from) a file. These types do not support random file access.
Like the StreamReader/StreamWriter classes, these classes also work
StringWriter
with textual information. However, the underlying storage is a string
StringReader
buffer rather than a physical file.
35
Leggere e scrivere: un modo veloce
Con WriteAllLines
se non esiste il file
viene creato in quel
momento
36
La classe astratta Stream
Read() Write()
Seek() Position
ReadByte() WriteByte()
37
FileStream
Codifica da
string a byte
Decodifica da
byte a string
38
StreamWriter
39
Scrivere su file di testo
Scrivo del testo sul file Inserisco nel file una nuova riga
NOTA: Con Autoflush = true la scrittura sul file avviene dopo ogni write, con
Autoflush = false (predefinito) la scrittura viene fatta al momento del Close()
40
StreamReader: leggere da file di testo
ReadBlock() ReadToEnd()
41
StringWriter e StringReader
Scrivere testo
in memoria
Leggere testo
dalla memoria
42
Esercizi
43
Contacts MANUEL SCAPOLAN
website: www.manuelscapolan.it
twitter: manuelscapolan
e-mail: info@manuelscapolan.it
Credits
Le immagini contenute in questa presentazione
hanno licenza Creative Commons
Slide 2 : http://www.flickr.com/photos/ajc1/4663140532
44