Introduction To C Sharp
Introduction To C Sharp
Raimonds Rudmanis
Senior Consultant
Microsoft Baltic
Session Prerequisites
This session assumes that you
understand the fundamentals of
Object oriented programming
This is a Level 200 Session
What Will Be Covered Today
Brief introduction to the
.NET framework
C# language overview
Agenda
Hello World
The .NET Framework
Design Goals of C#
Language Features
Hello World
DEMO 1: Hello World
using System;
class Hello
{
static void Main() {
Console.WriteLine("Hello world");
}
}
Agenda
Hello World
The .NET Framework
Design Goals of C#
Language Features
The .NET Framework
Overview
VB C++ C# JScript …
Visual Studio.NET
ASP.NET: Web Services Windows
And Web Forms forms
Visual Studio.NET
ASP.NET: Web Services Windows
and Web Forms Forms
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
.NET Framework and CLR
CLR Execution Model
Source VB C# C++
code
Unmanaged
Compiler Compiler Compiler
Component
JIT Compiler
Native Code
Visual Studio.NET
ASP.NET: Web Services Windows
and Web Forms Forms
ASP.NET
Separation of code and presentation
Compiled
Web Forms
Web Services
Windows® Forms
Framework for building rich clients
ADO.NET, Evolution of ADO
New objects (e.g., DataSets)
XML support throughout
Agenda
Hello World
The .NET Framework
Design Goals of C#
Language Features
Design Goals of C#
The Big Ideas
Traditional views
C++, Java™: Primitive types are “magic”
and do not interoperate with objects
Smalltalk, Lisp: Primitive types are
objects, but at great performance cost
C# unifies with no performance cost
Deep simplicity throughout system
Improved extensibility and reusability
New primitive types: Decimal, SQL…
Collections, etc., work for all types
Design Goals of C#
Robust and Durable Software
Garbage collection
No memory leaks and stray pointers
Exceptions
Error handling is not an afterthought
Type-safety
No uninitialized variables, unsafe casts
Versioning
Pervasive versioning considerations in all
aspects of language design
Design Goals of C#
Preserving Your Investment
C++ Heritage
Namespaces, enums, pointers (in unsafe
code), unsigned types, etc.
No unnecessary sacrifices
Real-world useful constructs
foreach, using, switch on string
decimal type for financial applications
ref and out parameters
Millions of lines of C# code in .NET
Short learning curve
Increased productivity
Design Goals of C#
Interoperability
Value types
Directly contain data
Cannot be null
Reference types
Contain references to objects
May be null
int i = 123;
string s = "Hello world";
i 123
s "Hello world"
Language Features
Type System
Value types
Primitives int i;
Enums enum State { Off, On }
Structs struct Point { int x, y; }
Reference types
Classes class Foo: Bar, IFoo {...}
Interfaces interface IFoo: IBar {...}
Arrays string[] a = new string[10];
Delegates delegate void Empty();
Language Features
Predefined Types
C# predefined types
Reference object, string
Signed sbyte, short, int, long
Unsigned byte, ushort, uint, ulong
Character char
Floating-point float, double, decimal
Logical bool
Predefined types are simply aliases for
system-provided types
For example, int = System.Int32
Language Features
Classes
Single inheritance
Multiple interface implementation
Class members
Constants, fields, methods,
properties, indexers, events,
operators, constructors, destructors
Static and instance members
Nested types
Member access
Public, protected, internal, private
Language Features
Structs
10
sp
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);
}
Everything is an object
All types ultimately inherit from object
Any piece of data can be stored,
transported, and manipulated with no
extra work
object
MemoryStream FileStream
Language Features
Unified Type System
Boxing
Allocates box, copies value into it
Unboxing
Checks type of box, copies value out
int i = 123;
object o = i;
int j = (int)o;
i 123
o System.Int32
123
j 123
Language Features
Unified Type System
Benefits
Eliminates “wrapper classes”
Collection classes work with all types
Replaces OLE Automation's Variant
Lots of examples in .NET framework
string s = string.Format(
"Your total was {0} on {1}", total, date);
public MyForm() {
okButton = new Button(...);
okButton.Caption = "OK";
okButton.Click += new EventHandler(OkButtonClick);
}
[XmlRoot("Order", Namespace="urn:acme.b2b-schema.v1")]
public class PurchaseOrder
{
[XmlElement("shipTo")] public Address ShipTo;
[XmlElement("billTo")] public Address BillTo;
[XmlElement("comment")] public string Comment;
[XmlElement("items")] public Item[] Items;
[XmlAttribute("date")] public DateTime OrderDate;
}
Discovery
http://myservice.com
Web
Service HTML or XML with link to WSDL
Consumer
How do we talk? (WSDL)
http://myservice.com?wsdl
Web
XML with service descriptions Service
Iteration of arrays
public static void Main(string[] args) {
foreach (string s in args) Console.WriteLine(s);
}
#define, #undef
#if, #elif, #else, #endif
Simple boolean logic
Conditional methods
public class Debug
{
[Conditional("Debug")]
public static void Assert(bool cond, String s) {
if (!cond) {
throw new AssertionException(s);
}
}
}
Language Features
Unsafe Code
COM integration, P/invoke cover most cases
Unsafe code
Low-level code without leaving the box
Enables unsafe casts, pointer arithmetic
Declarative pinning
Fixed statement
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
class FileStream: Stream
{
int handle;
[dllimport("kernel32", SetLastError=true)]
static extern unsafe bool ReadFile(int hFile,
void* lpBuffer, int nBytesToRead,
int* nBytesRead, Overlapped* lpOverlapped);
}
Language Features
COM Support