Download presentation
Presentation is loading. Please wait.
Published byLuke Quinn Modified over 9 years ago
1
C# Programming: From Problem Analysis to Program Design1 2 Your First C# Program
2
C# Programming: From Problem Analysis to Program Design2 Chapter Objectives Explore a program written in C# Examine the basic elements of a C# program Compile, run, and build an application Create an application that displays output
3
C# Programming: From Problem Analysis to Program Design3 Console Applications Normally send requests to the operating system Display text on the command console Easiest to create –Simplest approach to learning software development –Minimal overhead for input and output of data
4
C# Programming: From Problem Analysis to Program Design4 Exploring the First C# Program line 1 // This is traditionally the first program written. line 2 using System; //why do we need this? line 3 namespace FirstProgram line 4 { line 5 class HelloWorld //can it be different from the file name? line 6 { line 7 static void Main( ) //can it be main? Can we omit static? line 8 { line 9 Console.WriteLine(“Hello World!”); //how many arguments? line 10 } line 11 } line 12 } Comments in green Keywords in blue
5
C# Programming: From Problem Analysis to Program Design5 Output from the First C# Program Console-based application output Figure 2-3 Output from Example 2-1 console application
6
C# Programming: From Problem Analysis to Program Design6 Elements of a C# Program Comments –line 1 // This is traditionally the first program written. –Like making a note to yourself or readers of your program –Not considered instructions to the computer –Not checked for rule violations –Document what the program statements are doing
7
C# Programming: From Problem Analysis to Program Design7 Comments Make the code more readable Three types of commenting syntax –Inline comments –Multiline comments –XML documentation comments
8
C# Programming: From Problem Analysis to Program Design8 Inline Comments Indicated by two forward slashes (//) Considered a one-line comment Everything to the right of the slashes ignored by the compiler Carriage return (Enter) ends the comment // This is traditionally the first program written.
9
C# Programming: From Problem Analysis to Program Design9 Multiline Comment Forward slash followed by an asterisk (/*) marks the beginning Opposite pattern (*/) marks the end Also called block comments Does not allow you to nest multiline comments /* This is the beginning of a block multiline comment. It can go on for several lines or just be on a single line. No additional symbols are needed after the beginning two characters. Notice there is no space placed between the two characters. To end the comment, use the following symbols. */
10
C# Programming: From Problem Analysis to Program Design10 using Directive Permits use of classes found in specific namespaces without having to qualify them Framework class library –Over 2,000 classes included Syntax –using namespaceIdentifier;
11
C# Programming: From Problem Analysis to Program Design11 namespace Namespaces provide scope for the names defined within the group Groups semantically related types under a single umbrella System: most important and frequently used namespace Can define your own namespace –Each namespace enclosed in curly braces: { }
12
C# Programming: From Problem Analysis to Program Design12 namespace ( continued ) From Example 2-1 line 1 // This is traditionally the first program written. line 2 using System; line 3 namespace FirstProgram line 4 { line 12 } Predefined namespace (System)– part of.NET FCL User defined namespace Body of user defined namespace
13
C# Programming: From Problem Analysis to Program Design13 class Building block of object-oriented program Everything in C# is designed around a class Every program must have at least one class Classes define a category, or type, of object Every class is named
14
C# Programming: From Problem Analysis to Program Design14 class ( continued ) line 1 // This is traditionally the first program written. line 2 using System; line 3 namespace FirstProgram line 4 { line 5 class HelloWorld line 6 { line 11 } line 12 } User defined class
15
C# Programming: From Problem Analysis to Program Design15 class ( continued ) Define class members within curly braces –Include data members Stores values associated with the state of the class –Include method members Performs some behavior of the class Can call predefined classes’ methods –Main( )
16
C# Programming: From Problem Analysis to Program Design16 Main( ) “Entry point” for all applications –Where the program begins execution –Execution ends after last statement in Main( ) Can be placed anywhere inside the class definition Applications must have one Main( ) method Begins with uppercase character
17
C# Programming: From Problem Analysis to Program Design17 Main( ) Method Heading line 7 static void Main( ) –Begins with the keyword static –Second keyword → return type void signifies no value returned –Name of the method Main is the name of Main( ) method Must be Main() instead of main() (like in C++) –Parentheses “( )” used for arguments No arguments for Main( ) – empty parentheses
18
C# Programming: From Problem Analysis to Program Design18 Body of a Method Enclosed in curly braces –Example Main( ) method body line 7 static void Main( ) line 8 { line 9 Console.WriteLine(“Hello World!”); line 10 } Includes program statements –Calls to other method Here Main( ) calling WriteLine( ) method
19
C# Programming: From Problem Analysis to Program Design19 Method Calls line 9 Console.WriteLine(“Hello World!”); Program statements WriteLine( ) → member of the Console class Main( ) invoking WriteLine( ) method Member of Console class Method call ends in semicolon
20
C# Programming: From Problem Analysis to Program Design20 Program Statements Write ( ) → Member of Console class –Argument(s) enclosed in double quotes inside ( ) –“Hello World!” is the method’s argument –“Hello World!” is string argument string of characters May be called with or without arguments –Console.WriteLine( ); –Console.WriteLine(“WriteLine( ) is a method.”); –Console.Write(“Main( ) is a method.”);
21
C# Programming: From Problem Analysis to Program Design21 Program Statements ( continued ) Read( ) accepts one character from the input device ReadLine( ) accepts string of characters from the input device –Until the enter key is pressed Write( ) does not automatically advance to next line Write(“An example\n”); –Same as WriteLine(“An example”); –Includes special escape sequences
22
C# Programming: From Problem Analysis to Program Design22 Program Statements ( continued ) Special characters enclosed in double quotes
23
C# Programming: From Problem Analysis to Program Design23 C# Elements Figure 2-4 Relationship among C# elements
24
What does Java do? import java.io.*; public class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); // Display the string. }
25
What does C++ do? // a small C++ program #include int main() { std::cout << “Hello, world!” << std:: endl; return 0; }
26
C# Programming: From Problem Analysis to Program Design26 Create Console Application Begin by opening Visual Studio Create new project –Select New Project on the Start page –OR use File → New Project option
27
C# Programming: From Problem Analysis to Program Design27 Create New Project Figure 2-6 Creating a console application
28
C# Programming: From Problem Analysis to Program Design28 Code Automatically Generated Figure 2-7 Code automatically generated by Visual Studio
29
C# Programming: From Problem Analysis to Program Design29 Typing Your Program Statements IntelliSense feature of the IDE Change the name of the class and the source code filename –Use the Solution Explorer Window to change the source code filename Select View → Solution Explorer
30
C# Programming: From Problem Analysis to Program Design30 Rename Source Code Name Figure 2-8 Changing the source code name from Class1 Clicking Yes causes the class name to also be renamed
31
C# Programming: From Problem Analysis to Program Design31 Compile and Run Application To Compile – click Build on the Build menu To run or execute application – click Start or Start Without Debugging on the Debug menu –Shortcut – if execute code that has not been compiled, automatically compiles first Start option does not hold output screen → output flashes quickly –Last statement in Main( ), add Console.Read( );
32
C# Programming: From Problem Analysis to Program Design32 Build Visual Studio Project Figure 2-9 Compilation of a project using Visual Studio
33
C# Programming: From Problem Analysis to Program Design33 Running an Application Figure 2-10 Execution of an application using Visual Studio
34
C# Programming: From Problem Analysis to Program Design34 Debugging an Application Types of errors (which one is harder to detect?) –Syntax errors Typing error Misspelled name Forget to end a statement with a semicolon –Run-time errors Failing to fully understand the problem Logical error
35
C# Programming: From Problem Analysis to Program Design35 Error Listing Figure 2-12 Syntax error message listing Pushpin Errors reported Missing ending double quotation mark
36
C# Programming: From Problem Analysis to Program Design36 Creating an Application – ProgrammingMessage Example Figure 2-13 Problem specification sheet for the ProgrammingMessage example
37
C# Programming: From Problem Analysis to Program Design37 ProgrammingMessage Example ( continued ) Figure 2-14 Prototype for the ProgrammingMessage example
38
C# Programming: From Problem Analysis to Program Design38 ProgrammingMessage Example ( continued ) Pseudocode would include a single line to display the message “Programming can be FUN!” on the output screen Figure 2-15 Algorithm for ProgrammingMessage example
39
C# Programming: From Problem Analysis to Program Design39 ProgrammingMessage Example ( continued ) Figure 2-16 Recommended deletions Depending on your current settings, you may not need to make some of these changes May want to remove the XML comments (lines beginning with ///) Delete [STAThread] Replace TODO: with your program statements Change the name Can replace with static void Main( )
40
C# Programming: From Problem Analysis to Program Design40 /* Programmer:[supply your name] Date:[supply the current date] Purpose:This class can be used to send messages to the output screen. */ using System; namespace ProgrammingMessage { class ProgrammingDisplay { static void Main( ) { Console.WriteLine(“Programming can be”); Console.WriteLine(“FUN!”); Console.Read( ); } Complete program listing ProgrammingMessage Example ( continued )
41
C# Programming: From Problem Analysis to Program Design41 Chapter Summary Types of applications developed with C# –Web applications –Windows graphical user interface (GUI) applications –Console-based applications Framework class library groups by namespaces –Namespaces group classes –Classes have methods –Methods include program statements
42
C# Programming: From Problem Analysis to Program Design42 Chapter Summary ( continued ) Visual Studio includes.NET Framework –Editor tool, compiler, debugger, and executor –Compile using Build –Run using Start or Start without Debugging Debugging –Syntax errors –Run-time errors Use five steps to program development to create applications
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.