Language Fundamentals in brief C# - Introduction.
-
Upload
taliyah-coster -
Category
Documents
-
view
261 -
download
0
Transcript of Language Fundamentals in brief C# - Introduction.
Language Fundamentals in brief
C# - Introduction
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-sensitiveNo multiple inheritance (only between interfaces) All classes inherit object Garbage-collectionC# supports operator and method overloading
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);}
}
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);
}}
C#- Namespaces and Using
Namespaces is a tool for structuring programs and systemsMakes it possible to use the same names (identifiers) in different parts of an application.Namespaces may be nestedVisual Studio creates default a namespace with the same name as the projectusing <namespace name> tells the compiler where to look for definitions that our program refers toNamespaces are not the same as Java-packages, but they are used for the same things and there are similarities
C#- value- and reference-types
Objects of value-type are stack allocated – objects of reference type are allocated on the heapValue types die, when control goes out of the scope, where they are declared – reference types are removed by the garbage collectorValue types are copied with assignment – with reference types a reference (the address) is copied
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
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... }}
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!
C#- arrays
Arrays are reference typesCreated from the Array-class in FCLCreated using the new-operator0-based indexingAre 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
C#- structs
In some ways like a class, but there are differences:
Can have instance variables and methodsCannot have a default constructorVariables of a struct-type are value types and as such stack allocatedCan only inherit from interfacesCannot be inherited from
Can be used to implement ADTs (Abstract Data Type), but no inheritance and polymorphism
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++)
{ ...}
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
C#- Methods
A class may have two kind of methods:Instance methodsStatic methods (class methods)Instance methods need an object to be invokedStatic methods are called using the class name only
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
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