37
1 Visual Programming Visual Programming C# C#

Visula C# Programming Lecture 1

Embed Size (px)

DESCRIPTION

Lecter 1

Citation preview

Page 1: Visula C# Programming Lecture 1

11

Visual ProgrammingVisual Programming

C#C#

Page 2: Visula C# Programming Lecture 1

22

SUNSUN

MICROSOFTMICROSOFT

Page 3: Visula C# Programming Lecture 1

33

Microsoft and Sun's ConfrontationMicrosoft and Sun's Confrontation It's newIt's new it's powerfulit's powerful it solves lots of problemsit solves lots of problems it is threatening to a vast community of Java it is threatening to a vast community of Java

worshippers, and best (or worst) of all it's from worshippers, and best (or worst) of all it's from Microsoft. You guessed it right;I am talking Microsoft. You guessed it right;I am talking about .NETabout .NET

Around 1995, Java was generating the same Around 1995, Java was generating the same Strength as is generated by .NET todayStrength as is generated by .NET today

!. Sun's Java introduced a revolutionary approach !. Sun's Java introduced a revolutionary approach of software developmentof software development

Java provided the programmer's with a unique tool Java provided the programmer's with a unique tool that solved their ages long problem of platform that solved their ages long problem of platform independence independence

Page 4: Visula C# Programming Lecture 1

44

Microsoft and Sun's ConfrontationMicrosoft and Sun's Confrontation

Sun addressed industry's needs by coming up with Sun addressed industry's needs by coming up with tools like JSP, Servlets, EJB, JMS, JNDI, JNI etc. tools like JSP, Servlets, EJB, JMS, JNDI, JNI etc. Sun stuffed these excellent tools under one Sun stuffed these excellent tools under one umbrella - the Java2 Enterprise Edition (umbrella - the Java2 Enterprise Edition (J2EE). J2EE). Microsoft was trying to catch up Microsoft was trying to catch up

All this time when Sun was winning our admiration; All this time when Sun was winning our admiration; Microsoft was trying their level best to match up to Microsoft was trying their level best to match up to Sun's tools. But Sun was un-catchableSun's tools. But Sun was un-catchable

Microsoft answer was Microsoft's Windows Microsoft answer was Microsoft's Windows DNA DNA ((Distributed interNet Applications Distributed interNet Applications ArchitectureArchitecture ) ) programming model programming model

Page 5: Visula C# Programming Lecture 1

55

Microsoft and Sun's ConfrontationMicrosoft and Sun's Confrontation

With the world depending more and more on With the world depending more and more on the Internet - and Sun's tools becoming the the Internet - and Sun's tools becoming the foremost media for the web applications foremost media for the web applications 

So Microsoft put their best men at work and So Microsoft put their best men at work and started calling the project as  Next started calling the project as  Next Generation Windows Services (Generation Windows Services (NGWSNGWS).).

Page 6: Visula C# Programming Lecture 1

66

What is .NET?What is .NET?

Microsoft defines .NET as “a set of Microsoft Microsoft defines .NET as “a set of Microsoft software technologies for connecting software technologies for connecting information, people, systems, and devices“information, people, systems, and devices“

However, Microsoft .NET is not just another However, Microsoft .NET is not just another programming languageprogramming language It’s a complete new business initiativeIt’s a complete new business initiative

Mission statement: Access to Information Mission statement: Access to Information anywhere, anytimeanywhere, anytime

Everything evolves around the InternetEverything evolves around the Internet

Page 7: Visula C# Programming Lecture 1

77

The .NET Framework objectives of The .NET The .NET Framework objectives of The .NET Framework Framework To provide a consistent object-oriented programming To provide a consistent object-oriented programming

environment whether object code is stored and executed environment whether object code is stored and executed locally, executed locally but Internet-distributed, or executed locally, executed locally but Internet-distributed, or executed remotely. remotely.

To provide a code-execution environment that minimizes To provide a code-execution environment that minimizes software deployment and versioning conflicts. software deployment and versioning conflicts.

To provide a code-execution environment that guarantees To provide a code-execution environment that guarantees safe execution of code, including code created by an safe execution of code, including code created by an unknown or semi-trusted third party. unknown or semi-trusted third party.

To provide a code-execution environment that eliminates To provide a code-execution environment that eliminates the performance problems of scripted or interpreted the performance problems of scripted or interpreted environments. environments.

To make the developer experience consistent across widely To make the developer experience consistent across widely varying types of applications, such as Windows-based varying types of applications, such as Windows-based applications and Web-based applications. applications and Web-based applications.

To build all communication on industry standards to ensure To build all communication on industry standards to ensure that code based on the .NET Framework can integrate with that code based on the .NET Framework can integrate with any other code. any other code.

Page 8: Visula C# Programming Lecture 1

88

Information Anywhere, AnytimeInformation Anywhere, Anytime

■ Server Server machines that machines that provide provide information or information or services services (database (database server, content server, content provider,…)provider,…)

Page 9: Visula C# Programming Lecture 1

99

Information Anywhere, AnytimeInformation Anywhere, Anytime

■ Sophisticated Sophisticated computing computing devices that devices that communicate communicate with PCs to with PCs to allow access to allow access to informationinformation

Page 10: Visula C# Programming Lecture 1

1010

Information Anywhere, AnytimeInformation Anywhere, Anytime

■ A standardized A standardized protocol for the protocol for the internet to allow internet to allow different different programs and programs and devices to talk to devices to talk to each othereach other

Page 11: Visula C# Programming Lecture 1

1111

Information Anywhere, AnytimeInformation Anywhere, Anytime

■ A set of A set of developer tools developer tools that allows you that allows you to write server to write server and client and client programs as programs as well as XML Web well as XML Web ServicesServices

Page 12: Visula C# Programming Lecture 1

1212

Personal OpinionPersonal Opinion

New programming languagesNew programming languages

new virtual machine new virtual machine

simply a great IDEsimply a great IDE

Page 13: Visula C# Programming Lecture 1

1313

Binary code

Conventional DevelopingConventional Developing

In the past, compiled code only runs on the computer it was compiled for

In the past, compiled code only runs on the computer it was compiled for

Source Source CodeCode

Binary Binary codecode

Operating System

Operating System

Source Code

Page 14: Visula C# Programming Lecture 1

1414

Intermediate code

Virtual Machine ConceptVirtual Machine Concept

Virtual Machine acting as the adaptor to the physical computerCompiled code will run on any computer as long as this virtual machine is installed

Virtual Machine acting as the adaptor to the physical computerCompiled code will run on any computer as long as this virtual machine is installed

Source Source CodeCode

Operating System

Operating System

Virtual Machine Virtual Machine

Intermediate code

Page 15: Visula C# Programming Lecture 1

1515

.NET Framework.NET Framework.NET Framework.NET Framework

Virtual Machine is called “Common Language Runtime” (CLR) or sometimes simply .NET Framework

Compiled code also called “Microsoft Intermediate Language” (MSIL)

Virtual Machine is called “Common Language Runtime” (CLR) or sometimes simply .NET Framework

Compiled code also called “Microsoft Intermediate Language” (MSIL)

Operating System

.NET Framework / Common Language Runtime (CLR)

Microsoft Intermediate

Language (MSIL)

Page 16: Visula C# Programming Lecture 1

1616

.NET Framework.NET Framework.NET Framework.NET Framework

Many .NET languages

C#, VB.NET

J#

C++ .NET

Cobol, Perl

...

Many .NET languages

C#, VB.NET

J#

C++ .NET

Cobol, Perl

... MSILMSIL

C# VB.NET J#

Page 17: Visula C# Programming Lecture 1

1717

.Net Languages.Net Languages

Unlike Java’s VM, different languages can Unlike Java’s VM, different languages can be used to compile into Intermediate be used to compile into Intermediate Language.Language.

C# - more like Java than C++C# - more like Java than C++ VB.NET – Visual Basic’s responseVB.NET – Visual Basic’s response J#, Cobol, Fortran, Scheme, Perl, ...J#, Cobol, Fortran, Scheme, Perl, ...

Page 18: Visula C# Programming Lecture 1

1818

Installing the .NET FrameworkInstalling the .NET Framework

.NET Framework installs only on Windows .NET Framework installs only on Windows 98 and above98 and above

It’s a 20 MB file named “dotnetfx.exe”It’s a 20 MB file named “dotnetfx.exe” Can also be foundCan also be found

http://windowsupdate.microsoft.com Comes pre-installed starting with Windows Comes pre-installed starting with Windows

Server 2003Server 2003

Page 19: Visula C# Programming Lecture 1

1919

.NET Framework Class Library.NET Framework Class Library

There are literally thousands of extremely There are literally thousands of extremely useful classes that cover very common useful classes that cover very common tasks:tasks: File operations, networking, cryptographyFile operations, networking, cryptography Database access, Windows-related tasksDatabase access, Windows-related tasks

One major key to .NET programming is One major key to .NET programming is actually to know the classes (instead of actually to know the classes (instead of wasting time writing them from scratch)wasting time writing them from scratch)

Page 20: Visula C# Programming Lecture 1

2020

NamespacesNamespaces

Every class belongs to a namespaceEvery class belongs to a namespace e.x., FileInfo belongs to the System.IO e.x., FileInfo belongs to the System.IO

namespacenamespace You can refer to it using System.IO.FileInfo or You can refer to it using System.IO.FileInfo or

use the “using” or “imports” keyword to “import” use the “using” or “imports” keyword to “import” entire namespacesentire namespaces

This allows different classes with the same This allows different classes with the same name to co-existname to co-exist

Page 21: Visula C# Programming Lecture 1

2121

What is "Common Type System" What is "Common Type System" (CTS)(CTS)

provide language interoperabilityprovide language interoperability language sharing the same data typeslanguage sharing the same data types intint should mean the same in VB, VC++, C# should mean the same in VB, VC++, C#

and all other and all other CTS, much like Java, defines every data CTS, much like Java, defines every data

type as a Classtype as a Class Every .NET compliant language must stick Every .NET compliant language must stick

to this definition to this definition

Page 22: Visula C# Programming Lecture 1

2222

Popular Project TypesPopular Project Types

Console applications. Console applications. Scripted or hosted applications. Scripted or hosted applications. Windows GUI applications (Windows Forms). Windows GUI applications (Windows Forms). ASP.NET applications. ASP.NET applications. XML Web services. XML Web services. Windows services. Windows services. Windows Forms ApplicationWindows Forms Application

.NET programs that run under the Microsoft Windows platform .NET programs that run under the Microsoft Windows platform (previously known as “Win32 Applications”)(previously known as “Win32 Applications”)

ASP.NET ApplicationsASP.NET Applications An entire website that is driven by Microsoft .NETAn entire website that is driven by Microsoft .NET

Smart ClientsSmart Clients Sophisticated computing devices that communicate with PCs to Sophisticated computing devices that communicate with PCs to

allow access to informationallow access to information PDAs, Laptops, Terminals, CellphonesPDAs, Laptops, Terminals, Cellphones

Page 23: Visula C# Programming Lecture 1

2323

.NET Development.NET Development

Operating System

Common Language Runtime

Base Class Library

ADO.NET and XML

ASP.NETWeb Forms Web Services

Mobile Internet Toolkit

WindowsForms

Common Language Specification

VB C++ C# J# …

Page 24: Visula C# Programming Lecture 1

2424

The .NET Framework: Common Language The .NET Framework: Common Language SpecificationSpecification Basic themes of .NET is unification and Basic themes of .NET is unification and

interoperability between various interoperability between various programming languagesprogramming languages To achieve this there must be some rules To achieve this there must be some rules

followed by all the languagesfollowed by all the languages CLS is the collection of the rules and CLS is the collection of the rules and

constraints that every language (that seeks constraints that every language (that seeks to achieve .NET compatibility) must follow to achieve .NET compatibility) must follow

Page 25: Visula C# Programming Lecture 1

2525

The .NET Framework: Common Language The .NET Framework: Common Language SpecificationSpecification Microsoft have defined three level of CLS Microsoft have defined three level of CLS

compatibility/compliance.compatibility/compliance. COMPLIANT PRODUCERCOMPLIANT PRODUCER

The component developed in this type of language can be used by The component developed in this type of language can be used by any other languageany other language

CONSUMERCONSUMER The language in this category can use classes produced in any The language in this category can use classes produced in any

other language other language EXTENDEREXTENDER

Languages in this category can not just use the classes as in Languages in this category can not just use the classes as in CONSUMER category; but can also extend classes using CONSUMER category; but can also extend classes using inheritance inheritance

Microsoft is providingMicrosoft is providing Visual BasicVisual Basic®®, C++, C#, J#, JScript, C++, C#, J#, JScript®®

Industry and academiaIndustry and academia APL, COBOL, Eiffel, Fortran, Haskell, ML, Perl, Python, RPG, Scheme, APL, COBOL, Eiffel, Fortran, Haskell, ML, Perl, Python, RPG, Scheme,

Smalltalk, …Smalltalk, …

Page 26: Visula C# Programming Lecture 1

2626

Common Language InfrastructureCommon Language Infrastructure

The following organizations have participated The following organizations have participated and their contributions are gratefully and their contributions are gratefully acknowledged:acknowledged:

Fujitsu Software, Hewlett-Packard, Intel Fujitsu Software, Hewlett-Packard, Intel Corporation, International Business Machines, ISE, Corporation, International Business Machines, ISE, Microsoft Corporation, Monash University, Microsoft Corporation, Monash University, Netscape, OpenWave, Plum Hall, Sun Netscape, OpenWave, Plum Hall, Sun Microsystems Microsystems

Page 27: Visula C# Programming Lecture 1

2727

.NET Development.NET Development

Operating System

Common Language Runtime

Base Class Library

ADO.NET and XML

ASP.NETWeb Forms Web Services

Mobile Internet Toolkit

WindowsForms

Common Language Specification

VB C++ C# J# …

Page 28: Visula C# Programming Lecture 1

2828

Common Language Runtime: Execution modelCommon Language Runtime: Execution model

VBSource code

Compiler

C++C#

CompilerCompiler

AssemblyIL Code

AssemblyIL Code

AssemblyIL Code

Operating System Services

Common Language Runtime

JIT Compiler

Native Code

Managedcode

UnmanagedComponent

Page 29: Visula C# Programming Lecture 1

2929

What is "Microsoft Intermediate What is "Microsoft Intermediate Language" (MSIL)Language" (MSIL)

A .NET programming language (C#, A .NET programming language (C#, VB.NET, J# etc.) does not compile into VB.NET, J# etc.) does not compile into executable codeexecutable code

compiles into an intermediate code called compiles into an intermediate code called Microsoft Intermediate Language (MSIL).Microsoft Intermediate Language (MSIL).

The MSIL code is then send to the CLR The MSIL code is then send to the CLR (Common Language Runtime) that converts (Common Language Runtime) that converts the code to machine language which is then the code to machine language which is then run on the host machinerun on the host machine

MSIL is similar to Java Byte code MSIL is similar to Java Byte code

Page 30: Visula C# Programming Lecture 1

3030

Common Language RuntimeCommon Language Runtime Every .NET Framework application relies on the Every .NET Framework application relies on the

CLRCLR Every .NET Framework assembly contains a Every .NET Framework assembly contains a

manifest:manifest: Class/Method Meta-DataClass/Method Meta-Data Versioning InformationVersioning Information

The common language runtime manages memory, The common language runtime manages memory, thread execution, code execution, code safety thread execution, code execution, code safety verification, compilation, and other system servicesverification, compilation, and other system services

The runtime is designed to enhance performanceThe runtime is designed to enhance performance The runtime also accelerates developer The runtime also accelerates developer

productivity productivity

Page 31: Visula C# Programming Lecture 1

3131

Inside a .NET Framework ApplicationInside a .NET Framework Application

During Execution, MSIL is JIT Compiled During Execution, MSIL is JIT Compiled Method-by-Method, and stored in the Global Method-by-Method, and stored in the Global Assembly Cache (machine-wide code cache Assembly Cache (machine-wide code cache ) on the Hard Drive) on the Hard Drive

The GAC knows the version information!The GAC knows the version information! Copy a new version, execute – and both Copy a new version, execute – and both

versions are still available in the cache.versions are still available in the cache.

App1.exe Ref: Widget.dll, ver 1.2.1.7App1.exe Ref: Widget.dll, ver 1.2.1.7 GACGACWidget.dll

Ver 1.2.1.7Widget.dllVer 1.2.1.7

Widget.dllVer 1.3.0.0Widget.dllVer 1.3.0.0App2.exe

Ref: Widget.dll, ver 1.3.0.0App2.exe Ref: Widget.dll, ver 1.3.0.0

Page 32: Visula C# Programming Lecture 1

3232

.NET Development.NET Development

Operating System

Common Language Runtime

Base Class Library

ADO.NET and XML

ASP.NETWeb Forms Web Services

Mobile Internet Toolkit

WindowsForms

Common Language Specification

VB C++ C# J# …

Page 33: Visula C# Programming Lecture 1

3333

Start Visual Studio.Net 2005Start Visual Studio.Net 2005Click on New Project ButtonClick on New Project Button

Page 34: Visula C# Programming Lecture 1

3434

. Starting Projects . Starting Projects

Creating a New ProjectCreating a New Project In the Visual Studio.NET environment, select File | In the Visual Studio.NET environment, select File |

New | Project from the menu. New | Project from the menu.

Page 35: Visula C# Programming Lecture 1

3535

• In left window, select “Visual Basic Projects”.• In right window, select “Console Application”• Name it What u want (Note: No Spaces!)• Click “Browse” button to select your Location.• Note location where project will be saved.• Click “OK” button to create the project.

Page 36: Visula C# Programming Lecture 1

3636

using System;using System;using System.Collections.Generic;using System.Collections.Generic;using System.Text;using System.Text;

namespace ConsoleApplication1namespace ConsoleApplication1{{ class Program class Program { { static void Main(string[] args) static void Main(string[] args) { { Console.WriteLine ("This is first Program"); Console.WriteLine ("This is first Program"); Console.ReadLine(); Console.ReadLine(); } } } }}}

Page 37: Visula C# Programming Lecture 1

3737

Console OutputConsole Output

Note the console window.

Press enter to exit that window.