The document discusses various topics related to object-oriented programming and .NET Framework development. It introduces goals of the .NET Framework like support of industry standards, extensibility, unified programming models, and improved memory and security models. It also covers topics like assemblies, namespaces, inheritance, polymorphism, exceptions, collections, and data access using ADO.NET. The document discusses Windows Presentation Foundation (WPF) and web application development concepts like controls, data binding, templates, page and control events, and data-bound web controls.
2. Introducing the .NET Framework
and Visual Studio
• Goals of the .NET Framework
• Microsoft designed the .NET Framework with certain goals in mind. The
following sections examine these goals and how the .NET Framework
achieves them.
• Support of Industry Standards
• the framework relies heavily on industry standards such as the Extensible Markup
Language (XML), HTML 5, and OData.
• Extensibility
• Through inheritance and interfaces, you can easily access and extend the functionality of
these classes.
• Unified Programming Models
• you could develop a class written in C# that inherits from a class written using Visual
Basic (VB). Microsoft has developed several languages that support the .NET Framework.
3. Introducing the .NET Framework
and Visual Studio
• Goals of the .NET Framework
• Improved Memory Management
• Improved Security Model
4. Working with the .NET Framework
• Understanding Assemblies and Manifests
• The assembly contains the code, resources, and a manifest (metadata about the
assembly) needed to run the application. Assemblies can be organized into a single file
where all this information is incorporated into a single dynamic link library (DLL) file or
executable (EXE) file, or multiple files where the information is incorporated into
separate DLL files, graphics files, and a manifest file.
• Referencing Assemblies and Namespaces
• you create an instance of the System.Windows.Controls.TextBox class, like so:
private System.Windows.Controls.TextBox newTextBox;
5. Using the Visual Studio Integrated
Development Environment
• Creating a New project
• to create a new project, follow these
steps:
1. on the start page, click the Create
project link, which launches the new
project dialog box. (You can also choose
File ➤ new ➤ project to open this dialog
box.)
6. Introducing Objects and Classes
• In OOP, you use objects in your programs to encapsulate the data
associated with the entities with which the program is working. For
example, a human resources application needs to work with
employees. Employees have attributes associated with them that
need to be tracked.
• Defining Classes
8. Using Constructors
• In OOP, you use constructors to
perform any processing that
needs to occur when an object
instance of the class becomes
instantiated. For example, you
could initialize properties of the
object instance or establish a
database connection.
9. Overloading Methods
• The ability to overload methods is a useful feature of OOP languages.
You overload methods in a class by defining multiple methods that
have the same name but contain different signatures.
10. Understanding Inheritance
• One of the most powerful
features of any OOP
language is inheritance.
Inheritance is the ability to
create a base class with
properties and methods
that can be used in classes
derived from the base
class.
11. Creating a Sealed Class
• By default, any C# class can be inherited. When creating classes that
can be inherited, you must take care that they are not modified in
such a way that derived classes no longer function as intended.
• Creating an Abstract Class
• At this point in the example, a client can access the GetBalance method through an instance
of the derived CheckingAccount class or directly through an instance of the base Account
class. Sometimes, you may want to have a base class that can’t be instantiated by client code.
Access to the methods and properties of the class must be through a derived class. In this
case, you construct the base class using the abstract modifier.
12. Using Access Modifiers in Base Classes
• When setting up class hierarchies using inheritance, you must manage how the
properties and methods of your classes are accessed. Two access modifiers you
have looked at so far are public and private. If a method or property of the base
class is exposed as public, it is accessible by both the derived class and any client
of the derived class. If you expose the property or method of the base class as
private, it is not accessible directly by the derived class or the client.
By defining the GetBalance method as protected, it becomes accessible to the derived class
CheckingAccount,
but not to the client code accessing an instance of the CheckingAccount class.
13. Overriding the Methods of a Base Class
• When a derived class inherits a method from a base class, it inherits
the implementation of that method. As the designer of the base class,
you may want to let a derived class implement the method in its own
unique way. This is known as overriding the base class method.
To override the Deposit method in the derived CheckingAccount class, use the following code:
14. Overriding the Methods of a Base Class
• One scenario to watch for is when a derived class inherits from the
base class and a second derived class inherits from the first derived
class. When a method overrides a method in the base class, it
becomes overridable by default. To limit an overriding method from
being overridden further up the inheritance chain, you must include
the sealed keyword in front of the override keyword in the method
definition of the derived class.
15. Calling a Base Class Method from a Derived
Class
• In some cases, you may want to develop a derived class method that
still uses the implementation code in the base class but also
augments it with its own implementation code. In this case, you
create an overriding method in the derived class and call the code in
the base class using the base qualifier.
16. Hiding Base Class Methods
• If a method in a derived class has the same
method signature as that of the base class
method, but it is not marked with the override
keyword, it effectively hides the method of
the base class. Although this may be the
intended behavior, sometimes it can occur
inadvertently. Although the code will still
compile, the IDE will issue a warning asking if
this is the intended behavior. If you intend to
hide a base class method, you should explicitly
use the new keyword in the definition of the
method of the derived class. Using the new
keyword will indicate to the IDE this is the
intended behavior and dismiss the warning.
17. Implementing Interfaces
• When you use an abstract class, classes that derive from it must implement its
inherited methods. You could use another technique to accomplish a similar
result. In this case, instead of defining an abstract class, you define an interface
that defines the method signatures.
• Classes that implement an interface are contractually required to implement the
interface signature definition and can’t alter it. This technique is useful to ensure
that client code using the classes knows which methods are available, how they
should be called, and the return values to expect.
18. Understanding Polymorphism
• For example, suppose you want all account classes in
a banking application to contain a GetAccountInfo
method with the same interface definition but
different implementations based on account type.
Client code could loop through a collection of
account-type classes, and the compiler would
determine at runtime which specific account-type
implementation needs to be executed. If you later
added a new account type that implements the
GetAccountInfo method, you would not need to
alter existing client code. You can achieve
polymorphism either by using inheritance or by
implementing interfaces. The following code
demonstrates the use of inheritance. First, you
define the base and derived classes.
19. Defining Method Signatures
• The following code demonstrates how methods are defined in C#. The
access modifier is first followed by the return type (void is used if no
return value is returned) and then the name of the method.
Parameter types and names are listed in parenthesis separated by
commas. The body of the method is contained in opening and closing
curly brackets.
20. Passing Parameters
• When you define a method in the class, you also must indicate how
the parameters are passed. Parameters may be passed by value or by
reference.
21. Understanding Delegation
• Delegation is when you request a service by making a method call to
a service provider. The service provider then reroutes this service
request to another method, which services the request.
25. Static Properties and Methods
• sometimes you may want different object instances of a class to
access the same, shared variables.
26. Using Asynchronous Messaging
• When a client object passes
a message asynchronously,
the client can continue
processing. After the server
completes the message
request, the response
information will be sent back
to the client.
29. Working with Arrays and Array Lists
• You access the elements of an array through its index.
The index is an integer representing the position of the
element in the array. For example, an array of strings
representing the days of the week has the following
index values:
• two-dimensional array where a student’s name (value) is
referenced by the ordered pair of row number, seat
number (index).
30. Working with Arrays and Array Lists
• The following code demonstrates declaring and working with an
array of integers. It also uses several static methods exposed by the
Array class. Notice the foreach loop used to list the values of the
array. The foreach loop provides a way to iterate through the
elements of the array.
31. Working with Arrays and Array Lists
• One comma indicates two dimensions; two commas indicate three
dimensions, and so forth. When filling a multidimensional array,
curly brackets within curly brackets define the elements. The
following code declares and fills a two-dimensional array:
32. Working with Arrays and Array Lists
• When you work with collections, you often do not know the
number of items they need to contain until runtime. This is where
the ArrayList class fits in. The capacity of an array list automatically
expands as required, with the memory reallocation and copying of
elements performed automatically.
34. Implementing the Data Access Layer
• Introducing ADO.NET
• To access and work with data in a consistent way across these various data stores, the .NET
Framework provides a set of classes organized into the System.Data namespace. This
collection of classes is known as ADO.NET.
• Establishing a Connection
35. Executing a Command
A Command object stores and executes command statements against the database. You can use the Command object to execute
any valid SQL statement understood by the data store. In the case of SQL Server, these can be Data Manipulation Language
statements (Select, Insert, Update, and Delete), Data Definition Language statements (Create, Alter, and Drop), or Data Control
Language statements (Grant, Deny, and Revoke)
37. Using Stored Procedures
When executing a stored procedure,
you often must supply input
parameters. You may also need to
retrieve the results of the stored
procedure through output
parameters. To work with
parameters, you need to instantiate
a parameter object of type
SqlParameter, and then add it to the
Parameters collection of the
Command object.
40. Populating a DataTable from a SQL Server
Database
• The Load method of the
DataTable fills the table
with the contents of a
DataReader object.
41. Populating a DataSet from a SQL Server
Database
• Create a separate DataAdapter for each
DataTable. The final step is to fill the
DataSet with the data by executing the Fill
method of the DataAdapter. The following
code demonstrates filling a DataSet with
data from the publishers table and the
titles table of the Pubs database
47. Data Binding in Windows-Based GUIs
• To bind a control to data, you need a data source object. The DataContext of a container control
allows child controls to inherit information from their parent controls about the data source that
is used for binding. The following code sets the DataContext property of the top level Window
control.
48. Creating and Using Control and Data
Templates
• In WPF, every control has a template that manages its visual appearance. If you don’t explicitly set
its Style property, then it uses a default template. Creating a custom template and assigning it to
the Style property is an excellent way to alter the look and feel of your applications.
52. Web Server Control Fundamentals
• The .NET Framework provides a set of Web server controls specifically for hosting within a Web
Form. The types of Web server controls available include common form controls such as a
TextBox, Label, and Button, as well as more complex controls such as a GridView and a Calendar.
The Web server controls abstract out the HTML coding from the developer. When the Page class
sends the response stream to the browser, the Web server control is rendered on the page using
the appropriate HTML.
53. Using the Visual Studio Web Page Designer
The Visual Studio Integrated Development Environment (IDE) includes an excellent Web Page Designer. Using
the designer, you can drag and drop controls onto the Web Page from the Toolbox and set control properties
using the Properties window.
55. Control Events
• When an event occurs on the client—for example, a button click—the event information is
captured on the client and the information is transmitted to the server via a Hypertext Transfer
Protocol (HTTP) post. On the server, the event information is intercepted by an event delegate
object, which in turn informs any event handler methods that have subscribed to the invocation
list.
56. Understanding Application and Session Events
• Along with the Page and control events, the .NET
Framework provides the ability to intercept and
respond to events raised when a Web session starts
or ends. A Web session starts when a user requests
a page from the application and ends when the
session is abandoned or it times out. In addition to
the session events, the .NET Framework exposes
several application-level events. The
Application_Start event occurs the first time
anyone requests a page in the Web application.
Application_BeginRequest occurs when any page or
service is requested. There are corresponding
events that fire when ending requests,
authenticating users, raising errors, and stopping
the application. The session-level and application-
level event handlers are in the Global.asax.cs code-
behind file.
57. Using Query Strings
• While view state is used to store information between post backs to the same page, you often
need to pass information from one page to another. One popular way to accomplish this is
through the use of query strings. A query string is appended to the page request URL
58. Using Cookies
• You can use cookies to store small amounts of data in a text file located on the client device. The
HttpResponse class’s Cookie property provides access to the Cookies collection. The Cookies collection
contains cookies transmitted by the client to the server in the Cookies header. This collection contains
cookies originally generated on the server and transmitted to the client in the Set-Cookie header. Because
the browser can only send cookie data to the server that originally created the cookie, and cookie
information can be encrypted before being sent to the browser, it is a fairly secure way of maintaining user
data. A common use for cookies is to send a user identity token to the client that can be retrieved the next
time the user visits the site.
To read a cookie, you use the Request object.
59. Maintaining Session and Application State
• Session state is the ability to maintain information pertinent to a user as they request the various pages
within a Web application. Session state is maintained on the server and is provided by an object instance of
the HttpSessionState class.
• Although session state is scoped on a per-session basis, there are times a Web application needs to share a
state among all sessions in the application. You can achieve this globally scoped state using an object
instance of the HttpApplicationState class. The application state is stored in a key-value dictionary structure
similar to the session state, except that it is available to all sessions and from all forms in the application.
60. Data-Bound Web Controls
• Data-bound controls automate the process of presenting data in a web form. ASP.NET provides
various controls to display the data depending on the type of data, how it should be displayed,
and whether it can be updated. Some of these controls are used to present read-only data, for
example, the Repeater control displays a list of read-only data. If you need to update the data in a
list, you could use the ListView control. If you need to display many columns and rows of data in a
tabular format, you would use the GridView control. If you need to display a single record at a
time, you could use the DetailsView or the FormView controls.
62. Model Binding
• To configure a data control to use
model binding to select data, you
set the control’s SelectMethod
property to the name of a method
in the page’s code. In the case
above, the SelectMethod is set to
the GetEmployee method which
returns a list of Employee objects.
The data control calls the method
at the appropriate time in the
page life cycle and automatically
binds the returned data.