C#: Introduction for Developers
-
Upload
arden-burke -
Category
Documents
-
view
30 -
download
4
description
Transcript of C#: Introduction for Developers
![Page 2: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/2.jpg)
Tonight’s Agenda
Interfaces Generics Code Organization Databases ADO.NET Datasets Q&A
![Page 3: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/3.jpg)
CHAPTER 15
INTERFACES
![Page 4: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/4.jpg)
What’s an interface? It’s like a class with…
No member dataAll methods are publicAll methods are abstract
An interface implies nothing about the implementation behind it
A class can only inherit from one class A class can implement multiple interfaces
Interfaces often represent only a portion of a class’ functionality
![Page 5: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/5.jpg)
Why interfaces? Interfaces are better suited to situations in which
your applications require many unrelated object types to provide certain functionality
Interfaces are more flexible than base classes because you can define a single implementation that can implement multiple interfaces.
Interfaces are better in situations in which you do not want or need to inherit implementation from a base class.
Structures cannot inherit from classes, but they can implement interfaces.
![Page 6: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/6.jpg)
Common .NET Interfaces
IComparableUsed to compare two objectsSorting algorithms
IDisposableFrees unmanaged resources when an
object goes out of scope If your classes implement these
interfaces, they can be used wherever these interfaces are specified
![Page 7: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/7.jpg)
Example Interfacespublic interface IStreamable{ bool Read(FileStream inInput); bool Write(FileStream inOutput);}
public interface IHTMLDisplayable{ void Render(OutputStream inStream);}
![Page 8: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/8.jpg)
Implementing Interfacespublic interface IStreamable{ bool Read(FileStream inInput); bool Write(FileStream inOutput);}
public class MyObject : IStreamable{ public bool Read(FileStream inInput) { } public bool Write(FileStream inOutput) { }}
![Page 9: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/9.jpg)
Implementing ICloneable// Duplicate any cloneable objectpublic List<object> Duplicator(ICloneable src, int count){ List<object> list = new List<object>(); for (int index = 0; index < count; ++index) { list.Add(src.Clone()); } return list;}
![Page 10: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/10.jpg)
Looking at IComparable
Any object that implements IComparable can be sorted with the generic Array.Sort() method
![Page 11: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/11.jpg)
CHAPTER 15, PART 2
GENERICS
![Page 12: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/12.jpg)
What’s a generic class?
A class definition that doesn’t explicitly declare all of the types it uses internally
Allows creation of new types by specifying those internal types later
![Page 13: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/13.jpg)
Using Generic Types
Generic types are declared using <> after the type nameInstead of the general purpose interface:
IComparableUse the type-specific interface:
IComparable<T>
class Temperature : IComparableclass Temperature : IComparable<Temperature>
![Page 14: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/14.jpg)
Generics Exampleclass Average<T>{ public void Include(T inValue);
public T Average { get { ... } }}
Average<int> integerAverage;Average<double> doubleAverage;
![Page 15: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/15.jpg)
Updating IComparable
Instead of comparing generic objects, we can compare Temperature objects
![Page 16: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/16.jpg)
Common Generics
Collection classesList<T>SortedList<K, V>
Enumeration (foreach)IEnumerable<T>IEnumerator<T>
![Page 17: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/17.jpg)
Generic Constraintspublic class MyGeneric<T> where T: class{ // T is a class (can be assigned null)}
public class MyGeneric<T> where T: class, IComparable<T>{ // T implements IComparable interface}
public class MyGeneric<T> where T: struct{ // T is a struct}
public class MyGeneric<T> where T: new(){ // T has a default constructor}
![Page 18: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/18.jpg)
CHAPTER 16
CODE ORGANIZATION
![Page 19: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/19.jpg)
Code Organization
Multiple classes in a single fileClosely related classes (e.g. EventArgs)
Nested classesObjects that only exist within the context of
another type of object
![Page 20: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/20.jpg)
Partial Classes
Split a single class over multiple filesForms split Designer code into separate filePossible to split interface implementations
into separate files Partial classes must have the same
visibility Partial methods split the declaration and
implementation across files
![Page 21: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/21.jpg)
Namespaces
Organize classes into logical groupings Avoid type name collisions using <namespace> <namespace>.<ClassName>
![Page 22: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/22.jpg)
Nested Namespaces
using ns1{ using ns2 { }}
using ns1.ns2{}
![Page 23: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/23.jpg)
CHAPTER 16, PART 2
DOCUMENTING CLASSES
![Page 24: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/24.jpg)
XML Documentation
Standardized documentation format for C# code
Type three slashes “///” before a method implementation or class member
<summary> tag is used by IntelliSense Processed by third party tools
DoxygenSandcastle
![Page 25: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/25.jpg)
CHAPTER 16, PART 3
CLASS LIBRARIES
![Page 26: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/26.jpg)
Class Libraries
Share class across multiple projects Projects “reference” other projects
Move SportsTeam into a class library
![Page 27: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/27.jpg)
CHAPTER 17
DATABASE PROGRAMMING
![Page 28: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/28.jpg)
Databases
Client-server architectureOne server, many clientsServer runs Microsoft SQL ServerClients use ADO.NET 4
Relational databasesSQL (Structured Query Language)
![Page 29: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/29.jpg)
Tables
Tables store data One or more records (rows) A primary key uniquely identifies each
row Indexes provide an efficient way to
access data based on values in specific columns
![Page 30: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/30.jpg)
“Relations” Among Tables Key columns are used to relate tables Foreign keys in one table correspond to
primary keys in another tableOne-to-manyOne-to-oneMany-to-many
![Page 31: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/31.jpg)
Table Columns
Columns are defined by a name and data typebitchar, varchar, textdatetime, smalldatetimedecimal, numericfloat, realbigint, int, smallint, tinyintmoney, smallmoney
![Page 32: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/32.jpg)
Column Values
null (maybe, maybe not – depends on column definition)
Default value Identity column (auto-increment) Constraints
![Page 33: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/33.jpg)
SELECT
Select data from a databaseSELECT column_name,column_name FROM table_name;
SELECT * FROM table_name;SELECT title, publisher FROM books;
![Page 34: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/34.jpg)
INNER JOIN
Select all rows from two table where specified columns have matching valuesSELECT column_name(s)FROM table1INNER JOIN table2ON table1.column_name=table2.column_name;
SELECT column_name(s)FROM table1JOIN table2ON table1.column_name=table2.column_name;
INNER JOIN is the same as JOIN
![Page 35: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/35.jpg)
ADD, UPDATE, DELETE
Add a row to a table INSERT INTO table_nameVALUES (value1,value2,value3,...);
INSERT INTO table_name (column1,column2,column3,...)VALUES (value1,value2,value3,...);
Update a row in a table UPDATE table_nameSET column1=value1,column2=value2,...WHERE some_column=some_value;
Delete a row from a table DELETE FROM table_nameWHERE some_column=some_value;
![Page 37: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/37.jpg)
ADO.NET
![Page 38: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/38.jpg)
ADO.NET
Data providers implement a common API to various database serversSQL ServerOLE DBODBCOracle
Third partyMySQLSQLite
![Page 39: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/39.jpg)
Components
Database server .NET data provider
Connection○ Connects to a specific database
Command○ SELECT, UPDATE, etc.
DataReader, DataAdapter○ Help retrieve data from a query
![Page 40: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/40.jpg)
Datasets
Store data in a disconnected cache Data source may be a database, XML
data, local application data Mimics the structure and behavior of a
relational database
![Page 41: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/41.jpg)
Datasets Mimic Relational DB
Dataset contains a collection of tablesNot necessarily the same table from the databaseMay be a subset of columns and rowsMay be joined to another table
Tables contain a collection of columns Tables contain a collection of rows Tables contain a collection of constraints Dataset contains a collection of relations Everything is accessed through object
properties
![Page 42: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/42.jpg)
Alternatives to Datasets
You can use Command and Connection objects directlySelect, Insert, Update, Delete
![Page 43: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/43.jpg)
Database Concurrency Multiple clients accessing data ADO.NET datasets are “disconnected” Pessimistic concurrency
Lock database records to prevent conflicts Optimistic concurrency
Check for data changes before writingThrow an exception if data has changed
“Last in wins”Data is written by last operationData may be lost
![Page 44: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/44.jpg)
Avoid Concurrency Issues Update and refresh datasets frequently Avoid updating large tables in datasets Only reduces risk!
You still must handle the exceptions
![Page 45: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/45.jpg)
CHAPTER 18
CREATING DATASETS
![Page 46: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/46.jpg)
DATABASE CHECK
![Page 47: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/47.jpg)
Populating a Database
SQLExpress should be installed with Visual Studio
The book provides a .sql file for populating the MMABooks database in SQLExpress
Double-click the .bat file on the S: drive We’ll need to repeat this process at the
start of each class session
![Page 48: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/48.jpg)
Confirm Database Access Using Visual Studio to locate the new
database as a Data SourceView > Server ExplorerAdd Connection...Server name: .\SQLEXPRESSDatabase name: MMABooksTest Connection
![Page 49: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/49.jpg)
Using the Data Sources Window
![Page 50: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/50.jpg)
Dataset Summary
Instead of a database, we can pull data from:WCF Data ServicesCustom objectsSharePoint
Entity FrameworkObject-relational mapping framework (ORM)Maps database table data to C# objects
○ Object instance represents a table row
View > Server Explorer
![Page 51: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/51.jpg)
Dataset Summary
Connection strings can be stored in app.config
Allows reuse of the connection string A dataset can be modified in Visual
Studio to add tables, columns, etc. Visual Studio generates .xsd schema
files for the dataset
![Page 52: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/52.jpg)
CHAPTER 18, PART 2
USING DATASETS
![Page 53: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/53.jpg)
DataGridView Control
Bound control Table view of the dataset Multiple objects are added to the form
![Page 54: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/54.jpg)
Other Bound Controls
Change default data control for table in Data Sources
Change default data control for columns in Data Sources
![Page 55: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/55.jpg)
Behind the Scenes…
Load event is updated for the form Save click event is updated for the
binding navigator
![Page 56: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/56.jpg)
Bound TextBox Controls
Formatting and Advanced Binding Select TextBox Open Properties Window Expand DataBindings property Select Advanced option, click “…”
Select new format type Specify representation of null value
![Page 57: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/57.jpg)
Bound ComboBox Controls Populate a ComboBox with values from
a column of a database table SelectedItem is used to specify the
value in a column of another database table
![Page 58: C#: Introduction for Developers](https://reader035.fdocuments.us/reader035/viewer/2022081515/568135cb550346895d9d2e50/html5/thumbnails/58.jpg)
Code Practice Select customer state using dropdown list
ComboBox instead of TextBox
Create StatesDataSet in Data Source window Add DataSet control for StatesDataSet and set
DataSetName property Add BindingSource control for DataSet and set
DataSource/DataMember properties Set State field to use ComboBox Set ComboBox to use data bound controls Clear ComboBox data bindings for Text property