Presentation is loading. Please wait.

Presentation is loading. Please wait.

Language Fundamentals in brief C# - Introduction.

Similar presentations


Presentation on theme: "Language Fundamentals in brief C# - Introduction."— Presentation transcript:

1 Language Fundamentals in brief C# - Introduction

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 object Garbage-collection C# supports operator and method overloading

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); }

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); }

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 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 similarities

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 are declared – reference types are removed by the garbage collector Value types are copied with assignment – with reference types a reference (the address) is copied

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

8 C# - When are objects equal? Classes ought to override the Equals-method public 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... }

9 C# - Boxing and Unboxing C# converts automatically between simple value and object value => object = "boxing“ (the value is “wrapped in a box”) object => value = "unboxing“ (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!

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

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 (Abstract Data Type), but no inheritance and polymorphism

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++) {... }

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

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

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

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


Download ppt "Language Fundamentals in brief C# - Introduction."

Similar presentations


Ads by Google