Professional Documents
Culture Documents
Introduction To C Sharp
Introduction To C Sharp
Raimonds Rudmanis
Senior Consultant
Microsoft Baltic
Session Prerequisites
Agenda
Hello World
The .NET Framework
Design Goals of C#
Language Features
Hello World
Agenda
Hello World
The .NET Framework
Design Goals of C#
Language Features
C++
C#
JScript
Windows
forms
Visual Studio.NET
C++
C#
JScript
Windows
Forms
Visual Studio.NET
Language Interoperability
Common Classes for all Languages
Common Types for all Languages
Runtime Controls Compilation to Machine
Code
Assemblies
Application Domains
Simplified development
XCOPY deployment
Scalability
Rich Web clients and safe Web hosting
Potentially multi-platform
Multiple languages (cross inheritance)
Increases productivity
Robust and secure execution environment
VB
C#
C++
Compiler
Compiler
Compiler
Assembly
IL Code
Assembly
IL Code
Assembly
IL Code
Unmanaged
Component
C++
C#
JScript
Windows
Forms
Visual Studio.NET
ASP.NET
Windows Forms
Agenda
Hello World
The .NET Framework
Design Goals of C#
Language Features
Design Goals of C#
The Big Ideas
Design Goals of C#
Design Goals of C#
Traditional views
Design Goals of C#
Garbage collection
Exceptions
Type-safety
Versioning
Design Goals of C#
C++ Heritage
Design Goals of C#
Interoperability
.NET Languages
XML/SOAP
VB.NET
MC++
JScript
...
C#
COM
OLE Automation
P/Invoke and unsafe code
Agenda
Hello World
The .NET Framework
Design Goals of C#
Language Features
Language Features
Program Structure
Namespaces
Type declarations
Members
Organization
Language Features
Program Structure
using System;
namespace System.Collections
{
public class Stack
{
Entry top;
public void Push(object data) {
top = new Entry(top, data);
}
public object Pop() {
if (top == null) throw new InvalidOperationException();
object result = top.data;
top = top.next;
return result;
}
}
}
Language Features
Type System
Value types
Reference types
123
"Hello world"
Language Features
Type System
Value types
Primitives
Enums
Structs
int i;
enum State { Off, On }
struct Point { int x, y; }
Reference types
Classes
Interfaces
Arrays
Delegates
Language Features
Predefined Types
C# predefined types
Reference
Signed
Unsigned
Character
Floating-point
Logical
object, string
sbyte, short, int, long
byte, ushort, uint, ulong
char
float, double, decimal
bool
Language Features
Classes
Single inheritance
Multiple interface implementation
Class members
Member access
Language Features
Structs
Benefits
Language Features
Classes and Structs
10
20
cp
CPoint
10
20
Language Features
Interfaces
Multiple inheritance
Can contain methods, properties,
indexers and events
Private interface implementations
interface IDataBound
{
void Bind(IDataBinder binder);
}
class EditBox: Control, IDataBound
{
void IDataBound.Bind(IDataBinder binder) {...}
}
Language Features
Enums
Strongly typed
Language Features
Delegates
Language Features
Unified Type System
Everything is an object
MemoryStream
Hashtable
FileStream
int
double
Language Features
Unified Type System
Boxing
Unboxing
123
System.Int32
o
j
123
123
Language Features
Unified Type System
Benefits
Language Features
Component Development
Language Features
Properties
Language Features
Indexers
Can be overloaded
Language Features
Language Features
Handling an Event
Language Features
Language Features
Attributes
Traditional solutions
C# solution: Attributes
Language Features
Attributes
Language Features
Attributes
Attributes can be
Completely extensible
Type-safe
user-to-machine
XML/SOAP = machine-to-machine
Leveraging the Web
Same
infrastructure
Same programming model
Anyone can play
Truly
Stateless
UDDI
Discovery
Web
Service
Consumer
http://myservice.com
HTML or XML with link to WSDL
Web
Service
Web
Programs
Data
XML
Objects
Schema
XSD
Classes
Services
WSDL
Methods
Invocation
SOAP
Calls
</SubmitOrder>
OrderProcessor.SubmitOrder(po);
</soap:Body>
</soap:Envelope>
Language Features
DEMO 3: Attributes
Language Features
XML Comments
class XmlElement
{
/// <summary>
///
Returns the attribute with the given name and
///
namespace</summary>
/// <param name="name">
///
The name of the attribute</param>
/// <param name="ns">
///
The namespace of the attribute, or null if
///
the attribute has no namespace</param>
/// <return>
///
The attribute value, or null if the attribute
///
does not exist</return>
/// <seealso cref="GetAttr(string)"/>
///
public string GetAttr(string name, string ns) {
...
}
}
Language Features
Language Features
goto default
in_label;
No fall-through, goto case or goto
switch( argwhile
)
( i<100 )
Goto cant jumpstatic
into {blocks
short x = {32767; // Max short
static short
= in_label:
32767;
casey0:
Foreach statement
int i;
case 1:
i++;
Checked and try
Console.WriteLine(Low);
}
i )goto
//
case
break;
2:error
case 2;
unchecked foreachif{((string
word
in (myArray.words
ifz(=case
i>0
)2: while
checked
((short)(x
Console.WriteLine(Med);
i<100+ )y)); )
statements {
}
break;
Console.WriteLine(Med);
{
Console.WriteLine({0},
void
Foo()
{ ( j>50e)) word)
Expression
FileClass
file;
catch
(OverflowException
default:
break;
if
}
i == 1;
// error
{ default:
Console.WriteLine(High);
goto
out_label;
statements
i =+ OpenFile()
j;
// )
error
ifConsole.WriteLine(e.ToString());
// error
}( file
Console.WriteLine(High);
}
must do work
if}}( (file = out_label:
OpenFile()) != NULL )
Language Features
For Each Statement
Iteration of arrays
Language Features
Parameter Arrays
Language Features
Operator Overloading
Language Features
Operator Overloading
x
y
z
=
=
=
123;
DBInt.Null;
x + y;
Language Features
Versioning
Language Features
Versioning
class
Base
class Base
{
{
} public virtual void Foo() {
}
//
version2 1
// version
Console.WriteLine("Base.Foo");
1
class Derived: Base
// version 2b
2a
{
public
virtual
void
Foo()
{{ {
new public
override
virtual
void
void
Foo()
Foo()
Console.WriteLine("Derived.Foo");
base.Foo();
} Console.WriteLine("Derived.Foo");
} }
}
Language Features
Conditional Compilation
#define, #undef
#if, #elif, #else, #endif
Conditional methods
Language Features
Unsafe Code
Declarative pinning
Basically inline C
unsafe void Foo() {
char* buf = stackalloc char[256];
for (char* p = buf; p < buf + 256; p++) *p = 0;
...
}
Language Features
Unsafe Code
[dllimport("kernel32", SetLastError=true)]
static extern unsafe bool ReadFile(int hFile,
void* lpBuffer, int nBytesToRead,
int* nBytesRead, Overlapped* lpOverlapped);
Language Features
COM Support
Language Features
COM Support
System.Runtime.Interopservices
Language Features
DEMO 5: COM and C#
Language Features
Language Specification
Common Language Infrastructure (CLI)
C# Books
C# Customers
More Resources
http://msdn.microsoft.com/
C# language specification
C# newsgroups
microsoft.public.dotnet.languages.csharp
Questions?