You are on page 1of 69

PARADIGM2K5

REVOLUTIONS BEGIN HERE


C# .NET
Contents
Chapter 1
Introducing the .NET Platform
Chapter 2
Introducing C# Programming
Chapter 3
Windows Forms
Chapter 4
Network Programming
Chapter 5
Remoting
Chapter 6
Message Queuing
Chapter 7
ADO.NET
Chapter 8
Working with XML
Chapter 9
Web Services
1. Introducing the .NET Platform
Introducing the .NET Platform
The .NET platform is the foundation upon
which the next generation of software will
be built.
.NET consists of three primary
components.
.NET Components

.NET

.NET Framework .NET Products .NET Services

CLR Base Library Development Target

Web Application

Windows Application
.NET Architecture
VB C# C++ Perl OL V
I
Common Language Specification (CLS)
S
Web Services Windows Forms U
A
Data and Xml L
STU
Base Class Library DIO
.NET
Common Language Runtime (CLR)
Features
Multi Language Development
Automatic Memory Management
Versioning Support
Easy Deployment
Security
Performance and Scalability
Interoperability
2. Introducing C# Programming
Introduction
C# is a modern Object-Oriented
Language.
Only a Command Line C# compiler with
.NET Framework is enough for compiling
C# Programs.
Creating Your First C# Program
The first C# program we look at is a very
simple program that writes a couple of lines
of text to the console.
The program source code is shown in listing
chap02\2.1.
Class
Classes are the basic ingredients of object-
oriented languages.
Classes are declared in C# by using the
class keyword followed by the class name
and brackets surrounding the body of the
class.
C#, like most object-oriented classes,
supports member variables and methods.
The program source code is shown in
listing Chap02\2.2
Class and Class Member
Visibility Access Modifiers
public Accessible from anywhere
protected Accessible from this class or
any class derived from this
class
internal Accessible within current
program (assembly) only
protected internal Accessible within
current program (assembly)
or any class derived from this
class
private (default) Accessible only
within current class
Execution of the program starts at the
Main method.
Namespaces are used in .NET to organize
class libraries into a hierarchical structure.
The using keyword allows you to reference
classes in the System namespace without
having to include System prior to the class
name.
Adding Comments
C# supports three different types of
source code comments, single-line
comments, multiline comments, and
source code documentation comments.
Single-line comments begin with //.
Multi line comments begin with /* and
end with */ and can span multiple lines.
Text between them constitutes the
comment.
Source code documentation comments
begin with ///.
Introducing Data Types
A programming language wouldnt be able
to do much if it didnt have data to work
with.
C# supports two data types: value types
and reference types.
Value types are the typical primitive types
available in most programming languages
and
are allocated on the stack.
Reference types are typically class
instances and are allocated on the heap.
Primitive Data Types
Data Type Size in Bytes Description
sbyte 1 Signed byte
byte 1 Unsigned
byte
short 2 Signed short
ushort 2 Unsigned
short
int 4 Signed
integer
Data Type Size in bytes Description
uint 4 Unsigned
integer
long 8 Signed long
integer
ulong 8 Unsigned long
integer
float 4 Floating point
Data Type Size in bytes Description
double 8 Double-
precision
floating
point
decimal 8 96-bit signed
number
string n/a Unicode
string
Data Type Size in bytes Description
char n/a Unicode
character
bool n/a True or false
Control Structures
The C# language supports all of the flow-
of-control statements you would normally
expect.
if Statement
The if statement executes a series of
statements if a test Boolean expression
evaluates to true.
Example:
int i = 3;
int j = 0;
if ( i > 2 )
{ j = 3; }
switch case Statement
The switch statement chooses flow of control
based on the evaluation of a numeric or string
comparison.
Syntax:
switch ( Variable )
{
case 1:
Statements
break;
default:
break;
}
for Statement
The for statement is used to loop through
a series of statements until a test Boolean
expression evaluated at the beginning of
the loop is false.
Syntax:
for ( Initialization; Condition; Increment )
{
Statements;
}
while Statement
The while statement is also used to loop
through a series of statements until a test
Boolean expression evaluated at the
beginning of the loop is false.
Syntax:
while ( condition )
{
Statements;
}
Using Exception Handling
If you look through the .NET Framework
SDK documentation, you wont find
an error code returned from any method
calls in the library.
Instead, the Framework uses exceptions
to indicate errors that occur.
The code snippet in Listing Chap02\2.3
shows Exception Handling.
Inheritance
Inheritance and polymorphism are the two
characteristics that make object-oriented
programming languages so powerful.
Inheritance means you can create a new
type of Object.
Polymorphism means that this new object
that can choose to inherit some
characteristics and supply its own
implementation for others.
3. Windows forms
Introducing Windows Forms
Windows Forms is a collection of classes
and types that encapsulate and extend the
Win32 API in a tidy object model.
Core Classes
Class What It Does
Object Acts as a base class for
all types in the .NET
Framework.
Component Provides the basics of
containership, facilitates
hosting in a visual
designer, and defines a
protocol for resource
disposal.
Control Provides the core
functionality for a visual
control that responds to
mouse and key-
boardmessages, accepts
focus, and can
participate in drag and-
drop operations.
Writing a Simple Text Editor
This walkthrough will take you through
developing a simple Notepad style text
editor, demonstrating the following:
Adding a menu
Creating and activating a new form
Creating a Multiple Document Interface
Creating a dialog form
Using form inheritance
Adding a tab control
Anchoring controls
Connecting the dialog form
The listing Chap03\3.1 contains the code
for this sample.
Creating Control
The three most common scenarios:
You have a recurring group of controls that
you would like to make into a reusable
component (a UserControl ).
You need a control that cannot be
assembled or adapted from existing
components (a custom control).
You want to extend a standard controlin
order to modify or enhance its appearance
or behavior (an inherited control).
Creating a User Control
The listing Chap03\3.2 contains the user
control.
4. Network Programming
Introducing Networking and
Sockets
The first electronic network was ARPAnet.
TCP/IP is a protocol family that allows
connected computers to communicate and
share resources across a network.
To access IP-based networks from an
application, we need sockets.
A socket is a programming interface and
communication endpoint that can be used
for connecting to other computers,
sending and receiving data from them.
Types of Sockets
Raw sockets This type is implemented
on the network layer.
Datagram sockets Datagrams are
packets of data..
Stream sockets In contrast to datagram
sockets, these sockets provide a stream of
data.
TCP
The Transmission Control Protocol is a
connection and stream-oriented, reliable
point-to-point protocol. TCP
communication is analogous to a phone
call.
The listing Chap04\4.1 shows the sample
for TCP.
UDP
The User Datagram Protocol is a
connection-less and datagram-oriented
best-effort protocol. A UDP communication
is analogous to sending a letter.
The listing Chap04\4.2 shows the sample
for UDP.
Ports
Generally, a computer has a single
connection to the network.
If all data arrives through one connection,
how can it be determined which application
running on the computer receives the data?
The answer is through the use of ports.
A port is a 16-bit number in the range or 0 to
65535.
The port numbers 0 to 1023 are reserved for
special services such as HTTP (port 80), Mail
(port 25), and Telnet (port 23).
5. Remoting
Introduction
Remoting provides you with a number of
choices as to the method and
configuration of communication used.
Creating a Simple Remoting
Client Server
All remoting objects must inherit from
MarshalByRefObject.
Hosting applications use the
RegisterWellKnownServiceType method of
the RemotingConfiguration class to
register objects for remoting.
Singletons objects only have a single
instance and handle multiple client
requests.
SingleCall objects do not maintain state.
They handle a single request and are then
recycled by the remoting framework.
Remoting applications that act as servers
must listen on a port as specified by the
developer.
External XML configuration files may also
be used to configure remoting on both the
server and the client.
The listing Chap05\5.1 shows the sample
application.
6. Message Queuing
Introduction
MSMQ provides solutions to a range of
problem scenarios that appear during
application development.
client-server architecture is the best
example.
where a direct connection to the server is
needed to handle information updates.
In this situation, any disruption to the
server will cause all client applications to
fail, possibly causing idle staff and lost
productivity caused by a hardware failure,
power outage, or a server upgrade.
The solution is to use message queuing.
MSMQ also provides the plumbing you
need for true distributed processing.
A good example of this is in the area of
mathematical computation.
If the work required to find a solution to a
mathematical problem can be divided into
pieces then such pieces could be placed
on a queue.
MSMQ Architecture
In the world of MSMQ, you will be dealing
with two main objectsqueues and
messages.
A queue is a storage area for messages on
a MSMQ server.
A message can be thought of as an
envelope containing data plus information
that describes the type of data being sent,
its priority, security needs,
acknowledgement, and timing information.
Two types of clients:
Dependent clients Dependent clients
must have direct access to a Message
Queuing server.
Independent clients Independent
clients do not need continuous access to a
Message Queuing server to send
messages.
Creating a Simple Application
Messages can be sent using two methods
by using a Message object or a simple
data type.
When sending a simple data type, the
message queues default properties are
used.
When using the Message object, you can
set its properties to handle your
messaging requirements instead of using
the message queues default properties.
The page in the listing Chap06\6.1 shows
how to send and receive messages from
message queue.
7. ADO.NET
Introduction
There are three layers to data access in
ADO.NET:
The Physical data store This can be an
OLE database, a SQL database, or an XML
file.
The data provider This consists of the
Connection object and command objects
that create the in-memory representation
of the data.
The data set This is the in-memory
representation of the tables and
relationships that you work with in your
application.
Two types of database connection:
Use an OleDbConnection object to connect
to a local database.
Use a SqlDbConnection object to connect
to a server database.
ADO.NET Namespace
System.Data Classes, types and services
for creating and accessing data sets and
their subordinate objects.
System.Data.SqlClient Classes, types for
accessing Microsoft SQL Server databases.
System.Data.OleDb Classes and types
for accessing OLE databases.
DataGrid
The DataGrid control renders a tabular,
data-bound grid.
This control allows you to define various
types of columns to control the layout of
the cell contents of the grid and add
specific functionality.
The page in the listing Chap07\7.1 uses a
DataGrid.
DataList
Use the DataList control to display a
template-defined data bound list.
The DataList control supports selecting
and editing.
The page in the listing Chap07\7.2 shows
the use of DataList.
8. Working with XML
Frequently Used XML Classes
XmlReader System.Xml
XmlTextReader System.Xml
XmlValidatingReader System.Xml
XmlNodeReader System.Xml
XmlWriter System.Xml
XmlTextWriter System.Xml
XmlDocument System.Xml
XmlDataDocument System.Xml
XPathDocument System.Xml.XPath
Two types of clients:
XPathNavigator System.Xml.XPath
XmlSchema System.Xml.Schema
XslTransform System.Xml.Xsl
Working with XML DOM
XML DOM API is used to create, modify,
and traverse XML documents.
The classes most often used to work with
XML DOM documents are contained in the
System.Xml namespace.
The listing Chap08\8.1 shows an example
for DOM.
Working with XPath
XPath is used to find a set of nodes in an
XML document based on a location
qualifier and a pattern to match.
XSLT also makes use of XPath expression.
The listing Chap08\8.2 shows the code for
XPath.
Working with XSL
XSLT used to transform XML documents to
a different format.
XSL stylesheets will use XPath expressions
to select node lists for transformation.
The listing Chap08\8.3 shows a example
for using xsl to create Html page.
9. Webservices
Web Services are classes that extend
System.Web.Services.WebService.
A method becomes a Web method by
decorating it with
[System.Web.Services.WebMethod].
The listing Chap09\9.1 shows an example
of WebService.
Introduction
Web Services is a new technology
designed primarily to facilitate
communications between enterprises on
the Internet.
HTTP as the network protocol (among
others)
XML to encode data
SOAP as the wire transport protocol
WSDL to describe Web Service syntax
UDDI to publish Web Service information

You might also like