Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Programming C# Introduction.

Similar presentations


Presentation on theme: "Advanced Programming C# Introduction."— Presentation transcript:

1 Advanced Programming C# Introduction

2 C# Genealogy Object Oriented Advanced Object Oriented
Elementary Procedural Advanced Procedural Special Procedural Eiffel Fortran Algol 68 C C++ C# Cobol PL/I Pascal Ada 95 Java Ada 83

3 Program A program is a machine-compatible representation of an algorithm If no algorithm exists for performing a task, then the task can not be performed by a machine Programs and algorithms they represent collectively referred to as Software

4 Programming Languages
Machine language Strings of 0 and 1, machine dependent Assembly language English like abbreviations representing elementary operations Assemblers needed to convert assembly into machine language High-level languages (C, C++, Java, …) Single statements accomplish substantial tasks Compiler needed to convert the program into machine language Some languages are interpreted (executed without the need for compilation)

5 Example Machine Code Fragment
A number specifies what action the computer should take.

6 Example Assembly Code Fragment
movl (%edx,%eax), %ecx movl 12(%ebp), %eax leal 0(,%eax,4), %edx movl $nodes, %eax movl (%edx,%eax), %eax fldl (%ecx) fsubl (%eax) movl 8(%ebp), %eax Symbols to help programmers to remember the words.

7 Programming Languages
Examples of High-level languages FORTRAN (FORmula TRANslator) in 1950s scientific and engineering applications COBOL (Common Business Oriented language) in 1959 Commercial applications for manipulation of large amounts of data BASIC (Beginner’s All Purpose Symbolic Instruction Code) Familiarize novices with programming techniques (in mid-1960s) Pascal (17th century mathematician Blaise Pascal) in 1971 Teaching structured programming in academic environments

8 Programming Languages
C Bell labs  Dennis Ritchie, 1973 C++ Bjarne Stroustrup, 1980 Hybrid OOP Java Sun Microsystems (formally announced in May 1995) Pure OOP Web programming

9 What is C#? C# (pronounced "C sharp") is an object-oriented language that is used to build applications for the Microsoft .NET platform C# is designed by Microsoft to combine the power of C/C++, Java and the productivity of Visual Basic The goal of C# and the .NET platform is to shorten development time by allowing developers to spend their time working on the application logic instead of low level programming details

10 Why C#? support so-called safe internet programming
simpler than other object-oriented languages [C++] safe and robust --- no core dump or dead console good graphics package related to C and C++ good client-server and network support good for your future job

11 C# A component oriented language
C# is the first “component oriented” language Component concepts are first class: Properties, methods, events Design-time and run-time attributes Integrated documentation using XML Enables one-stop programming No header files, IDL, etc. Can be embedded in web pages

12 C# Everything really is an object
Traditional views C++, Java: Primitive types are “magic” and do not interoperate with objects Smalltalk, Lisp: Primitive types are objects, but at great performance cost C# unifies with no performance cost Deep simplicity throughout system Improved extensibility and reusability New primitive types: Decimal, SQL… Collections, etc., work for all types

13 Learning C# Just like learning any new language Syntax: “new words”
Grammar: how to put them together Programming: telling a coherent story Library: use plots already written Picking up Java and C++ after C# should be easy!

14 Programming Languages C#
Microsoft announced the .NET initiative (June 2000) and C# .NET enables web-based applications to be distributed to a great variety of devices and desktop computers Allows applications created in various programming languages to communicate with each other Designed to ease migration to .NET (roots in C and C++) Event-driven, fully OO visual programming language

15 Microsoft’s .NET Technologies
Many languages run on .NET framework C#, C++, J#, Visual Basic even have Python (see IronPython)

16 Example C++/C#/Java Code Fragment
bool DetermineNeighbor(int i, int j) { double distanceX = (nodes[i].x - nodes[j].x); double distanceY = (nodes[i].y - nodes[j].y); double distanceSquare = disx * disx + disy * disy; double distance = sqrt(distanceSquare); if (distance < radius) return true; else return false; } You do not need to understand the exact meaning of this program, just the feeling.

17 Visual Studio HTML 5 / CSS 3 / Javascript
Full HTML 5 and CSS 3 specs supported Quirks for downlevel browsers fully supported CSS and JavaScript now first class citizens in editor DOCTYPE JavaScript Based on Chakra engine in IE 9 Directly tell us which JS files to provide intellisense

18 C# compiling. C# is executed indirectly through an abstract computer architecture called the CLR. CLR => Common Language Runtime. Abstract, but well defined. C# programs are compiled to an IL. Also called MSIL, CIL (Common Intermediate Language) or bytecode.

19 Compiling C# Source Code
C# file names have the extension .cs To create the IL file, we need to compile the .cs file using the csc (using the command line), followed by the name of the source file The result is a file with the same name but the .exe extension, which is called an assembly The assembly file contains all the information that the common runtime needs to know to execute the program We can also create multi-file assemblies using an assembly linker, see the following link for more details:

20 C# Compilation and Execution
C# source code MSIL C# compiler Machine code Just in time compiler

21 C# and Language Comparison 60% Java, 10% C++, 10% VB, 20% new
As in Java Object-orientation (single inheritance) Namespaces (like packages) Interfaces Strong typing Exceptions Threads Garbage collection Reflection Dynamic loading of code As in C++ Operator overloading Pointer arithmetic in unsafe code Some syntactic details As in VB Properties Events RAD development

22 OOP Structured programming versus OOP
Objects have properties (attributes) and perform actions Objects are defined in classes (represent groups of related objects) Programs are more understandable better organized programs

23 Introduction to C# Console applications Windows applications
No visual components Only text output Two types MS-DOS prompt (Used in Windows 95/98/ME) Command prompt (Used in windows 2000/NT/XP) Windows applications Forms with several output types Contain Graphical User Interfaces (GUIs)

24 New Project dialog.

25 New Project dialog. New project dialogue C# .NET project
Group of related files, images, and documentations C# .NET solution Group of projects creating one or a group of applications Windows Applications Anything that runs in the Windows OS Microsoft Word Microsoft Internet Explorer

26

27 Simple Program

28 A First C# Program: 'Hello World'
using System; public class HelloWorld { public static void Main(string[] args) // This is a single line comment /* This is a multiple line comment */ Console.WriteLine("Hello World"); }

29 Simple Program Execution of the Welcome1 program.

30 Windows Application menu title bat tabs menu bar active tab
Solution Explorer Form (windows application) Properties window

31 Windows Application IDE after a new project The form Tabs
Grey rectangle in window Represents the project’s window Part of the GUI or Graphical User Interface Graphical components for user interaction User can enter data (input) Shows user instructions or results (output) Tabs One tab appears for each open document Used to save space in the IDE

32 Simple Program: Displaying Text and an Image

33 Simple Program: Displaying Text and an Image
Project type Project name Click to change project location Project location Creating a new Windows application.

34

35

36 Simple Program: Displaying Text and an Image
Name and type of object Selected property Property value Property description Setting the form’s Text property.

37 Simple Program: Displaying Text and an Image
Resize the form Click and drag one of the forms size handles Enables handles are white Disables handles are gray The grid in the background will not appear in the solution Change the form’s background color The BackColor determines the form’s background color Dropdown arrow is used to set the color Add a label control to the form Controls can be dragged to the form Controls can be added to the form by double clicking The forms background color is the default of added controls

38 Simple Program: Displaying Text and an Image
Title bar grid Disabled sizing handle Mouse pointer over a sizing handle Enabled sizing handle Form with sizing handles.

39 Simple Program: Displaying Text and an Image
Current color Custom palette Down arrow Changing property BackColor.

40 Simple Program: Displaying Text and an Image
Label control New background color Adding a new label to the form.

41 Simple Program: Displaying Text and an Image
Set the label’s text The Text property is used to set the text of a label The label can be dragged to a desired location Or Format > Center In Form > Horizontal can also be used to position the label as in in this example Set the label’s font size and align text The Font property changes the label’s text (Fig. 2.23) The TextAlign property to align the text Add a picture box to the form Picture boxes are used to display pictures Drag the picture box onto the form

42 Simple Program: Displaying Text and an Image
Label centered with updated Text property Label in position with its Text property set.

43 Simple Program: Displaying Text and an Image
Ellipsis indicate dialog will appear Properties window displaying the label’s properties.

44 Simple Program: Displaying Text and an Image
Current font Font size Font window for selecting fonts, styles and sizes.

45 Simple Program: Displaying Text and an Image
Text alignment option Top-center alignment option Centering the text in the label.

46 Simple Program: Displaying Text and an Image
Updated Label New PictureBox Inserting and aligning the picture box.

47 Simple Program: Displaying Text and an Image
Insert an image The Image property sets the image that appears Pictures should be of type .gif, .jpeg, or .png The picture box is resizable to fit the entire image Save the project In the Solution Explorer select File > Save Using Save All will save the source code and the project Run the project In run mode several IDE features are disabled Click Build Solution in the Build menu to compile the solution Click Debug in the Start menu or press the F5 key

48 Simple Program: Displaying Text and an Image
Image property value (no image selected) Image property of the picture box.

49 Simple Program: Displaying Text and an Image
Selecting an image for the picture box.

50 Simple Program: Displaying Text and an Image
Newly inserted image (after resizing the picture box) Picture box after the image has been inserted.

51 Simple Program: Displaying Text and an Image
Start button Run mode Design form End button Design form (grid) Running application run mode, with the running application in the foreground.

52 Simple Program: Displaying Text and an Image
Terminating the program Click the close button (x in the top right corner) Or click the End button in the toolbar

53 Major Language Differences
Automatic memory management Garbage Collection No pointers Everything inherits from System.Object Additional language constructs to aid in implementing common patterns: Iterators – foreach and yield statements Data encapsulation – Properties Function pointers or Functors – delegates Observer Design Pattern – events Aspect-oriented programming – Attributes (loosely) Functional programming – LINQ and anonymous methods

54 Other Language Differences
Conditionals must evaluate to a Boolean No pointers. Use the dot “.” to access both namespaces and fields/methods. All fields are initialized by the CLR (zero for value types, null for reference types). The switch statement can take bool’s , enum’s, integral types and strings. Expressions must be useful (no a==b;).

55 Simple Program // A first program in C#. using System; class Welcome1
{ static void Main( string[] args ) Console.WriteLine( "Welcome to C# Programming!" ); } }

56 Constructions of Note using namespace class
like import in Java: bring in namespaces namespace disambiguation of names like Internet hierarchical names and C++ naming class like in C++ or Java single inheritance up to object

57 Constructions of Note static void Main() Console.Write(Line)
Defines the entry point for an assembly. Four different overloads – taking string arguments and returning int’s. Console.Write(Line) Takes a formatted string: “Composite Format” Indexed elements: e.g., {0} can be used multiple times only evaluated once {index [,alignment][:formatting]}

58 Common Type System (CTS)
From MSDN

59 Atomic Data

60 Built-in Types C# predefined types The “root” object Logical bool
Signed sbyte, short, int, long Unsigned byte, ushort, uint, ulong Floating-point float, double, decimal Textual char, string Textual types use Unicode (16-bit characters)

61 Type System Value types Reference types Primitives int i;
Enums enum State { Off, On } Structs struct Point { int x, y; } Reference types Classes class Foo: Bar, IFoo {...} Interfaces interface IFoo: IBar {...} Arrays string[] a = new string[10]; Delegates delegate void Empty();

62 C# Comments Comments can be created using //…
Multi-lines comments use /* … */ Comments are ignored by the compiler

63 C# Keywords Words that cannot be used as variable or class names
Have a specific unchangeable function within the language Example: class

64 Classes And Structs class CPoint { int x, y; ... }
struct SPoint { int x, y; ... } CPoint cp = new CPoint(10, 20); SPoint sp = new SPoint(10, 20); 10 sp 20 cp CPoint 10 20

65 C# Classes Class names can only be one word long (i.e. no white space in class name ) Class names are capitalized, with each additional English word capitalized as well (e.g., MyFirstProgram ) Each class name is an identifier Can contain letters, digits, and underscores (_) Cannot start with digits Can start with the at symbol

66 C# Class Methods Class bodies start with a left brace ({)
Class bodies end with a right brace (}) Methods Building blocks of programs The Main method Each console or windows application must have exactly one All programs start by executing the Main method Braces are used to start ({) and end (}) a method

67 C# Statements Anything in quotes (“) is considered a string
Every statement must end in a semicolon (;)

68 C#

69 NameSpaces You import namespaces when you want to be able to refer to classes by their short name, rather than full name For example, import System.XML allows XmlDataDocument and XmlNode rather than System.XML.XmlDataDocument and System.XML.XmlNode to be in your code.

70 Events Events are a way for an object to communicate with those that are interested in what it has to offer, like a button has a click event Interested parties use Event Handlers, which are a way of subscribing to the event


Download ppt "Advanced Programming C# Introduction."

Similar presentations


Ads by Google