CSharp
CSharp
C++ Developers!
Struct is not a class with everything being public
Example: struct Point
{
double X;
double Y;
void MoveBy(double dX, double dY)
{ X+=dX; Y+=dY; }
}
Properties
Mix between fields and methods
Use properties for:
Implementation of read-only members
(by omitting set)
Validation on assignment
Calculated or composed values
Exposing values on interfaces
Example: string Name
{
get { return name; }
set { name = value; }
}
Indexers
Consistent way to build containers
Build on properties idea
Allow indexed access to contained objects
Index qualifier may be of any type
Example: object this[string index]
{
get { return [Link](index); }
set { [Link](index,value); }
}
Delegates and Events
Delegates
Similar to function pointers found in C and C++
Strongly typed, no type-cast confusion
delegate void Clicked(Element e, Point p);
Events
Language-intrinsic event model (at last!)
Declare and call in the event-source class
Consume by adding handler in sink
[Link] += MyClickHandler;
All management done by C#
event Clicked OnClicked;
...
OnClicked(this,PointerLocation);
Attributes
Similar to attributes known from IDL
Declarative access to functionality
Extensible through custom attributes
Allow code augmentation with:
Hints for the runtime environment
[Transaction([Link])]
class MyBusinessComponent { ... }
Declarative semantics
Very not C:
lock(<object>){ ... };
Language inherent critical section synchronization
checked {...}; unchecked { ...};
Integer overflow protection
Collections Built-in:
foreach
Straightforward support for iterating over collections
Can be used for arrays and other collections
Can also be used with any custom class
Implements IEnumerable with GetEnumerator()
Returning object implementing IEnumerator
Example: Point[] Points = GetPoints();
foreach( Point p in Points )
{
[Link](p.x,p.y);
}
Operators
Very C:
Logical/conditional: && || ^
Arithmetic: * / + - % << >>
Relational: == != < > >= <=
Not exactly C:
For bool: & and | are logical with full evaluation
For integer: & and | perform binary AND/OR
Very un-C:
is Tests run-time type
as Converts a value to a specified type
typeof Retrieves run-time type
Operator Overloading
Most operators can be overloaded
Arithmetic, relational, conditional, and logical
No overloading for
Assignment operators
Special operators (sizeof, new, is, typeof)
Example: Total operator +(int Amount, Total t)
{
[Link] += Amount;
}
Access Protection
Adopts C++ model
public Everyone may call or access
protected Only members may access
private Only members of exactly this class
Expands C++ model
sealed Can't use as base class
internal Public access only within assembly
protected internal Protected in assembly
“Pointers, I need
pointers!”
C# supports
Intrinsic string type
Rich garbage-collection model
By-reference parameters using ref
void increment(ref int value, int by)
Outbound parameters using out
bool add(int a, int b, out int c)
Eliminates vast majority of C++ uses of pointers
Pointers are available for code marked unsafe
unsafe void crypt(byte[] arr)
{
byte * b = arr;
...
}
Boxing and Unboxing
By-value types can be "boxed" and "unboxed"
"Boxing" allows by-value types to travel by-ref
Based on objectness of all types.
Think: Throw value in box and reference the box
double Value;
42 42
// Boxing
object BoxedValue = Value;
// Unboxing
42 Value = (double)BoxedValue;
Boxed: Unboxed:
Reference Copy
Garbage Collection 1/2
IIS
Web
SystemFramework
[Link]
BusinessFacade
BusinessRules
DataAccess
[Link]
T Database
Common Components
[Link]
Contains systems configuration options
Contains common data definitions (classes)
Subnamespace [Link]
"Internal" data representation for Book, Category,
Customer, OrderData
[Link]
Diagnostics utilities
Pre and post condition checking classes
Dynamic configuration
In short:
Everything that's pure tech and not business code
[Link]
Contains all database-related code
Uses [Link] architecture
Using SQL Server managed provider
Shows DataSet, DataSetCommand usage
Optimized for performance by using stored procs
[Link]
s
Implements all business rules
Validation of business objects (for example,
Customer EMail)
Updating business objects
Calculations (Shipping Cost, Taxes)
All data access performed through DataAccess
[Link]
de
Implements logical business subsystems
CustomerSystem: Profile management
OrderSystem: Order management
ProductSystem: Catalog management
Reads data through DataAccess
Data validated and updated using BusinessRules
BusinessFacade encapsulates all business-related
functionality
[Link]
Implements the user interface for Web access
Uses [Link] architecture
Employs Web Forms model
Uses code behind forms
Manages state
Uses custom Web controls
All functionality accessed through BusinessFacade
Shop at Duwamish
[Link]
Demo: Duwamish in Action
Exploring
DuwamishOnline C#
Exploring C# Features in Duwamish Online
Extending
DuwamishOnline C#
Extending Duwamish Online Using C#
To C# from Visual Basic
A very brief introduction to the C language
family syntax for Visual Basic developers
C
Core principles: Be brief. Be expressive.
Relatively few and short keywords
Uses symbol ASCII characters instead of words
Core element: "{" The Block "}"
Code groups for structural or flow-control elements
a = add( 2, 3 ); a=
" +|,c6!)f$b(h*c6!(d'b(i)d5!(b*a'`&c"
")c5!'b+`&b'c)c4!&b-_$c'd*c3!&a.h'd+"
"d1!%a/g'e+e0!%b-g(d.d/!&c*h'd1d-!(d%g)"
"d4d+!*l,d7d)!,h-d;c'!.b0c>d%!A`Dc$![7)35E"
"!'1cA,,!2kE`*!-s@d(!(k(f//g&!)f.e5'f(!+a+)"
"f%2g*!?f5f,!=f-*e/!<d6e1!9e0'f3!6f)-g5!4d*b"
"+e6!0f%k)d7!+~^'c7!)z/d-+!'n%a0(d5!%c1a+/d4"
"!2)c9e2!9b;e1!8b>e/! 7cAd-!5fAe+!7fBe(!"
"8hBd&!:iAd$![7S,Q0!1 bF 7!1b?'_6!1c,8b4"
q = a – 1; add
"!2b*a,*d3!2n4f2!${4 f. '!%y4e5!&f%"
"d-^-d7!4c+b)d9!4c-a 'd :!/i('`&d"
";!+l'a+d<!)l*b(d=!' m- a &d>!&d'"
"`0_&c?!$dAc@!$cBc@!$ b < ^&d$`"
":!$d9_&l++^$!%f3a' n1 _ $ !&"
"f/c(o/_%!(f+c)q*c %! * f &d+"
"f$s&!-n,d)n(!0i- c- k) ! 3d"
"/b0h*!H`7a,![7* i] 5 4 71"
"[=ohr&o*t*q*`*d *v *r ; 02"
out( q ); (2,3
"7*~=h./}tcrsth &t : r 9b"
"].,b-725-.t--// #r [ < t8-"
"752793? <.~;b ].t--+r / # 53"
"7-r[/9~X .v90 <6/<.v;-52/={ k goh"
"./}q; u vto hr `.i*$engt$ $ ,b"
";$/ =t ;v; 6 =`it.`;7=` : ,b-"
"725 = / o`. .d ;b]`--[/+ 55/ }o"
"`.d : - ?5 / }o`.' v/i]q - "
"-[; 5 2 =` it . o;53- . "
q--; );q=a
"v96 <7 / =o : d =o"
"--/i ]q-- [; h. / = "
"i]q--[ ;v 9h ./ < - "
"52={cj u c&` i t . o ; "
"?4=o:d= o-- / i ]q - "
"-[;54={ cj uc& i]q - -"
"[;76=i]q[;6 =vsr u.i / ={"
"=),BihY_gha ,)\0 " , o [
3217];int i, r,w,f , b ,x ,
return; –1;out
p;n(){return r <X X X X X
768?d[X(143+ X r++ + *d ) %
768]:r>2659 ? 59: ( x = d
[(r++-768)% X 947 + 768] ) ?
x^(p?6:0):(p = 34 X X X )
;}s(){for(x= n (); ( x^ ( p
?6:0))==32;x= n () ) ;return x ; }
void/**/main X () { r = p
=0;w=sprintf (X X X X X X o
(q);q—-
,"char*d="); for ( f=1;f < * d
+143;)if(33-( b=d [ f++ X ] )
){if(b<93){if X(! p ) o
[w++]=34;for X(i = 35 +
(p?0:1);i<b; i++ ) o
[w++]=s();o[ w++ ]
=p?s():34;} else X
{for(i=92; i<b; i
++)o[w++]= 32;} }
;return;
else o [w++ ]
=10;o [
w]=0 ;
puts(o);}
"This too" Credits: Don Yang, Winner "Best Layout" International Obfuscated C Contest [Link]
Declaration Principles
Core declaration principle: "What then who"
What: data type or declaration keyword
Types: int, string, decimal, float
Keywords: enum, struct, class
Who: user-defined identifier
Must begin with a letter, may contain numbers, "_"
Attention: All identifiers are case sensitive
switch (i)
{
if (i == 0 ) case 0:
{ [Link]("This");
[Link]("This"); break;
} case 1:
else [Link]("That");
{ break;
[Link]("That"); default:
}; [Link]("Else");
break;
}
Loops
Counted loops
for(<pre>;<while>;<increment>) <block>
<pre>: Setting precondition "i=0"
<while>: Testing continuation condition "i<10"
<increment>: Calculating counter state "i++"
While loops
while (<expression>) <block>
do <block> while (<expression>);