Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 20 Microsoft’s Approach 3 – C# Rob Pooley

Slides:



Advertisements
Similar presentations
Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
Advertisements

Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
BY BOWYA G.  This International Standard is based on a submission from Hewlett-Packard, Intel, and Microsoft, that describes a language called C#. 
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Programming Based on Events
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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++
Advanced Object-Oriented Programming Features
Writing Object Oriented Software with C#. C# and OOP C# is designed for the.NET Framework  The.NET Framework is Object Oriented In C#  Your access to.
Lecture 9 Concepts of Programming Languages
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
ASP.NET Programming with C# and SQL Server First Edition
Abstract Data Types and Encapsulation Concepts
Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
A First Program Using C#
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Lecture 5 What is object-oriented programming OOP techniques How Windows Forms applications rely on OOP.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
ILM Proprietary and Confidential -
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.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Object-Oriented Programming Chapter Chapter
Introduction to Object-Oriented Programming Lesson 2.
Object Oriented Software Development 4. C# data types, objects and references.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Introduction to C# By: Abir Ghattas Michel Barakat.
Module 13: Properties and Indexers. Overview Using Properties Using Indexers.
Introduction to Web Application
Arrays & Enum & Events. Arrays Arrays are data structures consisting of related data items of the same type. Arrays are fixed-length entities—they remain.
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 –
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
INTRODUCTION BEGINNING C#. C# AND THE.NET RUNTIME AND LIBRARIES The C# compiler compiles and convert C# programs. NET Common Language Runtime (CLR) executes.
C# for C++ Programmers 1.
Classes (Part 1) Lecture 3
Abstract Data Types and Encapsulation Concepts
2.7 Inheritance Types of inheritance
Module 5: Common Type System
Structs.
Yunus Özen 12- Interfaces, Structures, and Enumerations Nesne Yönelimli Programlama - i Yunus Özen
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Abstract Data Types and Encapsulation Concepts
Lecture 22 Inheritance Richard Gesick.
Classes and Objects.
How to organize and document your classes
Chapter 5 Classes.
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 20 Microsoft’s Approach 3 – C# Rob Pooley

Programming Handheld and Mobile devices 2 C# and.NET Microsoft has its own approach to networked applications This is called the.NET approach It is intended to include programming mobile devices As part of their strategy, Microsoft have created a competitor for Java It is called C#

Programming Handheld and Mobile devices 3 Example of C# // created on 30/06/2003 at 10:06 // namespace Declaration using System; //Program start class class WelcomeCSS { //Main begins program execution public static void Main() { //Write to console Console.WriteLine("Welcome to the C# Station Tutorial!"); Console.ReadLine(); }

Programming Handheld and Mobile devices 4 What’s the difference C# is superficially similar to both Java and C++ Syntactically it is closer to C++ It is like Java in insisting on all methods being inside classes Programs start with Main Packages are known as namespaces

Programming Handheld and Mobile devices 5 Some differences include: C# uses properties to integrate getting and setting of values in objects instead of separate getter/setter methods. C# uses delegates as an alternative to interfaces in Java. C# has explicit support for events. C# has collections and a foreach iterator which makes a higher level approach than arrays/vectors. C# uses structs and classes to differentiate between local/stack objects and heap ones. C# allows non-virtual methods (the default). C# has parameter modifiers – out, ref and params. C# allows strings to govern switch/case statements.

Programming Handheld and Mobile devices 6 Value and reference class Class1 { public int Value = 0; } class Test { static void Main() { int val1 = 0; int val2 = val1; val2 = 123; Class1 ref1 = new Class1(); Class1 ref2 = ref1; ref2.Value = 123; Console.WriteLine("Values: {0}, {1}", val1, val2); Console.WriteLine("Refs:{0},{1}",ref1.Value,ref2.Value); } } shows this difference. The output produced is Values: 0, 123 Refs: 123, 123

Programming Handheld and Mobile devices 7 User defined types Developers can define new value types through enum and struct declarations, and can define new reference types via class, interface, and delegate declarations. public enum Color { Red, Blue, Green } public struct Point { public int x, y; } public interface IBase { void F(); } public interface IDerived: IBase { void G(); } public class A { protected virtual void H() { Console.WriteLine("A.H"); } } public class B: A, IDerived { public void F() { Console.WriteLine("B.F, implementation of IDerived.F"); } public void G() { Console.WriteLine("B.G, implementation of IDerived.G"); } override protected void H() { Console.WriteLine("B.H, override of A.H"); } } public delegate void EmptyDelegate();

Programming Handheld and Mobile devices 8 Classes class MyClass{ public MyClass() { Console.WriteLine("Instance constructor"); } public MyClass(int value) { MyField = value; Console.WriteLine("Instance constructor"); } ~MyClass() { Console.WriteLine("Destructor"); } public const int MyConst = 12; public int MyField = 34; public void MyMethod() { Console.WriteLine("MyClass.MyMethod"); } public int MyProperty { get { return MyField; } set { MyField = value; } } public int this[int index] { get { return 0; } set { Console.WriteLine("this[{0}] = {1}", index, value); } } public event EventHandler MyEvent; public static MyClass operator+(MyClass a, MyClass b) { return new MyClass(a.MyField + b.MyField); } internal class MyNestedClass {} }

Programming Handheld and Mobile devices 9 Instance constructors A member that implements the actions required to initialize an instance of a class. class Point { public double x, y; public Point() { this.x = 0; this.y = 0; } public Point(double x, double y) { this.x = x; this.y = y; } public static double Distance(Point a, Point b) { double xdiff = a.x – b.x; double ydiff = a.y – b.y; return Math.Sqrt(xdiff * xdiff + ydiff * ydiff); } public override string ToString() { return string.Format("({0}, {1})", x, y); } } class Test { static void Main() { Point a = new Point(); Point b = new Point(3, 4); double d = Point.Distance(a, b); Console.WriteLine("Distance from {0} to {1} is {2}", a, b, d); } }

Programming Handheld and Mobile devices 10 Destructors A destructor is a member that implements the actions required to destroy an instance of a class. Destructors cannot have parameters, cannot have accessibility modifiers, and cannot be called explicitly. The destructor for an instance is called automatically during garbage collection. class Point { public double x, y; public Point(double x, double y) { this.x = x; this.y = y; } ~Point() { Console.WriteLine("Destructed {0}", this); } public override string ToString() { return string.Format("({0}, {1})", x, y); } }

Programming Handheld and Mobile devices 11 Properties A property is a member that provides access to a characteristic of an object or a class. Examples of properties include the length of a string, the size of a font, the caption of a window, the name of a customer, and so on. However, unlike fields, properties do not denote storage locations. Instead, properties have accessors that specify the statements to be executed when their values are read or written. public class Button { private string caption; public string Caption { get { return caption; } set { caption = value; Repaint(); } } Button b = new Button(); b.Caption = "ABC";// set; causes repaint string s = b.Caption;// get b.Caption += "DEF";// get & set; repaint

Programming Handheld and Mobile devices 12 Events public delegate void EventHandler(object sender, System.EventArgs e); public class Button { public event EventHandler Click; public void Reset() { Click = null; } } the Button class defines a Click event of type EventHandler. Inside the Button class, the Click member is exactly like a private field of type EventHandler. However, outside the Button class, the Click member can only be used on the left hand side of the += and -= operators. The += operator adds a handler for the event, and the -= operator removes a handler for the event.

Programming Handheld and Mobile devices 13 Events 2 public class Form1 { public Form1() { // Add Button1_Click as an event handler for // Button1’s Click event Button1.Click += new EventHandler(Button1_Click); } Button Button1 = new Button(); void Button1_Click(object sender, EventArgs e) { Console.WriteLine("Button1 was clicked!"); } public void Disconnect() { Button1.Click -= new EventHandler(Button1_Click); } } shows a Form1 class that adds Button1_Click as an event handler for Button1’s Click event. In the Disconnect method, the event handler is removed. For a simple event declaration such as public event EventHandler Click; the compiler automatically provides the implementation underlying the += and -= operators

Programming Handheld and Mobile devices 14 Interfaces An interface defines a contract. A class or struct that implements an interface must adhere to its contract. Interfaces can contain methods, properties, events and indexers. The example interface IExample { string this[int index] { get; set; } event EventHandler E; void F(int value); string P { get; set; } } public delegate void EventHandler(object sender, EventArgs e); shows an interface that contains an indexer, an event E, a method F, and a property P. Interfaces may employ multiple inheritance.

Programming Handheld and Mobile devices 15 Structs The list of similarities between classes and structs is long— structs can implement interfaces, and can have the same kinds of members as classes. Structs differ from classes in several important ways, however: structs are value types rather than reference types, and inheritance is not supported for structs. Struct values are stored “on the stack” or “in-line”. Programmers can sometimes enhance performance through judicious use of structs. struct Point { public int x, y; public Point(int x, int y) { this.x = x; this.y = y; } }

Programming Handheld and Mobile devices 16 Delegates A delegate declaration defines a class that is derived from the class System.Delegate. A delegate instance encapsulates one or more methods, each of which is referred to as a callable entity. For instance methods, a callable entity consists of an instance and a method on that instance. For static methods, a callable entity consists of just a method. Given a delegate instance and an appropriate set of arguments, one can invoke all of that delegate instance’s methods with that set of arguments delegate void SimpleDelegate(); declares a delegate named SimpleDelegate that takes no arguments and returns void. class Test { static void F() { System.Console.WriteLine("Te st.F"); } static void Main() { SimpleDelegate d = new SimpleDelegate(F); d(); } } creates a SimpleDelegate instance and then immediately calls it.

Programming Handheld and Mobile devices 17 Namespaces and assemblies An application might depend on several different components, including some developed internally and some purchased from independent software vendors. Namespaces and assemblies enable this component-based approach. –Namespaces provide a logical organizational system. Namespaces are used both as an “internal” organization system for a program, and as an “external” organization system—a way of presenting program elements that are exposed to other programs. –Assemblies are used for physical packaging and deployment. An assembly may contain types, the executable code used to implement these types, and references to other assemblies. –There are two main kinds of assemblies: applications and libraries. Applications have a main entry point and usually have a file extension of.exe; Libraries do not have a main entry point, and usually have a file extension of.dll.

Programming Handheld and Mobile devices 18 To demonstrate the use of namespaces and assemblies, this section revisits the “hello, world” program presented earlier, and splits it into two pieces: a class library that provides messages and a console application that displays them. The class library will contain a single class named HelloMessage. The example // HelloLibrary.cs namespace Microsoft.CSharp.Introduction { public class HelloMessage { public string Message { get { return "hello, world"; } } } } shows the HelloMessage class in a namespace named Microsoft.CSharp.Introduction. The HelloMessage class provides a read-only property named Message. Namespaces can nest, and the declaration namespace Microsoft.CSharp.Introduction {...} is shorthand for several levels of namespace nesting: namespace Microsoft { namespace CSharp { namespace Introduction {...} } }

Programming Handheld and Mobile devices 19 The next step in the componentization of “hello, world” is to write a console application that uses the HelloMessage class. The fully qualified name for the class— Microsoft.CSharp.Introduction.HelloMessage —could be used, but this name is quite long and unwieldy. An easier way is to use a using namespace directive, which makes it possible to use all of the types in a namespace without qualification. The example // HelloApp.cs using Microsoft.CSharp.Introduction; class HelloApp { static void Main() { HelloMessage m = new HelloMessage(); System.Console.WriteLine(m.Message); } } shows a using namespace directive that refers to the Microsoft.CSharp.Introduction namespace. The occurrences of HelloMessage are shorthand for Microsoft.CSharp.Introduction.HelloMessage.

Programming Handheld and Mobile devices 20 C# also enables the definition and use of aliases. A using alias directive defines an alias for a type. Such aliases can be useful in situation in which name collisions occur between two class libraries, or when a small number of types from a much larger namespace are being used. The example using MessageSource = Microsoft.CSharp.Introduction.HelloMessage ; shows a using alias directive that defines MessageSource as an alias for the HelloMessage class. The code we have written can be compiled into a class library containing the class HelloMessage and an application containing the class HelloApp. The details of this compilation step might differ based on the compiler or tool being used. Using the command-line compiler provided in Visual Studio.NET, the correct invocations are csc /target:library HelloLibrary.cs which produces a class library HelloLibrary.dll and csc /reference:HelloLibrary.dll HelloApp.cs which produces the application HelloApp.exe