Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 1 Introducing C# Essential C# Mark Michaelis Ch. 11 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson.

Similar presentations


Presentation on theme: "Chapter 1 Introducing C# Essential C# Mark Michaelis Ch. 11 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson."— Presentation transcript:

1 Chapter 1 Introducing C# Essential C# Mark Michaelis Ch. 11 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

2 Chapter 1 Overview C# language builds on previous C-style languages: C, C++, and Java. C# is used for building software components and applications. This chapter focuses on the fundamentals of C# syntax including variables, console I/O, and comments. The managed execution context is also discussed. Ch. 12 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

3 1.1 Introduction A program is a set of instructions that a computer follows to perform a task –Programs are commonly referred to as software –Without software, computers cannot do anything Programmers, or software developers, create software –They are people with the training and skills necessary to design, create, and test programs This book introduces fundamental programming concepts using C# Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

4 1.2 Hardware and Software Hardware refers to all physical devices –A computer consists of many pieces of hardware that all work together –Each piece of hardware does its own work A typical computer system contains: –The CPU –Main memory –Secondary storage devices –Input devices –Output devices Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

5 The CPU The central processing unit is the part that actually runs programs –The most important part of a computer –Today CPUs are microprocessor –Commonly used CPU vendors are Intel and AMD Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

6 Main Memory The computer’s work area Where the computer loads instructions of programs and data for processing Commonly known as RAM, random-access memory –Designed for CPUs to quickly access data stored at any random location in the RAM They are a volatile type of memory –When the computer is powered off, the contents in RAM are erased Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

7 Secondary Storage Devices Devices that can hold data for long periods of time, even when the power is off –Where important data and system files are stored –Most commonly used is the disk drive which stores data by magnetically encoding it onto a circular disk –Optical devices such as DVD-ROMs and CD-ROMs are also popular –USB drives are small devices that plug into a USB port Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

8 Input Devices Input is any data the computer collects from people and devices Devices that collect the data and send them to the computer are called input devices Commonly used input devices are keyboards, mouses, scanners, microphones, and digital cameras Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

9 Output Devices Output is any data the computer produces for people or devices The device that generates output for a computer is called an output device Commonly used output devices are video displays, speakers, and printers Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

10 Software Categorized mainly into system software and application software –System software are programs that control and manage the basic operations of a computer. Subcategories are: Operating systems Utility programs Software development tools –Application software are programs that perform special tasks Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

11 1.3 How Computers Store Data All data stored in a computer is converted to sequence of 0s and 1s; each sequence is called a bit A computer’s memory is divided into tiny storage locations called bytes –Eight bits make a byte Combinations of bits, 0s and 1s, are used to represent characters. For example, –The character ‘A’ is 65 in ASCII code, which is converted to binary format 1000001 –When you press ‘A’, 1000001 will be stored in computer’s memory Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

12 Digital and Digital Data “Digital” refers to anything that can only have two possible values –Digital data is the data that is stored in binary. –Digital devices are devices that work with binary data –Computers are digital devices Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

13 1.4 How a Program Works CPU reads instructions written in machine language called instruction set A program will be copied into memory for CPU to execute CPU uses the “fetch-decode-execute” cycle for processing –Fetch: Reads instructions from memory –Decode: Decodes the instructions that were just read to determine how to perform operations –Execute: Actually performs the operations Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

14 Programming Languages –Machine languages: sequences of 0s and 1s –Assembly languages: use short words known as “mnemonics” to write program Must be translated by assembler Still considered low-level languages –High-level languages: more human readable languages that allow programmers to create programs without knowing how CPU works. Modern languages are high-level Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

15 Keywords, Operators, and Syntax High level languages use keywords that have special meaning and cannot be used for any purpose other than to write programs Operators are keywords that represent special program functions such as addition, subtraction, and multiplication Syntax is a set of rules that must be strictly followed to write computer-understandable instructions –Syntax error is a mistake or violation of these rules Each instruction in a program is called a statement Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

16 Compilers and Interpreters A compiler translates a high-level language program to a separate machine program for CPU to read and execute –Source code: the statements a programmer writes in a high-level language –Compilation: translates a text-based source code to binary codes Interpreter reads, translates, and executes the instructions of a high-level language program –Examples are PHP, Perl, Python, and ASP.NET Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

17 1.5 Graphical User Interface User interfaces allow users to interact with the computer. Categories are: –Command line interface (aka console interface) –Graphical user interface (GUI)- now the most commonly used Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

18 1.6 Objects Most programming languages use object-oriented programming in which a program component is called an “object” Program objects have properties (or fields) and methods –Properties – data stored in an object –Methods – the operations an object can perform Form object Label objects TextBox objects Button objects Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

19 Controls Objects that are visible in a program GUI are known as controls –Commonly used controls are Labels, Buttons, and TextBoxes –They enhance the functionality of your programs There are invisible objects in a GUI such as Timers, and OpenFileDialog Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

20 The.NET Framework The.NET Framework is a collection of classes and other codes that can be used to create programs for Windows operating system C# is a language supported by the.NET Framework Controls are defined by specialized classes provided by the.NET Framework You can also write your own class to perform a special task Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

21 1.7 The Program Development Process The process of creating a program is known as the programming development cycle It has six phases: –Understand the program’s purpose –Design the GUI –Design the program’s logic –Write the code –Correct syntax errors –Test the program and correct logic errors Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

22 Algorithm, Pseudocode, Flowchart An algorithm is a set of well-defined, logical steps that must be taken to perform a task An algorithm that is written out in plain English is called pseudocode A flowchart is a diagram that graphically depicts the steps of an algorithm Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

23 1.8 Getting Started with the Visual Studio Environment Visual Studio 2012 is a professional integrated development environment (IDE) The Visual Studio Environment includes: –Designer Window (1) –Solution Explorer Window (2) –Properties Window (3) Auto Hide allows a window to display only as a tab of the edges (1) (2) (3) Pushpin icon means it supports Auto Hide Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

24 Menu Bar and Standard Toolbar Menu bar provides menus such as File, Edit, View, Project, etc. Standard toolbar contains buttons that execute frequently used commands Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

25 Toolbox and Tooltip Toolbox is a window for selecting controls to use in an application –Divided into sections such as “All Windows Forms” and “Common Controls” –Typically appears on the left side of Visual Studio environment –Usually in Auto Hide mode Tooltip is a small box that pops up when you hover the mouse pointer over an item on the toolbar or toolbox Toolbox tab Tooltip

26 Projects and Solutions Each Visual Studio application (including Visual C#) you will create is a project –A project contains several files. –Typically they are Form1.cs, Program.cs, etc. A solution is a container that can hold one or more Visual Studio (including Visual C#) projects –Each project, however, is saved in its own solution Solution Project Files belonging to the project

27 Specifying the Project Name You can specify the project name the first time you save the project. Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

28 Hello World Application Hello World, or some variation, is traditionally the first program written in a new language. Ch. 1 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc. 28 class Program { class HelloWorld { static void Main() { System.Console.WriteLine( "Hello. My name is Inigo Montoya."); }

29 Facts about Hello World C# files use the “.cs” file extension: – For example: HelloWorld.cs Command-line compilation of the HelloWorld program results in the creation of an executable assembly (HelloWorld.exe). A class library can also be used to generate an assembly with a file extension of “*.dll.” Ch. 129 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

30 Compiling and Running the Application Ch. 1 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc. 30

31 C# Syntax Fundamentals Keywords are reserved words that provide the concrete syntax that the compiler interprets. Examples of keywords include class, static, and void. The compiler uses keywords to identify the structure and organization of the code. The compiler enforces strict rules concerning the use and the location of keywords. Ch. 131 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

32 C# Identifiers Identifiers are the names for variables used by the application. A variable name can be used to assign a value to a variable and then refer to it later. The variable names chosen by the developer should be meaningful so that the resulting code is easier to understand. Keywords should be avoided as identifiers. Ch. 132 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

33 Type Definitions C# code is enclosed within a type definition, the most common type being the class type. The standard for naming types is called Pascal casing, capitalizing the first letter of each word: Greetings, HelloInigoMontoya, Hello. An alternative naming standard is camel casing, capitalizing the first letter of each word except for the first word: quotient, firstName, theDreadPirateRoberts. Ch. 133 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

34 Class Type Example Ch. 1 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc. 34 class HelloWorld { }

35 What is a Method? A method is a named block of code introduced by a method declaration and followed by zero or more statements enclosed within curly braces ({…}). Methods perform computations or actions. They provide a means to structure and organize code, avoiding the need to duplicate code. Ch. 135 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

36 The Main Method In C#, the Main() method is the entry point into the application. It is always static, meaning that it is called directly through the class name. It must return either a void or integer value. It may have an args parameter, which is an array of strings representing command-line arguments or no parameters at all. Ch. 136 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

37 Main Method Variations Ch. 1 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc. 37 static void Main(string[] args) { } static int Main(string[] args) { } static void Main() { } static int Main() { }

38 Line Termination C# uses a semicolon to end a statement. Examples of statements include variable declarations and method calls. Some programming elements do not end in a semicolon, for example, the switch statement or code blocks enclosed within curly braces. Multiple statements can be placed on one line, or a single statement can span multiple lines. Ch. 138 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

39 White Space Whitespace is the name given to formatting characters such as tabs, spaces, and newline characters. The C# compiler ignores whitespace in code, but it can improve readability. Whitespace in a quoted string is not ignored. Ch. 139 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

40 Variable Declarations A variable refers to a named storage location. Variables can be assigned a value after being declared and then used in various operations. The type of a variable cannot be changed once it has been declared. For example: string max; // string type named max Local variables are declared inside methods. Ch. 140 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

41 Data Types The type of data specified in the variable declaration is called the data type. Examples of data types include the following: – int is an integer type (32-bits in size); – char is a character type (16-bits in size). A data type is, therefore, a way to classify data having similar characteristics. Ch. 141 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

42 Variable Naming The name of a variable must begin with a letter or underscore followed by any number of letters, numbers, and/or underscores. By convention, local variables are camel-cased and do not include underscores. Ch. 142 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

43 Variable Declaration and Assignment After declaring a variable, a local variable must be assigned a value before it is accessed. A variable can be assigned a value in the variable declaration or in a separate statement. Ch. 143 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc. string valerie; string max = "Have fun storming the castle!"; valerie = "Think it will work?";

44 String Immutability String types are immutable, meaning not modifiable. A new literal can be assigned to a string variable, but this reassigns the variable to a new location in memory. Ch. 144 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

45 Console Input and Output Console Input/Output (I/O) involves reading and writing from the console window. The console is an operating system window where users interact with a text-based application. The Console class provides basic support for applications that read characters from, and write characters to, the console. Ch. 145 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

46 Example of Console I/O Ch. 1 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc. 46 static void Main() { string firstName; string lastName; System.Console.WriteLine("Hey you!"); System.Console.Write("Enter your first name: "); firstName = System.Console.ReadLine(); System.Console.Write("Enter your last name: "); lastName = System.Console.ReadLine(); System.Console.WriteLine("Your full name is {0} {1}.", firstName, lastName); }

47 Example of Console I/O (cont’d) Ch. 1 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc. 47

48 Reading from the Console Ch. 1 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc. 48 Console.Read(…) reads the next character from the standard input stream. Console.ReadKey(…) obtains the next character or function key pressed by the user. Console.ReadLine(…) reads the next line of characters from the standard input stream.

49 Writing to the Console Ch. 1 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc. 49 Console.Write(…) writes the text representation of the specified value or values to the standard output stream, without a line terminator. Console.WriteLine(…) writes the specified data, followed by a line terminator, to the standard output stream.

50 Comments Comments are used to describe and explain the code being written, for example, if the syntax is difficult to understand. The compiler ignores comments and generates an assembly that removes the comments from the original source code. Ch. 150 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

51 Types of Comments Comment TypeDescriptionExample Delimited commentsComments of this type may span multiple lines or appear embedded within a line of code. /* comment */ Single-line commentsThe compiler treats all text from the delimiter to the end of the line as a comment. // comment XML-delimited comments These have the same characteristics as regular delimited comments except the compiler can save them into a separate XML text file. /**comment**/ XML single-line comments The compiler can also save these comments into a separate XML text file. ///comment Ch. 151 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

52 Extensible Markup Language (XML) XML is a simple, flexible text format designed to describe the structure of data in an exchangeable format. Ch. 152 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

53 C# Compilation The C# compiler transforms the C# source code file into intermediate language (CIL). At runtime, the final compilation step, just-in- time (JIT) compilation, takes place, translating the intermediate language into platform- specific machine code. The Common Language Runtime (CLR) executes this final version of the program, a process called managed execution. Ch. 1 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc. 53

54 Common Language Infrastructure (CLI) An international standard that includes specifications for the following items: – The Common Language Runtime (CLR) – Common Intermediate Language (CIL) – Common Type System (CTS) – Common Language Specification (CLS) – Metadata supporting CLI services – Base Class Library (BCL). Ch. 154 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

55 CLI Features Language interoperability Type safety Code access security Garbage collection Platform portability Base Class Library Ch. 1 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc. 55

56 C# and.NET Versioning C#/.NET VersionDescription C# 1.0 with.NET Framework 1.0/1.1 (Visual Studio 2002 and 2003) The initial release of C#. C# 2.0 with.NET Framework 2.0 (Visual Studio 2005) Generics were added..NET Framework 3.0Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), Windows Workflow (WF), and CardSpace (web authentication). C# 3.0 with.NET Framework 3.5 (Visual Studio 2008) Added support for LINQ. C# 4.0 with.NET Framework 4.0 (Visual Studio 2010) Added support for dynamic typing. Ch. 156 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

57 Common Intermediate Language Common Intermediate Language (CIL) is the lowest-level programming language in the Common Language Infrastructure (CLI) and in the.NET Framework. ildasm.exe, a CIL disassembler utility, creates a viewable CIL representation of an assembly or executable. Ch. 157 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

58 Chapter 1 Summary This chapter served as a rudimentary introduction to C#. C# resembles C, C++, and Java in many ways. C# supports object-oriented programming methodology. The next chapter examines the fundamental data types that are part of the C# language. Ch. 158 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc.

59 The End Ch. 1 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson Education, Inc. 59


Download ppt "Chapter 1 Introducing C# Essential C# Mark Michaelis Ch. 11 Excerpted from Essential C# 4.0 by Mark Michaelis (ISBN: 0321694694). Copyright 2010 Pearson."

Similar presentations


Ads by Google