Bill`s Big Idea - scottlaing.net
Download
Report
Transcript Bill`s Big Idea - scottlaing.net
Basics of .NET Framework and C#
Phoenix Java Users Group Presentation
12/10/2003
Scott Laing
Microsoft’s Vision of the Future
Increased Internet
Evolution
Information Services will
become prevalent
XML and SOAP as likely
communication
mechanism
Out of the Browser
Richter’s Restaurant
Broker Piece Example
Idea: Create new Platform
Need dev tools to more
easily create applications
and services.
XML Services and more
XML/SOAP support in
general needed.
Integration with .NET My
Services
.NET My Services
Passport
.NET Alerts
.NET ApplicationSettings
.NET Calendar
.NET Contacts
.NET Inbox
.NET Locations
.NET Services
.NET Wallet
etc…
Idea: some services free, some
subscription based in future
(Richter)
Other Difficulties in Windows Dev
Several competing
dev platforms (ATL,
MFC, VB, COM)
COM difficulties
DLL Hell
Registry, Setup
Complexities
Ideas from Java
High Level Well Designed
Class Library (FCL)
Platform Independence
via IL (= Intermediate
Language, new buzzword
for .NET VM code,
evolution of bytecode
concept)
Garbage Collection
Type-safe design
Rehash: Design Goals for .NET
Avoid existing Windows
weaknesses (DLL Hell, COM
design difficulties)
Faster and easier development
than existing tools
Built in Web Service abilities,
XML/SOAP
Integrate new improvements
seen in Java, other languages
Other issues: Revised
ASP.NET, Improved GUI dev
platform, expanded dev
features
.NET Framework Basics
Two core pieces:
FCL – Framework
Class Library
CLR – Common
Language
Runtime
FCL Examples
System
System.Collections
System.IO
System.Net
System.Threading
System.Xml
System.Reflection
C# using = java import,
similar behavior
CLR
JIT Compiler behavior (like
java)
Performance issues, new tricks
and enhancements
Similar to AL* without register
references
Multiple-CPU sensitive
optimizations
ILDasm.exe – nice
disassembly tool
Nomenclature: Managed Code
(code running under the CLR)
Ngen.exe utility (can compile
to native code)
*native Intel 8086 Assembly Language
Managed Code Up Close
EXE code is still in PE*
format
Managed EXE contains:
1.
2.
3.
PE Header
CLR Header
Metadata
4.
Lists all types referenced in
project
Ensures type safety
Helps with garbage
collection
IL Code
IL handled on fly by
invocation of _CorExeMain
function in MSCorEE.dll
system DLL.
*Portable Executable format is normal native windows EXE format
CTS – Common Type System
Nomenclature: think .NET “type” =
java “class” (.NET has structures)
Type members
Visibility options
Field
Method
Property
Event
Public
Private
Family (C# protected)
Assembly (C# internal)
Family and Assembly (ILAL* only)
Family or Assembly (C# protected
internal)
All types derive from
System.Object
*C# does not support this but native .NET IL does, you can create with IL Assembly Language (Richter)
CLS - Common Language Specification
Subset of CLR/CTS
Less types
Less features
Allows Complete Language
interoperability between
VB.NET, C#.NET, J#.NET,
etc.
CLS can be ignored if
Language Interoperability
not big issue (e.g. C# only
project)
Visual Studio.NET Enhancements
.NET development not limited
to VS.NET but in practice it is
useful.
Unified interface for VB and C#
now, unification of two camps.
Misc GUI and debugger
improvements
ASP.NET, XML Services made
much easier to create.
Can create Windows Services
Basic Database Design
manipulation abilities built in
Global and local configuration
files in XML format
Creating New Project Example
Unified interface to
create different types
of applications
One stop shop to
create Windows
Application, Class
Library, ASP.NET
Web Service,
ASP.NET Application,
Windows Service,
etc.
Just choose which
language and go go
go…
This is a recent Windows Form Application I am working on, to show the new .NET dev environments look and feel.
Note: New GUI Feature, DataField and DataSet are custom properties I added to
this Form class, and they automatically show up in the GUI Designer (see
Properties description later in presentation)
Assemblies
“An assembly is a collection of
one or more files containing
type definitions and resource
files” – Richter
One assembly file contains the
manifest
Manifest lists
Name of files in assembly
Assembly version
Culture
Publicly exported types
Publisher
Assemblies (2)
CLR operates on
Assemblies, first loads file
with manifest, then from
manifest loads sub files.
Can contain resource
files (.GIF, .JPG)
Assembly new basic unit
- not EXE or DLL:
“You might think of an
assembly as a logical EXE or
a DLL” - Richter
Assembly Flavors
Strongly Named
Assemblies
Private or “weakly named”*
Assemblies
Internal structure the same
apart from presence or
absence of cryptographic
signature (both types are
normal .NET PE format)
*Richter uses the term “weakly named” as a useful term for private assemblies, it is not an official MS term
Strongly Named Assemblies
Globally available (by any other EXE
or Assembly on machine)
Uniquely Identified by:
Filename
Version Number
Culture Identity
Public Key Token
Must be signed
Use: SN –k keyfile.keys
Requires some coding mods
(attribute referencing key*)
After signing Assembly you can:
Store in GAC** (Global Assembly
Cache)
This is often located in:
c:\windows\Assembly\GAC
Install with GACUtil.exe or
Drag file into GAC folder
*See example later in Attributes coverage
**Liberty says you should pronounce this “GACK”
Private (Weakly Named) Assemblies
Typically not signed
Must be contained in
folder or subfolder of App
referencing them (not
publicly available)
Simple deployment: can
use xcopy to simply copy
files and subfolders to
new computer
(nomenclature: xcopy
deployment)
Type Basics
Reference types, Value types
(similar to java)
Reference types pointer to object
on the heap, can be set to null
Value types kept on stack
All derive from System.Object
(even int or double – value types)
Can cast value type to a reference
type (boxing), and then cast back.
Value types passed by value,
reference types by reference as
with java
Garbage collection as with java for
ref types
C# type
Size
(bytes)
.NET type
Range
byte
1
Byte
0-255
char
2
Char
Unicode
char
bool
1
Boolean
True/false
sbyte
1
Sbyte
-128/127
short
2
Int16
-32K/+32K
ushort
2
Uint16
0-65K
int
4
Int32
-2B/+2B
uint
4
Uint32
0-4B
float
4
Single
IEEE float
double
8
Double
IEEE
double
decimal
12
Decimal
28 digits
fixed
precision
long
8
Int64
+-9e16
ulong
8
UInt64
0-1.8e17
Misc Type Stuff
References default to null, but
will throw exception if not
assigned before use (in C#).
enum, static, const all the
usual suspects
Structures (struct) are allowed,
sort of a crippled* class passed
by value (for short-lived
classes with value type
members)
*crippled is not an official MS term but strictly my own =/
Java Similarities Beyond Count
Interfaces
method overloading
Nested classes
private static main() = entry
function.
namespace = package
using = import
exceptions
String type (immutable)
abstract classes
streams, threads, net, file
classes all similar
etc. etc.
Properties
Properties act like a field (i.e. normal class instance
variable) but are implemented internally via get, set
functions.
They allow for validation and other special actions on
assignment or retrieval (e.g. loading or setting value
from/to database).
Sample invocations from code:
// set method call:
Time1.Hour = value;
// get method call:
var2 = Time1.Hour;
Objects purists are happy (encapsulation is respected)
Simple property example:
public class Time
{
…
// publicly exposed property Hour
public int Hour
{
get
{
return hour;
}
set
{
// value is default rvalue on assignment
hour = value;
}
}
// hidden inner value
private int hour;
}
Coding Candy
foreach operator
Useful regular expression
class (Regex)
Indexers (similar to overriding
the [ ] or “bracket” operator in
C++)
is a operators like C++
Nice collection classes
(stack, queue, dictionary –
key/value pair*, etc.)
Nice wrapping of Win32
stuff, e.g. thread class,
registry class
SMTP mail class built in
*Like Perl’s associative array or “hash” type
Attributes
Syntactical “sugar”
“You can consider attributes
Annotations that you intersperse
throughout your code.” - Balena
Format:
[MyAttrib(param1, param2)] public class modifiedClass
Or
[NoParamAttrib] public class modClass2
Examples:
For a signed assembly, must use this
Assembly level Attribute
[AssemblyKeyFile(“c:\\myKey.key”)]*
To allow a class to be serialized use:
[Serializable] attribute
*Note: Assembly attributes must be placed after any using statements and before any code, and technically this should be [assembly:
AssemblyKeyFile(“mykeyfile.key”)], the assembly: part specifies that this attribute applies to the assembly itself (e.g. not the first class).
Attributes (2)
Examples (cont.):
Can use [StructLayoutAttrib()] to create a
C# structure that emulates a C union
(Balena example)
Can use [Conditional(“DEFINED_VAL”)] to
mark off code as being ignored if
preprocessor DEFINED_VAL Is not
defined. (Balena example)
Note: Attributes can apply to class,
method, field, property, assembly, etc.
You can create your own custom
classes and use the Attribute FCL
class to access your own custom attrib
related info, e.g. use a function call
like: Attribute.GetCustomAttributes(classType, AttType) to
grab your custom attribute info.
Good examples of Custom Attributes
in Liberty’s Programming C#.
ADO.NET
Yes another data interface (hmm…
ADO, RDO, OLE-DB, ODBC, DAO…)
…but, some nice features:
New DataAdapter adds flexibility.
Can read data from one source
write to totally different data
source with little work.
Data-aware controls make simple
operations easy to code for both
Windows.Forms and ASP.NET.
No server side locked cursors
supported (probably a good thing)
But can still use old ADO via COM
InterOp. to do this.
Designed for MS SQL Server
(surprise) but now can work native
commands with any Odbc datasource*
as well.
*Although not well documented in earlier .NET tomes you can now download Microsoft.Data.Odbc class to get similar functions to the Sql* and
OleDb* function calls, via similar Odbc* functions, e.g. OdbcDataAdapter replaces OleDbAdapter in functionality, etc. Note SqlDataAdapter =
MS SQL Server function, OleDbAdapter = OleDb provider (e.g. Access) function. Most ADO.NET database related functions follow this pre-fixed
function format, of either: Sql*, or OleDb*, or via download you can now use Odbc* functions as well).
ASP.NET
Welcome to GUI-land ASP (cheers)
Customizes behavior per Browser and
can use CSS files to display pages
better
Event model similar to
Windows.Forms class but more
limited, e.g. only subset of events
supported per html behavior
Can use .NET controls (e.g. native
controls) in intranet or other
environment where you can assume
client has .NET.
Can work in GUI Design mode
(dragging, dropping controls onto
form like VB), in event firing Code
mode, and also in raw HTML code.
You can use C#.NET – or any .NET
language (big improvement over
VBScript).
C# vs. J#
Yes coffee lovers there is a J#
Not with .NET install but can be
downloaded and installed as normal
.NET language.
J# good possibility esp. for porting
existing Java or VJ++ projects.
There are upgrade utilities for both java
to C# and java to J# (Nanda).
C# converter will require more manual
fixits, J# much less, however C# is
closer to native FCL which has its
advantages.
More details: see article by Nitin Nanda
in javaworld for in-depth coverage of J#
and porting Java to J# vs. porting Java
to C#. Nitin Nanda's Article
Note: underlined elements may be hyperlinks where available (YMMV)
Personal Impressions.NET
ASP.NET new VB like interface is very
very nice for quick web dev., big
improvement over old ASP.
XML Services are very easy to make
(example handout).
Writing .NET app to access .NET XML
Service makes SOAP/HTTP stuff
completely seamless /transperant.
ADO.NET data model has more
flexibility and is a nicer model than
previous MS data models (old ADO).
Being able to customize property
designer windows during Design
Mode, etc., are pleasant.
C# is a reasonable language, big
improvement to MFC/ATL/VC++/VB dev
platforms for most projects.
Epilogue: Food for Thought
How does using .NET XML Services
compare to BEA Platform? To other
similar java mechanisms to integrate
Web Services with client apps? EJB?
Is extra setup work to setup the
HTTP/SOAP connectivity not a big deal
for people doing similar things with
java using tools or by hand?
Will .NET platform ever be seriously
used under other platforms? Will it be
subtly crippled?
Are people using EJB a great deal,
BMP or CMP?
Will the future involve integration of
java HTTP/SOAP communications with
.NET and have both integrate and
control separate companies or app
spaces? How does recent IBM/MS joint
web integration initiative affect this.
Bibliography
Programming C# 2nd Ed., Jesse Liberty,
O’Reilly & Associates (best C# book period)
Applied Microsoft .NET Framework
Programming, Jeffrey Richter, Microsoft
Press (absolutely essential for a real detailed in
depth understanding of .NET)
Programming Visual Basic .NET, Francesco
Balena, Microsoft Press (excellent examples,
excellent asp.net and ado.net coverage)
Visual Basic .NET How To Program 2nd Ed.,
Deitel & Deitel, Prentice Hall (some basic
ASP.NET stuff was helpful in this)
Migrating Java applications to .Net,
Nitin Nanda’s excellent article in JavaWorld
on C# and J#:
http://www.javaworld.com/javaworld/jw-01-2003/jw-0103migration.html
Mistakes and errata: strictly my own.
Feedback welcome: Scott Laing
<[email protected]>