170 likes | 280 Views
This brief overview of C# covers essential language fundamentals, including program structure, the significance of classes and the Main method, and core concepts such as namespaces, value types versus reference types, and object equality. The introduction also touches on critical features like garbage collection, operator overloading, and the use of arrays. With examples demonstrating object creation, method usage, and the differences between structs and classes, this guide serves as a foundational resource for understanding C# programming and its application in software development.
E N D
C# - Introduction Language Fundamentals in Brief UCN Technology: Computer Science
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 • No multiple inheritance (only between interfaces) • All classes inherit from Object • Garbage-collection • C# supports operator and method overloading UCN Technology: Computer Science
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); } } UCN Technology: Computer Science
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); } } UCN Technology: Computer Science
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 UCN Technology: Computer Science
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 UCN Technology: Computer Science
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 UCN Technology: Computer Science
C#- When are objects equal? 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... } } • Classes ought to override the Equals-method UCN Technology: Computer Science
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! UCN Technology: Computer Science
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; Creation Access element 1 Number of elements UCN Technology: Computer Science
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 UCN Technology: Computer Science
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++) { ... } UCN Technology: Computer Science
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 xin data) { sum += x; } foreach type value collection UCN Technology: Computer Science
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 UCN Technology: Computer Science
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 UCN Technology: Computer Science
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 UCN Technology: Computer Science
Exercises Use NotePad and the command prompt compiler (csc.exe) to do the following: (see HelloWorld – demo) • Create a class including a Main method that instantiates an array of int (previous slide). • Write methods (static) that do the following: • A method that returns the sum the elements in the array • A method that returns the average of the elements in the array • A method that returns the number of elements with the value 7 in the array • A method that returns true if the value 3 is contained in the array and false otherwise • 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. • For each method write driver code in the Main method that tests the method. UCN Technology: Computer Science