17
FEN 2012 UCN Technology: Computer Science 1 C# - Introduction Language Fundamentals in Brief

C# - Introduction

Embed Size (px)

DESCRIPTION

C# - Introduction. Language Fundamentals in Brief. As in Java. C#. … but note the capital ‘ M ’. All program logic must be embedded in (typically) a class. Every executable program must contain a Main -method. The Main-method is the starting point of the application. C# is case-sensitive - PowerPoint PPT Presentation

Citation preview

Page 1: C# - Introduction

FEN 2012 UCN Technology: Computer Science 1

C# - Introduction

Language Fundamentals in Brief

Page 2: C# - Introduction

FEN 2012 UCN Technology: Computer Science

2

C#

• All program logic must be embedded in (typically) a class.

• Every executable program must contain a Main-method. The Main-method is the starting point of the application.

• C# is case-sensitive• No multiple inheritance (only between

interfaces) • All classes inherit from Object • Garbage-collection• C# supports operator and method overloading

As in Java

…but note the capital

‘M’

Page 3: C# - Introduction

FEN 2012 UCN Technology: Computer Science

3

A class

public class Book{

private string title;private string author;public Book(string t, string a) //Constructor{ title= t; author= a;}public override string ToString(){ return (title+" "+author);}

}

Page 4: C# - Introduction

FEN 2012 UCN Technology: Computer Science

4

Driver Program (Main)

public class BookMain{ public static void Main() {

Book b1= new Book("C#","Troelsen");Book b2= new Book("Java","Kölling");System.Console.WriteLine(b1.ToString());System.Console.WriteLine(b2);

}}

Page 5: C# - Introduction

FEN 2012 UCN Technology: Computer Science

5

C#- Namespaces and Using

• Namespaces is a tool for structuring programs and systems

• Makes it possible to use the same names (identifiers) in different parts of an application.

• Namespaces may be nested• Visual Studio creates default a namespace with the

same name as the project• using <namespace name> tells the compiler where to

look for definitions that our program refers to• Namespaces are not the same as Java-packages,

but they are used for the same things and there are many similarities

Page 6: C# - Introduction

FEN 2012 UCN Technology: Computer Science

6

C#- value- and reference-types

• Objects of value-type are stack allocated – objects of reference type are allocated on the heap

• Value types die, when control goes out of the scope where they were declared – reference types are removed by the garbage collector (non-deterministic)

• Value types are copied with assignment – with reference types a reference (the address) to the object is copied

Page 7: C# - Introduction

FEN 2012 UCN Technology: Computer Science

7

C#- reference types - example

• creation, assignment and comparison:

Customer c1, c2, c3;string s1, s2;

c1 = new Customer("Flemming Sander", 36259);c2 = new Customer(”Bjarne Riis", 55298);c3 = null; // c3 refers to nothing

c3 = c1; // c3 refers to the same object as c1

if (c1 == null) ... // is c1 referring to something? if (c1 == c2) ... // compare references if (c1.Equals(c2)) ... // compares object-values

Page 8: C# - Introduction

FEN 2012 UCN Technology: Computer Science

8

C#- When are objects equal?

• Classes ought to override the Equals-methodpublic class Customer{ . . .

public override bool Equals(object obj) { Customer other; if ((obj == null) || (!(obj is Customer))) return false; // surely not equal

other = (Customer) obj; // explicit typecast return this.id == other.id; // equal if ids are... }}

Page 9: C# - Introduction

FEN 2012 UCN Technology: Computer Science

9

C#- Boxing and Unboxing

• C# converts automatically between simple values and objects– value => object = "boxing“ (the value is “wrapped in a box”)– object => value = "un boxing“ (the value is unwrapped again)

int i, j;object obj;string s;

i = 32;obj = i; // boxing (copy)i = 19;j = (int) obj; // unboxing!

s = j.ToString(); // boxing!s = 99.ToString(); // boxing!

Page 10: C# - Introduction

FEN 2012 UCN Technology: Computer Science

10

C#- arrays

• Arrays are reference types– Created from the Array-class in FCL– Created using the new-operator– 0-based indexing– Are initialised with default value (0 if numeric, null if

reference)int[] a;a = new int[5];

a[0] = 17;a[1] = 32;int x = a[0] + a[1] + a[4];

int l = a.Length;

Access element 1

Creation

Number of elements

Page 11: C# - Introduction

FEN 2012 UCN Technology: Computer Science

11

C#- structs

• In some ways like a class, but there are differences:– Can have instance variables and methods– Cannot have a default constructor– Variables of a struct-type are value types and as

such stack allocated– Can only inherit from interfaces– Cannot be inherited from

• Can be used to implement ADTs, but no inheritance and polymorphism

Page 12: C# - Introduction

FEN 2012 UCN Technology: Computer Science

12

C#- selection and iteration

x = obj.foo();

if (x > 0 && x < 10) count++;else if (x == -1) ...else { ...}

while (x > 0){ ...

x--;} for (int k = 0; k < 10; k++)

{ ...}

Page 13: C# - Introduction

FEN 2012 UCN Technology: Computer Science

13

C#- foreach-loop

• foreach loop is used to sweep over collections as arrays– Reduces the risk of indexing errors

int[] data = { 1, 2, 3, 4, 5 };int sum = 0;

foreach (int x in data){ sum += x;}

foreach

type value collection

Page 14: C# - Introduction

FEN 2012 UCN Technology: Computer Science

14

C#- Methods

• A class may have two kind of methods:– Instance methods– Static methods (class methods)– Instance methods need an object to be

invoked– Static methods are called using the class

name only

Page 15: C# - Introduction

FEN 2012 UCN Technology: Computer Science

15

C#- Example

• The array-class in BCL (FCL)– The class is a member of namespace System (System.Array)

namespace System{ public class Array { public int GetLength(int dimension) { ... }

public static void Sort(Array a) { ... }

. . .

}}

instance method

static method

Page 16: C# - Introduction

FEN 2012 UCN Technology: Computer Science

16

C#- calling the methods

/* main.cs */

using System;

public class App{ public static void Main() { int[] data = { 11, 7, 38, 55, 3 }; Array.Sort(data);

for (int i=0; i<data.GetLength(0); i++) Console.WriteLine(i + ": " + data[i]); }}

Class-method Instance-method

Page 17: C# - Introduction

FEN 2012 UCN Technology: Computer Science

17

ExercisesUse NotePad and the command prompt compiler (csc.exe) to dothe following: (see HelloWorld – demo)1. Create a class including a Main method that instantiates an

array of int (previous slide).2. Write methods (static) that do the following:

1. A method that returns the sum the elements in the array2. A method that returns the average of the elements in the array3. A method that returns the number of elements with the value 7 in

the array4. A method that returns true if the value 3 is contained in the array

and false otherwise5. Generalise your solution to exercise 2 and 3, so the value in

question (7 and 3 resp.) is passed as an argument to the method.

3. For each method write driver code in the Main method that tests the method.