FEN 2012 UCN Technology: Computer Science1 C# - Introduction Language Fundamentals in Brief.

Slides:



Advertisements
Similar presentations
Language Fundamentals in brief C# - Introduction.
Advertisements

CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Interfaces Reference: Joe Hummel. 2 UCN Technology: Computer Science 2012 Objectives “Good class design starts with good application design — how many.
C#: Data Types Based on slides by Joe Hummel. 2 UCN Technology: Computer Science Content: “.NET is designed around the CTS, or Common Type System.
Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared.
For use of IST410 Students only Arrays-1 Arrays. For use of IST410 Students only Arrays-2 Objectives l Declaring arrays l Instantiating arrays l Using.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#
3. Data Types. 2 Microsoft Objectives “.NET is designed around the CTS, or Common Type System. The CTS is what allows assemblies, written in different.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Lecture 9 Concepts of Programming Languages
4. Statements and Methods. 2 Microsoft Objectives “With regards to programming statements and methods, C# offers what you would come to expect from a.
Abstract Data Types and Encapsulation Concepts
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
C#: Statements and Methods Based on slides by Joe Hummel.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
Spring 2007NOEA: Computer Science Programme 1 C# - Introduction Language Fundamentals: Data Types string Objects and Classes Methods Iteration and Selection.
C# Intro Programming languages and programs: Source code and object code Editors and compilers C# fundamentals: Program structure Classes and Objects Variables.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
C# EMILEE KING. HISTORY OF C# In the late 1990’s Microsoft recognized the need to be able to develop applications that can run on multiple operating system.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Section 3 - Arrays and Methods. Arrays Array: collection of group of data variables of same type, sharing the same name for convenience - Easy to search.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
ISBN Object-Oriented Programming Chapter Chapter
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
Object Oriented Software Development 4. C# data types, objects and references.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Session 1 C# Basics.
Introduction to C# By: Abir Ghattas Michel Barakat.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
OOP in C# - part 1 OOP in C#: –Object Interaction. –Inheritance and Polymorphism (next module). FEN 20121UCN Technology: Computer Science.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
Chapter 9 Introduction to Arrays Fundamentals of Java.
Memory Management in Java Mr. Gerb Computer Science 4.
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
Sections 10.1 – 10.4 Introduction to Arrays
Modern Programming Tools And Techniques-I
Classes and Objects.
CS360 Windows Programming
CS 302 Week 11 Jim Williams, PhD.
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Lecture 9 Concepts of Programming Languages
Constructor Overloading
Conditional Statements
CIS 199 Final Review.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Classes and Objects Object Creation
Lecture 9 Concepts of Programming Languages
Presentation transcript:

FEN 2012 UCN Technology: Computer Science1 C# - Introduction Language Fundamentals in Brief

FEN 2012UCN 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’

FEN 2012UCN 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); }

FEN 2012UCN 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); }

FEN 2012UCN 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 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

FEN 2012UCN 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

FEN 2012UCN 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

FEN 2012UCN Technology: Computer Science 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... }

FEN 2012UCN 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!

FEN 2012UCN 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

FEN 2012UCN 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

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

FEN 2012UCN 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

FEN 2012UCN 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

FEN 2012UCN 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

FEN 2012UCN 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

FEN 2012UCN Technology: Computer Science 17 Exercises Use NotePad and the command prompt compiler (csc.exe) to do the 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 array 2.A method that returns the average of the elements in the array 3.A method that returns the number of elements with the value 7 in the array 4.A method that returns true if the value 3 is contained in the array and false otherwise 5.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.