platforms and tools for web services and mobile applications introduction to c# bent thomsen aalborg...

21
Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004

Upload: randolph-richards

Post on 23-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004

Platforms and tools for Web Services and Mobile Applications

Introduction to C#

Bent Thomsen

Aalborg University

3rd and 4th of June 2004

Page 2: Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004

Introduction to C#

What is C#

Java like language from Microsoft Object Orientated Language Derived from C++

Has the power of C++ and the simplicity of Visual Basic

Part of Microsoft .NET framework

If you are serious about .Net you need to learn C#

Page 3: Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004

Introduction to C#

Can only run on Windows OS

Cross Language Able to use objects created in C++.NET and

Visual Basic.NET and all the other .NET Languages

(or not)Cross Platform

What is C# (con’t)

Page 4: Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004

Introduction to C#

• What do you need?– Windows OS Machine.

• Windows 2000, XP or 2003

– Microsoft .NET SDK• www.microsoft.com

– Text Editor• Save files with a .cs extension

Getting started with C#

Page 5: Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004

Introduction to C#

• What about a development environment?– Visual C++ from Visual Studio 6.0

• Requires registry tweaking

– Visual Studio.NET– 3rd Party software

• Emacs and XEmacs• Borland

Getting started with C# (con’t)

Page 6: Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004

C# vs. The World

• Compilation Process

CPUcode

cscJIT.cs

Source Code

.exe.dll

Microsoft Intermediate Language (MSIL)

CLS CompliantLanguages

How does C# work?

Page 7: Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004

C# vs. The World

• Common Language Runtime (CLR)– Provides an execution engine for developers code

• Code management (loading and execution) • Memory management• Garbage Collection• Verification of type safety • Enforcement of code access security • Interoperation between managed code, COM objects,

and pre-existing DLLs

How does C# work? (con’t)

Page 8: Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004

C# vs. The World

• Hello world

using System;

class HelloWorld { public static void Main() { Console.WriteLine(“Hello World!"); }}

>csc HelloWorld.cs>Hello World!

The Language (con’t)

Page 9: Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004

C# vs. The World

• Properties

public class button { private string caption;

public string Caption { get { return caption; } set { if (value != null) caption = value; } }}

button b = new button();

b.Caption = “abc”string s = b.Caption;

The Language (con’t)

Page 10: Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004

C# vs. The World

• Parameter Passing

The Language (con’t)

By Value

public static void Swap(ref int x, ref int y) { int z = x; x = y; y = z;}

By Reference

Page 11: Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004

C# vs. The World

• Pointers– Not recommended for use

public struct Node { public int value; public unsafe Node* next;}

The Language (con’t)

public unsafe class A { . . .

public class A { public unsafe void B (char *p) { . . .

Page 12: Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004

C# vs. The World

The Language (con’t)

• Boxing / Unboxing– Allows value types to be converted to and from objects

automatically

ArrayList list = new ArrayList();int z = 100;

list.Add(1); list.Add(13.12); list.Add(z); //integers are automatically boxed //when they are added to the list

Page 13: Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004

C# vs. The World

The Language (con’t)• Delegates

– Basically a type-safe object orientated function pointer

delegate void simpleDelegate();

Class Test { static void F() { System.Console.Writeline(“Test.F”); } static void main (){ simpleDelegate d = new simpleDelegate(F); d(); }}

Page 14: Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004

C# vs. The World

The Language (con’t)• Delegates (con’t)

void multiCall (simpleDelegate d, int count) { for (int i=0; i<count; i++){ d(); }

Page 15: Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004

C# vs. The World

The Language (con’t)• Versioning

Class B

y();

Class A

x();

v.1Class A

x();y();

v.2

Class B

y();

Which one to use?

Page 16: Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004

C# vs. The World

• Versioning– C# requires developers to clearly state their intent

• Use of the keyword ‘new’ and ‘override’

Class Derived B { new public void x () { . . .

Class Derived B { public override void x () { . . .

The Language (con’t)

Page 17: Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004

C# vs. The World

• Libraries

The Language (con’t)

Few core libraries Uses the libraries from the .NET framework

Threading, Collection, XML, ADO+, ASP+, GDI+ & WinForms libraries

Page 18: Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004

C# vs. The World

Comparison of C# syntax with Java and C++• Similarities

Single rooted class hierarchy Similar keywords (derived from C++) Virtual Machine & IL/CLR Garbage Collection No global methods Interface, no Multiple inheritance Exception handling

Page 19: Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004

C# vs. The World

Comparison of C# syntax with Java and C++• Differences

Syntax and Keywords not from C++ Properties, Delegates, Boxing, Pointers,

Libraries, etc. Preprocessor Directives

Page 20: Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004

C# vs. The World

• Easy to learn, similar to both C++ and JAVA• Can use existing pool of C++ and Visual Basic

programmers.• Can use components from .NET• Syntactical Sugar

C# Advantages

Page 21: Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004

C# vs. The World

• Only runs on Windows machines• Limited choice in development environment• Locked into Microsoft compatibility• Not extensively tested• Not enough maturity time

C# Disadvantages