C# and the .NET Framework An Introduction to C#
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
Translating a high-level program Processor ABC program (executable) Compiler 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
Compiling and executing a Java program JIT1 C#Source Code MSIL code Processor 1 code Compiler JIT2 Processor 2 code 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); }
The output of Example 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
C# compiler lexical analyzer int number = 345; C# compiler lexical analyzer keyword identifier operator literal punctuator (int) (number) (=) (345) (;) (whitespace and comment discarded) Lexical analysis of a single line of Example 1
Syntax Grammatical rules for combining lexical elements into programs Declaration – defines a part of a program Statements – controls execution sequence Expressions – compute values
The overall syntactic structure of Example 1.1 class declaration method declaration statement statement statement 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.