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
JIT Compiler
Native Code
Visual Studio.NET
ASP.NET: Web Services Windows
and Web Forms Forms
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
Like classes, except
Stored in-line, not heap allocated
Assignment copies data, not reference
No inheritance
Ideal for light weight objects
Complex, point, rectangle, color
int, float, double, etc., are all structs
Benefits
No heap allocation, less GC pressure
More efficient use of memory
Language Features
Classes and Structs
struct SPoint { int x, y; ... }
class CPoint { int x, y; ... }
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);
}
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
[dllimport("kernel32", SetLastError=true)]
static extern unsafe bool ReadFile(int hFile,
void* lpBuffer, int nBytesToRead,
int* nBytesRead, Overlapped* lpOverlapped);
}
Language Features
COM Support
.Net framework provides great
COM support
TLBIMP imports existing COM classes
TLBEXP exports .NET types
Most users will have a
seamless experience
Language Features
COM Support
Sometimes you need more control
Methods with complicated structures
as arguments
Large TLB only using a few classes
System.Runtime.Interopservices
COM object identification
Parameter and return value marshalling
HRESULT behavior
Language Features
DEMO 5: COM and C#
Call a COM component from C#
Language Features
DEMO 6: Visual Studio .NET
Windows programming with C#
C# And CLI Standardization
Work begun in September 2000
Submitted to ECMA (www.ecma.ch)
Active involvement by Intel, HP, IBM,
Fujitsu, Plum Hall,
Since December 2001
C#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?
2001 Microsoft Corporation. All rights reserved.