Sie sind auf Seite 1von 47

Your First

C#
Program

C# Programming: From Problem Analysis to Program Design


2nd Edition

C# Programming: From Problem Analysis to Program Design 1


C#

• One of the newest programming languages


• Conforms closely to C and C++
• Has the rapid graphical user interface (GUI)
features of previous versions of Visual Basic
• Has the added power of C++
• Has the object-oriented class libraries similar to
Java

C# Programming: From Problem Analysis to Program Design 2


C# (continued)
• Can be used to develop a number of applications
– Software components
– Mobile applications
– Dynamic Web pages
– Database access components
– Windows desktop applications
– Web services
– Console-based applications

C# Programming: From Problem Analysis to Program Design 3


.NET
• Not an operating system
• An environment in which programs run
• Resides at a layer between operating system and
other applications
• Offers multilanguage independence
– One application can be written in more than one
language
• Includes over 2,500 reusable types (classes)
• Enables creation of dynamic Web pages and Web
services
• Scalable component development

C# Programming: From Problem Analysis to Program Design 4


C# Relationship to .NET

• Many compilers targeting the .NET platform are


available
• C# was used most heavily for development of the
.NET Framework class libraries
• C#, in conjunction with the .NET Framework
classes, offers an exciting vehicle to incorporate
and use emerging Web standards

C# Programming: From Problem Analysis to Program Design 5


C# Relationship to .NET (continued)

• C# is object-oriented
• In 2001, the European Computer Manufacturers
Association (ECMA) General Assembly ratified
C# and its common language infrastructure (CLI)
specifications into international standards

C# Programming: From Problem Analysis to Program Design 6


Visual Studio 2005

• Launched November 2005


– Included new language features (C# 2.0)
• i.e. partial classes, generics,
– Added enhancements to the IDE
• i.e. refactoring, code snippets
• Less than 6 months after the release, specifications
for C# 3.0 and the next version of Visual Studio
(code named Orcas) were unveiled [May 2006]

C# Programming: From Problem Analysis to Program Design 7


Types of Applications Developed
with C#
• Web applications
• Windows graphical user interface (GUI)
applications
• Console-based applications
• Class libraries and stand-alone components (.dlls),
smart device applications, and services can also be
created
C# Programming: From Problem Analysis to Program Design 8
Web Applications

• C# was designed with the Internet applications in


mind
• Can quickly build applications that run on the
Web with C#
– Using Web Forms: part of ASP.NET

C# Programming: From Problem Analysis to Program Design 9


Web Applications (continued)

Figure 2-1 Web application written using C#


C# Programming: From Problem Analysis to Program Design 10
Windows Applications

• Applications designed for the desktop


• Designed for a single platform
• Use classes from System.Windows.Form
• Applications can include menus, pictures, drop-
down controls, buttons, textboxes, and labels
• Use drag-and-drop feature of Visual Studio

C# Programming: From Problem Analysis to Program Design 11


Windows Applications (continued)

Figure 2-2 Windows application written using C#


C# Programming: From Problem Analysis to Program Design 12
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

C# Programming: From Problem Analysis to Program Design 13


Exploring the First C# Program
line 1 // This is traditionally the first program written.
line 2 using System; Comments
line 3 namespace FirstProgram in green
line 4 {
line 5 class HelloWorld Keywords
line 6 { in blue
line 7 static void Main( )
line 8 {
line 9 Console.WriteLine(“Hello World!”);
line 10 }
line 11 }
line 12 }

C# Programming: From Problem Analysis to Program Design 14


Output from the First C# Program

Console-based
application
output

Figure 2-3 Output from Example 2-1 console application

C# Programming: From Problem Analysis to Program Design 15


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
C# Programming: From Problem Analysis to Program Design 16
Comments

• Make the code more readable

• Three types of commenting syntax


– Inline comments (//)

– Multiline comments or block comments (/* */)

– XML documentation comments (///)

C# Programming: From Problem Analysis to Program Design 17


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;

C# Programming: From Problem Analysis to Program Design 18


namespace
• Namespaces provide scope for the names defined
within the group
– Captain example
• 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: { }
C# Programming: From Problem Analysis to Program Design 19
namespace (continued)
Predefined namespace
From Example 2-1 (System)– part of
.NET FCL

line 1 // This is traditionally the first program written.


line 2 using System;
line 3 namespace FirstProgram
line 4 {
User defined
namespace
line 12 } Body of user
defined
namespace

C# Programming: From Problem Analysis to Program Design 20


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

C# Programming: From Problem Analysis to Program Design 21


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 {
User
defined
line 11 } class
line 12 }

C# Programming: From Problem Analysis to Program Design 22


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( )

C# Programming: From Problem Analysis to Program Design 23


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

C# Programming: From Problem Analysis to Program Design 24


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
– Parentheses “( )” used for arguments
• No arguments for Main( ) – empty parentheses

C# Programming: From Problem Analysis to Program Design 25


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

C# Programming: From Problem Analysis to Program Design 26


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

C# Programming: From Problem Analysis to Program Design 27


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.”);

C# Programming: From Problem Analysis to Program Design 28


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
C# Programming: From Problem Analysis to Program Design 29
Program Statements (continued)
• Special characters enclosed in double quotes

C# Programming: From Problem Analysis to Program Design 30


C# Elements

Figure 2-4 Relationship among C# elements


C# Programming: From Problem Analysis to Program Design 31
Installing .NET Framework

• .NET Framework must be installed to:


– Compile, build, and run a C# application
• Can download Microsoft’s .NET Framework
Software Development Kit (SDK)→free download
• OR install Visual Studio software (from book)
• Create a place to store your work

C# Programming: From Problem Analysis to Program Design 32


Installing .NET Framework
(continued)
• Use the Visual Studio Integrated Development
Environment (IDE) → Built-in editor
– Type your program statements
• Use Visual Studio IDE → Built-in compiler
– Check for syntax rule violations
– Compiler generates a file with an .exe extension
• Use Visual Studio IDE → Built-in debugger
• Use Visual Studio IDE → Built-in executor
C# Programming: From Problem Analysis to Program Design 33
Create Console Application

• Begin by opening Visual Studio


• Create new project
– Select New Project on the Start page
– OR use File → New Project option

C# Programming: From Problem Analysis to Program Design 34


Create New Project

Figure 2-6 Creating a console application


C# Programming: From Problem Analysis to Program Design 35
Code Automatically Generated

Figure 2-7 Code automatically generated by Visual Studio


C# Programming: From Problem Analysis to Program Design 36
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

C# Programming: From Problem Analysis to Program Design 37


Rename Source Code Name

Clicking Yes
causes the class
name to also be
renamed

Figure 2-8 Changing the source code name from Class1


C# Programming: From Problem Analysis to Program Design 38
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( );

C# Programming: From Problem Analysis to Program Design 39


Build Visual Studio Project

Figure 2-9 Compilation of a project using Visual Studio

C# Programming: From Problem Analysis to Program Design 40


Running an Application

Figure 2-10 Execution of an application using Visual Studio

C# Programming: From Problem Analysis to Program Design 41


Debugging an Application

• Types of errors
– Syntax errors
• Typing error
• Misspelled name
• Forget to end a statement with a semicolon
– Run-time errors
• Failing to fully understand the problem
• More difficult to detect

C# Programming: From Problem Analysis to Program Design 42


Error Listing
Missing
ending double
quotation
mark

Pushpin

Errors reported

Figure 2-12 Syntax error message listing


C# Programming: From Problem Analysis to Program Design 43
Creating an Application –
ProgrammingMessage Example

Figure 2-13 Problem specification sheet for the


ProgrammingMessage example
C# Programming: From Problem Analysis to Program Design 44
ProgrammingMessage Example
(continued)

Figure 2-14 Prototype for the ProgrammingMessage example


C# Programming: From Problem Analysis to Program Design 45
ProgrammingMessage Example
(continued) Figure 2-16
Recommended
deletions

May want to remove the XML


comments (lines beginning with ///)
Change the name

Delete [STAThread]
Depending on your
Can replace with static void Main( )
current settings, you
may not need to make Replace TODO: with your program
some of these changes statements

C# Programming: From Problem Analysis to Program Design 46


ProgrammingMessage Example
/* Programmer: [supply your name] (continued)
Date: [supply the current date]
Purpose: This class can be used to send messages to the output
screen.
*/ Complete
using System;
namespace ProgrammingMessage
program
{ listing
class ProgrammingDisplay
{
static void Main( )
{
Console.WriteLine(“Programming can be”);
Console.WriteLine(“FUN!”);
Console.Read( );
}
}
} C# Programming: From Problem Analysis to Program Design 47

Das könnte Ihnen auch gefallen