Professional Documents
Culture Documents
A Component-Oriented Language For The Microsoft® .NET Framework
A Component-Oriented Language For The Microsoft® .NET Framework
Objectives
Introduction
to the C# Language
Syntax Concepts
Technologies
Overview
Contents
Section Section Section Section
Summary
Section 1: C# Overview
Component-oriented
Component Why
Systems
Concepts of .NET
C#?
Component-oriented Systems
COM
Most successful component model in history Only viable commercial component platform
However:
Makes some assumptions of binary layout Supports "least common denominator" type system only
Add
Why C# ?
First
component-oriented language
Namespaces
Versioning Attribute-driven development
Power
Minimal Much
More
More
Section 2: Elements of C#
Shape
Understanding Understanding
Similar
All
Conditional
Type System
Builds Native
Core
concepts:
Everything implicitly inherits from System.Object
Everything is an object
Simple Types
Integer
Types
byte, sbyte (8bit), short, ushort (16bit) int, uint (32bit), long, ulong (64bit)
IEEE
Exact
Numeric Type
Types
Character
Boolean
Type
Enums
Named
Strongly Better
Example:
Arrays
Zero Built
based, type bound on .NET System.Array class with type and shape, but no bounds
Declared
Created
Namespaces
Every
Can
and should be nested classes and types by semantics with keyword namespace with using
Group
Declared
Referenced
Interfaces
Declarations
Define
structure and semantics for specific purposes definitions of methods and properties (multiple) inheritance
interface IPersonAge { int YearOfBirth {get; set;} int GetAgeToday(); }
Abstract Support
Example:
Classes
Implementation
public class Person : IPersonAge { private int YOB; public Person() { } public int YearOfBirth { get { return YOB; }; set { YOB = value; }; } public int GetAgeToday() { return Today()YearOfBirth }; }
Implement Inherit
interfaces
Classes
Structs
Groups
No inheritance allowed Always passed by value Struct Lightweight data container, value type Class Rich object with references, reference type
C++
Developers!
struct Point { double X; double Y; void MoveBy(double dX, double dY) { X+=dX; Y+=dY; } }
Example:
Properties
Mix Use
Implementation of read-only members (by omitting set) Validation on assignment Calculated or composed values
Example:
Indexers
Consistent Build Allow Index
on properties idea indexed access to contained objects qualifier may be of any type
object this[string index] { get { return Dict.Item(index); } set { Dict.Add(index,value); } }
Example:
Delegates
Similar
to function pointers found in C and C++ typed, no type-cast confusion or errors creates typed method signature:
Strongly
Declaration
Actual
Argument
Events
Language-intrinsic All
event model
management done by C# are declared using a delegate type creates event source to bind to
Events
Declaration
Event
Add handler:
Remove handler:
btnAction.OnClicked -= MyClickHandler;
Event
OnClicked(this,PointerLocation);
Attributes
Similar
Declarative semantics
[PersonFirstName] String Vorname; [PersonFirstName] String PrimarioNome;
Automatically Explore
O.GetType().GetCustomAttributes();
Example:
Statements
Very C: Flow Control and Loops
if (<bool expr>) { ... } else { ... }; switch(<var>) { case <const>: ...; }; while (<bool expr>) { ... }; for (<init>;<bool test>;<modify>) { ... }; do { ... } while (<bool expr>);
Very not C:
lock(<object>){ ... };
Can
Operators
Very
C:
* / + - % << >>
== != < > >= <=
Logical/conditional: && || ^
Arithmetic:
Relational:
Not
exactly C:
Very
un-C:
Tests run-time type
Converts a value to a specified type
is
as
Operator Overloading
Most
No
overloading for
Assignment operators Special operators (sizeof, new, is, typeof)
{ t.total += Amount; }
Access Protection
Adopts
C++ model
public Everyone may call or access protected Only members may access
Expands
C++ model
supports
Intrinsic string type Rich garbage-collection model By-reference parameters using ref
void increment(ref int value, int by)
Eliminates Pointers
types can be "boxed" and "unboxed" allows by-value types to travel by-ref
42
42
Boxed: Reference
COM
Server
Solution:
The
.NET runtime tracks all memory usage automatically automatically removes all unused objects efficient memory management of use and "zero memory leaks"
GC
More Ease
Nondeterministic Destruction
Garbage
Collection downside:
Destructors called at some random future time Finalization code is never synchronous
Beware:
Instead:
Exception Handling
Very
Read
... if error occurs, catch what I can deal with ... ...finally allow me to do some manual cleanup work
Example: try { //... run code } catch(SomeException e) { //... handle } finally { //...end gracefully }
Fixes
Life:
Solution:
Must specify override to override a method Can specify new virtual to never inherit from base
intrinsic way to create doc from code "///" comments are exported
Example: ///<summary> /// This function serves to calculate the /// overall value of the item including all /// taxes /// </summary> /// <remarks> /// Tax calculates in CalcTax() /// </remarks> public decimal GetTotalValue() { }
Nmake
Visual
Visual
DebuggerGuiDebug
Tools
Free
for everyone
Visual Studio.NET
Built
on the .NET Framework SDK of the concept of Visual Studio, now with:
Reinvention
Multiple-language projects One integrated development environment for all languages and tasks Integrated tools: Visual Modeler, Database Management
Highest
Large-scale projects
Hello World
namespace Sample { using System; public class HelloWorld { public HelloWorld() { } public static int Main(string[] args) { Console.WriteLine("Hello World!"); return 0; } } }
point is "static int Main( ... )" "Hello World!" to the system console
Writes
Summary
C#
builds on the .NET Framework component model language with familiar structure
Easy to adopt for developers of C, C++, Java, and Visual Basic applications
New
Fully
Optimized
Questions?
C
Core
Relatively few and short keywords Uses symbol ASCII characters instead of words
Core
Blocks in C
void Xyz() { /*Code*/ } if (MyVal==0) { /*Code*/ }
Statements
All
"This too" Credits: Don Yang, Winner "Best Layout" International Obfuscated C Contest http://www.ioccc.org
Declaration Principles
Core
What:
Who:
user-defined identifier
Operators
Arithmetic
add (+), subtract (-), divide (/), multiply (*) Modulo (%) a=b%c; instead of a=b mod c increment (++), decrement (--) i++ instead of i=i+1
Logical
Bitwise
Relational
Expressions
Boolean
expressions
Arithmetic
1 + 2 * (6 / 2)
Assignment
Flow Control
Conditional
Loops
Counted
loops
for(<pre>;<while>;<increment>) <block>
While
loops
method int func(void) has no arguments Don't worry about them Reference and value types are clearly defined
Very