Docsity

C# Programming: Types, Structs, and Classes, Slides of C programming

Partial preview of the text

Download C# Programming: Types, Structs, and Classes and more Slides C programming in PDF only on Docsity!

Introduction to C#

The New Language for

H.Mössenböck

University of Linz, Austria

[email protected]

Contents

Advanced C#

Introduction to C#^ 1. Overview2. Types3. Expressions4. Declarations5. Statements6. Classes and Structs

Inheritance

Interfaces

Delegates

10. Exceptions11. Namespaces and Assemblies12. Attributes13. Threads14. XML Comments

References: • B.Albahari, P.Drayton, B.Merrill:

C# Essentials

. O'Reilly, 2001 - S.Robinson et al:

Professional C#

, Wrox Press, 2001

  • Online documentation on the .NET SDK CD

New Features in C# Really new

(compared to Java)

•^

Reference and output parameters

-^

Objects on the stack (structs)

-^

Rectangular arrays

-^

Enumerations

-^

Unified type system

-^

goto

-^

Versioning

"Syntactic Sugar"•^

Component-based programming-^

Properties

-^

Events

-^

Delegates

-^

Indexers

-^

Operator overloading

-^

foreach statements

-^

Boxing/unboxing

-^

Attributes

-^

Hello World^ File Hello.cs

using System; class Hello {

static void Main() {

Console.WriteLine("Hello World"); } }

•^

uses the namespace

System

•^

entry point must be called

Main

•^

output goes to the console

-^

file name and class name need

not

be identical

Compilation (in the Console window)

csc Hel

lo.cs

Execution

Hel

lo

A Program Consisting of 2 Files Counter.cs

Compilation

csc Counter.cs Prog.cs=> generates Prog.exe

Execution

Prog

Working with DLLs

csc /target:library Counter.cs=> generates Counter.dllcsc /reference:Counter.dll Prog.cs=> generates Prog.exe

class

Counter

int val = 0; public void

Add

(int x) { val = val + x; }

public int

Val

() { return val; }

} Prog.cs using System; class

Prog

static void

Main

Counter c = new Counter(); c.Add(3); c.Add(5); Console.WriteLine("val = " + c.Val()); } }

Types

Value Types versus Reference Types

Value Types

Reference Types

variable contains

value

reference

stored on

stack

heap

initialisation

0, false, '\0'

null

assignment

copies the value

copies the reference

example

int i = 17;

string s = "Hello";

int j = i;

string s1 = s;

i^

17

s

H e l l o

j^

17

s

Simple Types

Long Form

in Java

Range

sbyte

System.SByte

byte

byte

System.Byte

short

System.Int

short

ushort

System.UInt

int

System.Int

int

uint

System.UInt

long

System.Int

long

63

63

ulong

System.UInt

64

float

System.Single

float

1.5E- ..

3.4E38 (32 Bit)

double

System.Double

double

5E- ..

1.7E308 (64 Bit)

decimal

System.Decimal

1E- ..

7.9E28 (128 Bit)

bool

System.Boolean

boolean

true, false

char

System.Char

char

Unicode

character

Enumerations List of named constantsDeclaration (directly in a namespace)

enum Color {red, blue, green}

// values: 0, 1, 2

enum Access {personal=1, group=2, all=4}enum Access1 : byte {personal=1, group=2, all=4}

Use

Color c = Color.blue;

// enumeration constants must be qualified

Access a = Access.personal | Access.group;if ((Access.personal & a) != 0) Console.WriteLine("access granted");

Operations on Enumerations

Compare

if (c == Color.red) ...if (c > Color.red && c <= Color.green) ...

c = c + 2;

c++;

if ((c & Color.red) == 0) ...

|^

c = c | Color.blue;

~^

c = ~ Color.red;

The compiler does not check if the result is a valid enumeration value.

Note

  • Enumerations cannot be assigned to

int

(except after a type cast).

  • Enumeration types inherit from

object

Equals

,^ ToString

  • Class

System.Enum

provides operations on enumerations

( GetName

,^ Format

,^ GetValues

, ...).

Multidimensional Arrays Jagged (like in Java)

a[0][1]

a[0]a[1]

a

int[][] a = new int[2][]; a[0] = new int[3]; a[1] = new int[4]; int x = a[0][1]; int len = a.Length; // 2 len = a[0].Length;

Rectangular (more compact, more efficient access)

a[0, 1]

int[,] a = new int[2, 3]; int x = a[0, 1]; int len = a.Length;

len = a.GetLength(0); // 2 len = a.GetLength(1); // 3

a

Class System.String Can be used as standard type

string

string s = "Alfonso";

Note •^

Strings are immutable (use

StringBuilder

if you want to modify strings)

-^

Can be concatenated with +:

"Don " + s

-^

Can be indexed:

s[i]

-^

String length:

s.Length

-^

Strings are reference types => reference semantics in assignments

-^

but their values can be compared with

==

and

!=

:^

if (s == "Alfonso") ...

-^

Class

String

defines many useful operations:

CompareTo

,^ IndexOf

,^ StartsWith

,^ Substring

, ...

Classes Declaration

class Rectangle {

Point origin;public int width, height;public Rectangle() { origin = new Point(0,0); width = height = 0; }public Rectangle (Point p, int w, int h) { origin = p; width = w; height = h; }public void MoveTo (Point p) { origin = p; } }

Use

Rectangle r = new Rectangle(new Point(10, 20), 5, 5);int area = r.width * r.height;r.MoveTo(new Point(3, 3));

Differences Between Classes and Structs Classes Reference Types (objects stored on the heap) support inheritance (all classes are derived from

object

can implement interfaces may have a destructor

Structs Value Types (objects stored on the stack) no inheritance (but compatible with

object

can implement interfaces no destructors allowed