Document 16518074

advertisement
Computing with C#
and the .NET Framework
Chapter 1
An Introduction to
Computing with C#
©2003, 2011 Art Gittleman
Processor
Output
Memory
Input
Storage
Figure 1.1 A computer system
Introduction to Computing
Processors have different instruction sets with
low-level simple instructions
High-level languages – more expressive,
closer to software design
Compilers translate from one language to
another
Interpreters execute code
High-level
Processor
program
ABC program
(executable)
Compiler
Figure 1.1 Translating a high-level program
The .NET Framework
Two main parts
Common Language Runtime (CLR) manages
execution of code and provides services
.NET Framework Class Library provides a
large and very useful set of types
The Common Language Runtime
Common Type System defines the types of
data that manages code can use.
Common Language Specification (CLS)
defines features that every language for
developing managed code must provide
Each language code compiles to MSIL,
Microsoft Intermediate Language.
Compiles to native code at runtime.
The .NET Framework Class Library
Groups types into namespaces. Contains
about 100 namespaces. For example,
System -- Contains fundamental types.
System.Drawing -- Provides graphics.
System.Windows.Forms -For user interfaces in Windows-based
applications
C# Features
Object-oriented – used for business objects or
system applications – internet applications
Goals include
productivity and safety
power, expressiveness, and flexibility
Combines rapid application development of
Visual Basic with power of C++
How C# works
Compiler translates C# source to MSIL code
for a virtual machine, similar to a hardware
processor
During runtime the CLR use a Just-In-Time
(JIT) compiler to translate the MSIL code to
the instruction set of the processor
JIT1
C#Source
Code
MSIL
code
Compiler
Processor 1 code
JIT2
Processor 2 code
Figure 1.2 Compiling and executing a Java program
public class Square {
static void Main() {
int number = 345;
// '=' denotes assignment
int squared = number * number;
// '*' denotes multiplication
System.Console.WriteLine
("The square of {0} is {1}", number, squared);
}
}
Example 1.1
The output of Example 1.1
The square of 345 is 119025
System namespace, Console class, WriteLine
method
"The square of {0} is {1}" is a pattern
The value of number substitutes for {0}
The value of squared substitutes for {1}
Lexical Structure
Rules for dividing program text into a sequence of
elements
Whitespace -- spaces, tabs, carriage returns
Comment – for human readers only
Punctuators ( ) { } [ ] ; , . :
Operators
+ - * / and so on
Literals – specific values such as 345
Identifiers – the names used for program entities
Keywords – reserved names for special uses
int
number = 345;
C# compiler lexical analyzer
keyword
identifier operator literal punctuator
(int)
(number)
(=)
(345)
(;)
(whitespace and comment discarded)
Figure 1.7 Lexical analysis of a single line of Example 1.1
Syntax
Grammatical rules for combining lexical
elements into programs
Declaration – defines a part of a program
Statements – controls execution sequence
Expressions – compute values
class declaration
method declaration
statement
statement
statement
Figure 1.8 The overall syntactic structure of Example 1.1
The Steps of Software Development
• Identifying the requirements of the system.
• Designing a system that meets the
requirements.
• Implementing the system.
• Testing that the system operates correctly
and meets the requirements.
• Making improvements as needed.
Download