Visual Programming Comp315 Chapter 1 Introduction to NET

  • Slides: 24
Download presentation
Visual Programming Comp-315 Chapter #1 Introduction to. NET Framework & Introduction to C#

Visual Programming Comp-315 Chapter #1 Introduction to. NET Framework & Introduction to C#

What Is. NET • . NET is a new framework for developing web- based

What Is. NET • . NET is a new framework for developing web- based and windows-based applications within the Microsoft environment. • The framework offers a fundamental shift in Microsoft strategy: it moves application development from client-centric to servercentric.

Framework, Languages, And Tools VB. NET C# C++ Jscript … ASP. Net Windows Forms

Framework, Languages, And Tools VB. NET C# C++ Jscript … ASP. Net Windows Forms ADO. Net and XML Base Class Library Common Language Runtime (CLR) Visual Studio. NET Common Language Specification

Common Language Runtime (CLR) • CLR works like a virtual machine in executing all

Common Language Runtime (CLR) • CLR works like a virtual machine in executing all languages. • All. NET languages must obey the rules and standards imposed by CLR. Examples: • • Object declaration, creation and use Data types, language libraries Error and exception handling Interactive Development Environment (IDE)

Common Language Runtime • Development • Mixed language applications • • Common Language Specification

Common Language Runtime • Development • Mixed language applications • • Common Language Specification (CLS) Common Type System (CTS) Standard class framework Automatic memory management Consistent error handling and safer execution Potentially multi-platform Deployment • • Removal of registration dependency Safety – fewer versioning problems

Common Language Runtime Multiple Language Support • CTS is a rich type system built

Common Language Runtime Multiple Language Support • CTS is a rich type system built into the CLR – Implements various types (int, double, etc) – And operations on those types • CLS is a set of specifications that language and library designers need to follow – This will ensure interoperability between languages

Compilation in. NET Code in VB. NET Code in C# Code in another. NET

Compilation in. NET Code in VB. NET Code in C# Code in another. NET Language VB. NET compiler C# compiler Appropriate Compiler IL(Intermediate Language) code CLR just-in-time execution

Intermediate Language (IL) • . NET languages are not compiled to machine code. They

Intermediate Language (IL) • . NET languages are not compiled to machine code. They are compiled to an Intermediate Language (IL). • CLR accepts the IL code and recompiles it to machine code. The recompilation is just-in-time (JIT) meaning it is done as soon as a function or subroutine is called. • The JIT code stays in memory for subsequent calls. In cases where there is not enough memory it is discarded thus making JIT process interpretive.

Languages • Languages provided by MS • • VB, C++, C#, JScript Third-parties are

Languages • Languages provided by MS • • VB, C++, C#, JScript Third-parties are building • APL, COBOL, Pascal, Eiffel, Haskell, ML, Oberon, Perl, Python, Scheme, Smalltalk…

ASP. NET • ASP. NET, the platform services that allow to program Web Applications

ASP. NET • ASP. NET, the platform services that allow to program Web Applications and Web Services in any. NET language • ASP. NET Uses. NET languages to generate HTML pages. HTML page is targeted to the capabilities of the requesting Browser • ASP. NET “Program” is compiled into a. NET class and cached the first time it is called. All subsequent calls use the cached version.

C# Programming Language • Developed at Microsoft by Anders Hejlsberg et al • Event

C# Programming Language • Developed at Microsoft by Anders Hejlsberg et al • Event driven, object oriented, visual programming language • Based from C, C++ and Java • Incorporated into. NET platform • Web based applications can be distributed • Programs that can be accessed by anyone through any device • Allows communicating with different computer languages • Integrated Design Environment (IDE) • Makes programming and debugging fast and easy • Rapid Application Development (RAD) 11

C# Translation and Execution • The C# compiler translates C# source code (. cs

C# Translation and Execution • The C# compiler translates C# source code (. cs files) into a special representation called Microsoft Intermediate Language (MSIL) • MSIL is not the machine language for any traditional CPU, but a virtual machine • The Common Language Runtime (CLR) then interprets the MSIL file • It uses a just-in-time compiler to translate from MSIL format to machine code on the fly 12

C# Compilation and Execution C# source code C# compiler MSIL Just in time compiler

C# Compilation and Execution C# source code C# compiler MSIL Just in time compiler Machine code 13

A Simple C# Program //============================= // // File: Hello. World. cs // // Classes:

A Simple C# Program //============================= // // File: Hello. World. cs // // Classes: Hello. World // ----------// This program prints a string called "Hello, World!” // //============================= using System; class Hello. World { static void Main(string[] args) { Console. Write. Line(“Hello, World!”); } } 14

C# Program Structure • Program specifications (optional) //============================= // // File: Hello. World. cs

C# Program Structure • Program specifications (optional) //============================= // // File: Hello. World. cs // // // Classes: Hello. World // ----------// This program prints a string called "Hello, World!” // //============================= • Library imports (optional) • using System; • Class and namespace definitions class Hello. World { static void Main(string[] args) { Console. Write. Line(“Hello, World!”); } } 15

White Space and Comments • White Space • • Includes spaces, newline characters, tabs,

White Space and Comments • White Space • • Includes spaces, newline characters, tabs, blanklines C# programs should be formatted to enhance readability, using consistent indentation! • Comments • Comments are ignored by the compiler: used only for human readers (i. e. , inline documentation) • Two types of comments • Single-line comments use //… // this comment runs to the end of the line • Multi-lines comments use /* … */ /* this comment runs to the terminating symbol, even across line breaks */ 16

Identifiers • Identifiers are the words that a programmer uses in a program •

Identifiers • Identifiers are the words that a programmer uses in a program • An identifier can be made up of letters, digits, and the underscore character They cannot begin with a digit C# is case sensitive, therefore args and Args are different identifiers Sometimes we choose identifiers ourselves when writing a program (such as Hello. World) using System; • • Sometimes we are using another programmer's code, so we use the identifiers that they chose (such as Write. Line) class Hello. World { static void Main(string[] args) { Console. Write. Line(“Hello World!”); } } 17

Identifiers: Keywords • Often we use special identifiers called keywords that already have a

Identifiers: Keywords • Often we use special identifiers called keywords that already have a predefined meaning in the language • Example: class • A keyword cannot be used in any other way All C# keywords are lowercase! 18

Namespaces • • Partition the name space to avoid name conflict! All. NET library

Namespaces • • Partition the name space to avoid name conflict! All. NET library code are organized using namespaces! By default, C# code is contained in the global namespace To refer to code within a namespace, must use qualified name (as in System. Console) or import explicitly (as in using System; ) using System; class Hello. World { static void Main(string[] args) { Console. Write. Line(“Hello World!”); } } class Hello. World { static void Main(string[] args) { System. Console. Write. Line (“Hello World!”); } } 19

C# Classes • Each class name is an identifier • Can contain letters, digits,

C# Classes • Each class name is an identifier • Can contain letters, digits, and underscores (_) • Cannot start with digits • Can start with the at symbol (@) • Convention: Class names are capitalized, with each additional English word capitalized as well (e. g. , My. First. Program ) • Class bodies start with a left brace ({) • Class bodies end with a right brace (}) 20

C# Program Structure: Method // comments about the class Hello. World { // comments

C# Program Structure: Method // comments about the class Hello. World { // comments about the method static void Main (string[] args) { } Console. Write(“Hello World!”); Console. Write. Line(“This is from COMP-315!”); } 21

C# Method and Statements • Methods – Building blocks of a program – The

C# Method and Statements • Methods – Building blocks of a program – The Main method • Each console or windows application must have exactly one (actually can have more, but it is unlikely that you will see or use) • All programs start by executing the Main method – Braces are used to start ({) and end (}) a method • Statements – Every statement must end in a semicolon ; 22

Syntax and Semantics • The syntax rules of a language define how we can

Syntax and Semantics • The syntax rules of a language define how we can put symbols, reserved words, and identifiers together to make a valid program • The semantics of a program statement define what that statement means (its purpose or role in a program) • A program that is syntactically correct is not necessarily logically (semantically) correct • A program will always do what we tell it to do, not what we meant to tell it to do 23

Errors • A program can have three types of errors • The compiler will

Errors • A program can have three types of errors • The compiler will find problems with syntax and other basic issues (compile-time errors) • If compile-time errors exist, an executable version of the program is not created • A problem can occur during program execution, such as trying to divide by zero, which causes a program to terminate abnormally (run-time errors) • A program may run, but produce incorrect results (logical errors) 24