Title: Chapter 3 The .NET Framework Class Library (FCL)
1Chapter 3The .NET Framework Class Library (FCL)
2 File and Stream I/O
-
- FCL provides the API that managed applications
write to. - 100 hierarchically organized namespaces and more
than 7,000 types. - File and Stream I/O
- A stream is an abstract representation of
byte-oriented data. - Stream classes have methods that you can call to
perform input and output. - An additional level of abstraction readers and
writers.
3File and Stream I/O
- General Procedure
- 0. using System.IO
- 1. Open the file using a FileStream object.
- 2. For binary reads and writes, wrap instances
of BinaryReader and BinaryWriter around the
FileStream object and call BinaryReader and
BinaryWriter methods such as Read and Write to
perform input and output. - 3. For reads and writes involving text, wrap a
StreamReader and StreamWriter around the
FileStream object and use StreamReader and
StreamWriter methods such as ReadLine and
WriteLine to perform input and output. - 4. Close the FileStream object.
4File and Stream I/O Code
- using System.IO
- StreamReader reader new StreamReader
("Hello.cs") - for (string line reader.ReadLine ()
- line ! null line reader.ReadLine ())
- System.Console.WriteLine (line)
- reader.Close ()
5- Collections (System.Collections)
- Classes that serve as containers for groups, or
collections, of data.
ArrayList Resizable arrays
BitArray Bit arrays
Hashtable Tables of key/value pairs structured for fast lookups
Queue First-in, first-out (FIFO) buffers
SortedList Tables of sorted key/value pairs accessible by key or index
Stack Last-in, first-out (LIFO) buffers
6- Regex regular expressions, a language for
parsing and manipulating text. Regex supports
three basic types of operations Splitting,
Searching, Replacing. - using System.Text.RegularExpressions
- Regex regex new Regex (_at_"\\") // use \ as a
delimiter. - string parts regex.Split (_at_"c\inetpub\wwwroot
\wintellect") - foreach (string part in parts) Console.WriteLine
(part) - c
- inetpub
- wwwroot
- wintellect
- (_at_ no need of \\ for the expression to be
parsed. \ is an escape character, e.g., \n. \\
means to treat \ as a regular character.) - http//en.wikipedia.org/wiki/Regular_expressionusi
ng
7- Internet Classes
- HttpWebRequest and HttpWebResponse (System.Net)
- LinkList.cs
- System.Web.Mail MailMessage, MailAttachment and
SmtpMail - Data Access (System.Data) (More in Chapter 12)
- ADO.NET is .NET Frameworks API for database
access. - SqlDataReader (System.Data.SqlClient) reads data
from Microsoft SQL Server databases only
(optimized for it). - OleDbDataReader (System.Data.OleDb) reads data
from all types of databases (slower than
SqlDataReader, but portable). - Output format in C
- http//blog.stevex.net/string-formatting-in-csharp
/
8- Reflection
- The API that reads metadata
- for
- obtaining type information at runtime.
- https//msdn.microsoft.com/en-us/library/system.re
flection(vvs.110).aspx - https//msdn.microsoft.com/en-us/library/ms173183.
aspx
9- An Example Assembly Math.dll
Math.dll
Simple.netmodule
Complex.netmodule
10- Reflection (System.Reflection)
- Managed applications stores metadata in
assemblies and modules. - Retrieving information about assemblies and
modules and the types they contain - Reading information added to an executables
metadata by custom attributes - Performing late binding by dynamically
instantiating and invoking methods on types - Useful classes are in
- System.Reflection.Assembly, which represents
assemblies - System.Reflection.Module, which represents
managed modules - System.Type, which represents types
11- Retrieving Info (AsmInfo.cs)
- Assembly a Assembly.Load ("Math.dll")
- Assembly methods GetModules, GetExportedTypes,
GetReferencedAssemblies, - Custom Attributes (System.Attribute)
- Attributes are a declarative means for adding
information to metadata. - Conditional ("DEBUG")
- public DoValidityCheck () ...
- CodeRevision attribute
- CodeRevision ("billg", "07-19-2001")
- CodeRevision ("steveb", "09-30-2001",
Comment"Fixed Bill's bugs") - struct Point
- public int x
- public int y
- public int z
12A simple Example Reflection in C Tutorial _at_
CodeProject http//www.codeproject.com/Articles/1
7269/Reflection-in-C-Tutorial The next
example loads DLLs gets type info from the
metadata create objects from the types gets
methods in the types (classes) invokes the
methods to create objects of another type.
13 ArrayList images new ArrayList () // to hold
Image objects foreach (string name in names)
//names an array of file names Assembly a
Assembly.Load (name) //load the file Type
type a.GetType ("PlugIn") //get the class
PlugIn Object obj Activator.CreateInstance
(type) //Create a PlugIn MethodInfo
method type.GetMethod ("GetImage") //Get
the reference to the text memory of method
GetImage Image image (Image)
method.Invoke (obj, null) //Invoke the
GetImage method to create an Image object
//null arguments for GetImage().
images.Add (image)
14- How to Find Additional Resource Online
15- How to Find Additional Resource Online
Example Command Line Arguments
Official Documentations https//docs.microsoft.c
om/en-us/dotnet/csharp/programming-guide/main-and-
command-args/command-line-arguments
16- How to Find Additional Resource Online
Example Command Line Arguments
Online Tutorials https//www.dotnetperls.com/mai
n
17- How to Find Additional Resource Online
Example Command Line Arguments
Discussion Forums https//stackoverflow.com/quest
ions/552796/what-is-string-args-in-main-class-for
18https//m.youtube.com/watch?vgE_0ZIIuP40
- How to Find Additional Resource Online
Example Command Line Arguments
Youtube https//www.youtube.com/watch?vgE_0ZIIuP
40appdesktop
19https//m.youtube.com/watch?vgE_0ZIIuP40
- How to Find Additional Resource Online
Example Command Line Arguments
Specify Command Line Arguments in Visual
Studio https//dailydotnettips.com/how-to-pass-com
mand-line-arguments-using-visual-studio/
20https//m.youtube.com/watch?vgE_0ZIIuP40
- How to Find Additional Resource Online
Example Command Line Arguments
Note the search path of a running program in
Visual Studio is the directory which contains the
executable. For example, when the Hello.exe is
in Z\WP\Hello\Hello\bin\Debug\ the code
segment StreamReader reader new StreamReader
("Hello.cs") will read file Z\WP\Hello\Hello\bi
n\Debug\ Hello.cs
21FCL is the API for managed applications. Most
commonly used namespaces IO, Collections, Net,
Data, Reflection. Additional Resources Read
and practice the details.