39
Introduction to Introduction to Programming Programming Creating and Running Your First C# Program Creating and Running Your First C# Program Svetlin Nakov Svetlin Nakov Telerik Corporation Telerik Corporation www.telerik.com www.telerik.com

Introduction to Programming - WordPress.com · 2019-01-01 · Building blocks of .NET Framework FCL CLR 16. CLR ... Integrated type safety and security CLR 17. ... Provide a short

  • Upload
    others

  • View
    18

  • Download
    0

Embed Size (px)

Citation preview

Introduction to Introduction to ProgrammingProgramming

Creating and Running Your First C# ProgramCreating and Running Your First C# Program

Svetlin NakovSvetlin NakovTelerik CorporationTelerik Corporationwww.telerik.comwww.telerik.com

Table of ContentsTable of Contents

1.1. What is Computer Programming?What is Computer Programming?

2.2. Your First C# ProgramYour First C# Program

3.3. What is .NET Framework?What is .NET Framework?

4.4. What is Visual Studio?What is Visual Studio?

5.5. What is MSDN Library?What is MSDN Library?

2

What is Computer What is Computer Programming?Programming?

Define: Computer ProgrammingDefine: Computer Programming

Computer programmingComputer programming: creating a : creating a sequence of instructions to enable the sequence of instructions to enable the computer to do somethingcomputer to do something

Definition by GoogleDefinition by Google

4

Programming PhasesProgramming Phases

Define a task/problemDefine a task/problem Plan your solutionPlan your solution

Find suitable algorithm to solve itFind suitable algorithm to solve it

Find suitable data structures to useFind suitable data structures to use

Write codeWrite code Fix program error (bugs)Fix program error (bugs)

Make your customer happyMake your customer happy

= Specification= Specification

= Design= Design

= Implementation= Implementation

= Testing & Debugging= Testing & Debugging

= Deployment= Deployment

5

Your First C# ProgramYour First C# Program

First Look at C#First Look at C#

Sample C# program:Sample C# program:

using System;using System;

class HelloCSharpclass HelloCSharp{{ static void Main()static void Main() {{ Console.WriteLine("Hello, C#");Console.WriteLine("Hello, C#"); }}}}

77

C# Code – How It Works?C# Code – How It Works?

using System;using System;

class HelloCSharpclass HelloCSharp{{ static void Main()static void Main() {{ Console.WriteLine("Hello, C#");Console.WriteLine("Hello, C#"); }}}}

Include the standard Include the standard namespace "namespace "SystemSystem""

Define a class called Define a class called ""HelloCSharpHelloCSharp""

Define Define the the Main() Main() method – the method – the

program entry program entry pointpoint

Print a text on the console by Print a text on the console by calling the method calling the method

""WriteLineWriteLine" of the class " of the class ""ConsoleConsole""

8

C# Code Should Be Well C# Code Should Be Well FormattedFormatted

using System;using System;

class HelloCSharpclass HelloCSharp{{ static void Main()static void Main() {{ Console.WriteLine("Hello, C#");Console.WriteLine("Hello, C#"); }}}}

The The {{ symbol should symbol should be alone on a new be alone on a new

line.line.

The block after the The block after the {{ symbol should symbol should be indented by a be indented by a

TABTAB..

The The }} symbol symbol should be under the should be under the

corresponding corresponding {{..

Class names should use Class names should use PascalCasePascalCase and start with a and start with a

CAPITALCAPITAL letter. letter.

9

Example of Bad FormattingExample of Bad Formatting

using using SystemSystem ;;

class HelloCSharp {class HelloCSharp { static static

void Main( ) { Console void Main( ) { Console . WriteLine ("Hello, C#" ) . WriteLine ("Hello, C#" ) ;Console.;Console. WriteLine ( "Hello again"WriteLine ( "Hello again" ) ;}}) ;}}

Such formatting Such formatting makes the makes the

source code source code unreadable.unreadable.

10

What is "C#"?What is "C#"? Programming languageProgramming language

A syntax that allow to give instructions to the A syntax that allow to give instructions to the computercomputer

C# features:C# features:

New cutting edge languageNew cutting edge language

Extremely powerfulExtremely powerful

Easy to learnEasy to learn

Easy to read and understandEasy to read and understand

Object-orientedObject-oriented11

What You Need to Program?What You Need to Program? Knowledge of a programming languageKnowledge of a programming language

C#C#

Task to solveTask to solve Development environment, compilers, SDKDevelopment environment, compilers, SDK

Visual StudioVisual Studio, , .NET Framework SDK.NET Framework SDK

Set of useful standard classesSet of useful standard classes

Microsoft .NET Framework FCLMicrosoft .NET Framework FCL

Help documentationHelp documentation

MSDN LibraryMSDN Library12

Your First C# ProgramYour First C# ProgramLive DemoLive Demo

What is .NET What is .NET Framework?Framework?

What is .NET Framework?What is .NET Framework? Environment for execution of .NET programsEnvironment for execution of .NET programs Powerful library of classesPowerful library of classes Programming modelProgramming model Common execution engine for many Common execution engine for many

programming languagesprogramming languages

C#C#

Visual Basic .NETVisual Basic .NET

Managed C++Managed C++

... and many others... and many others15

Operating System (OS)Operating System (OS)

Common Language Runtime (CLR)Common Language Runtime (CLR)

Base Class Library (BCL)Base Class Library (BCL)

ADO.NET, LINQ and XML (Data Tier)ADO.NET, LINQ and XML (Data Tier)

WCF and WWF (Communication and Workflow Tier)WCF and WWF (Communication and Workflow Tier)

ASP.NETASP.NETWeb Forms, MVC, AJAXWeb Forms, MVC, AJAXMobile Internet ToolkitMobile Internet Toolkit

WindowsWindowsFormsForms WPFWPF SilverlightSilverlight

C#C# C++C++ VB.NETVB.NET J#J# F#F# JScriptJScript PerlPerl DelphiDelphi ……

Inside .NET FrameworkInside .NET Framework Building blocks of .NET FrameworkBuilding blocks of .NET Framework

FCLFCL

CLRCLR

16

CLR – The Heart of .NET CLR – The Heart of .NET FrameworkFramework

Common Language Runtime (CLR)Common Language Runtime (CLR) Managed execution environmentManaged execution environment

Executes .NET applicationsExecutes .NET applications

Controls the execution processControls the execution process

Automatic memory managementAutomatic memory management ( (garbage garbage collectioncollection))

Programming languages integrationProgramming languages integration

Multiple versions support for assembliesMultiple versions support for assemblies

Integrated type safety and securityIntegrated type safety and security

CLRCLR

17

Framework Class LibraryFramework Class Library

Framework Class Library (FCL)Framework Class Library (FCL) Provides basic functionality to developers:Provides basic functionality to developers:

Console applicationsConsole applications

WPF and Silverlight rich-media applicationsWPF and Silverlight rich-media applications

Windows Forms GUI applicationsWindows Forms GUI applications

Web applications (dynamic Web sites)Web applications (dynamic Web sites)

Web servicesWeb services, , communication and workflowcommunication and workflow

Server & desktop applicationsServer & desktop applications

Applications for mobile devicesApplications for mobile devices18

What is Visual Studio?What is Visual Studio?

Visual StudioVisual Studio Visual Studio – Integrated Development Visual Studio – Integrated Development

Environment (IDE)Environment (IDE) Development tool that helps us to:Development tool that helps us to:

Write codeWrite code

Design user interfaceDesign user interface

Compile codeCompile code

Execute / test / debug applicationsExecute / test / debug applications

Browse the helpBrowse the help

Manage project's filesManage project's files20

Benefits of Visual StudioBenefits of Visual Studio Single tool for:Single tool for:

Writing code in many languages (C#, VB, …)Writing code in many languages (C#, VB, …)

Using different technologies (Web, WPF, …)Using different technologies (Web, WPF, …)

For different platforms (.NET CF, Silverlight, …)For different platforms (.NET CF, Silverlight, …)

Full integration of most development activities Full integration of most development activities (coding, compiling, testing, debugging, (coding, compiling, testing, debugging, deployment, version control, ...)deployment, version control, ...)

Very easy to use!Very easy to use!

21

Visual Studio – ExampleVisual Studio – Example

22

Visual StudioVisual StudioCompiling, Running and Debugging C# Programs Compiling, Running and Debugging C# Programs

Creating New Console ApplicationCreating New Console Application

s File File New New Project ... Project ...

s Choose C# console applicationChoose C# console application

e Choose project directory and nameChoose project directory and name

24

Creating New Console Application (2)Creating New Console Application (2)

1.1. Visual Studio creates some source code for youVisual Studio creates some source code for you

Namespace Namespace not requirednot required

Class name Class name should be should be changedchanged

Some imports Some imports are not requiredare not required

25

Compiling Source CodeCompiling Source Code The process of The process of compilingcompiling includes: includes:

Syntactic checksSyntactic checks

Type safety checksType safety checks

Translation of the source code to lower level Translation of the source code to lower level language (MSIL)language (MSIL)

Creating of executable files (assemblies)Creating of executable files (assemblies)

You can start compilation byYou can start compilation by

Using Using Build->Build Solution/ProjectBuild->Build Solution/Project

PressingPressing [[F6]F6] oror [Shift+Ctrl+B][Shift+Ctrl+B]26

Running ProgramsRunning Programs The process of The process of runningrunning application includes: application includes:

Compiling (if project not compiled)Compiling (if project not compiled)

Starting the applicationStarting the application

You can run application by:You can run application by:

Using Using Debug->Start Debug->Start menumenu

By pressing By pressing [F5][F5] or or [Ctrl+F5][Ctrl+F5]

* NOTE: Not all types of projects are able to be * NOTE: Not all types of projects are able to be started!started!

27

Debugging The CodeDebugging The Code The process of The process of debuggingdebugging

application includes:application includes:

Spotting an errorSpotting an error

Finding the lines of code that Finding the lines of code that cause the errorcause the error

Fixing the codeFixing the code

Testing to check if the error is Testing to check if the error is gone and no errors are introducedgone and no errors are introduced

Iterative and continuous processIterative and continuous process28

Debugging in Visual StudioDebugging in Visual Studio

Visual Studio has built-in debuggerVisual Studio has built-in debugger It provides:It provides:

BreakpointsBreakpoints

Ability to trace the code executionAbility to trace the code execution

Ability to inspect variables at runtimeAbility to inspect variables at runtime

29

Visual StudioVisual StudioCompiling, Running and Debugging C# Programs Compiling, Running and Debugging C# Programs

LiveLive DemoDemo

What is MSDN What is MSDN Library?Library?

What is MSDN Library?What is MSDN Library? Complete documentation of all classes and Complete documentation of all classes and

their functionalitytheir functionality

With descriptions of all methods, properties, With descriptions of all methods, properties, events, etc.events, etc.

With code examplesWith code examples

Related articlesRelated articles Library of samplesLibrary of samples Use local copy or the Web version at Use local copy or the Web version at http://http://

msdn.microsoft.com/msdn.microsoft.com/32

MSDN LibraryMSDN Library

33

How to Use MSDN Library?How to Use MSDN Library? Offline versionOffline version

Use the table of contentsUse the table of contents

Use the alphabetical indexUse the alphabetical index

Search for phrase or keywordSearch for phrase or keyword

Filter by technologyFilter by technology

Browse your favorite articlesBrowse your favorite articles

Online versionOnline version

Use the built-in searchUse the built-in search34

MSDN LibraryMSDN LibraryBrowsing and Searching DocumentationBrowsing and Searching Documentation

Live DemoLive Demo

Introduction to ProgrammingIntroduction to Programming

Questions?Questions?

http://academy.telerik.com

ExercisesExercises

1.1. Familiarize yourself with:Familiarize yourself with:

Microsoft Visual StudioMicrosoft Visual Studio

Microsoft Developer Network (MSDN) Library Microsoft Developer Network (MSDN) Library DocumentationDocumentation

Find information about Find information about Console.WriteLine()Console.WriteLine() method. method.

2.2. Create, compile and run a “Hello C#” console Create, compile and run a “Hello C#” console application.application.

3.3. Modify the application to print your name.Modify the application to print your name.

4.4. Write a program to print the numbers 1, 101 and Write a program to print the numbers 1, 101 and 1001.1001.

37

Exercises (2)Exercises (2)

1.1. Install at home:Install at home:

1.1. Microsoft .NET Framework Microsoft .NET Framework

2.2. Microsoft Visual Studio (or Visual C# Express)Microsoft Visual Studio (or Visual C# Express)

3.3. Microsoft Developer Network (MSDN)Microsoft Developer Network (MSDN)

2.2. Create console application that prints your first and Create console application that prints your first and last name.last name.

3.3. Create a console application that prints the current Create a console application that prints the current date and time.date and time.

4.4. Create a console application that calculates and Create a console application that calculates and prints the square of the number 12345.prints the square of the number 12345.

38

Exercises (3)Exercises (3)

1.1. Write a program that prints the first 10 members of Write a program that prints the first 10 members of the sequence: 2, -3, 4, -5, 6, -7, ...the sequence: 2, -3, 4, -5, 6, -7, ...

2.2. Provide a short list with information about the most Provide a short list with information about the most popular programming languages. How do they differ popular programming languages. How do they differ from C#?from C#?

3.3. Describe the difference between C# and .NET Describe the difference between C# and .NET Framework.Framework.

4.4. * Write a program to read your age from the console * Write a program to read your age from the console and print how old you will be after 10 years.and print how old you will be after 10 years.

*NOTE: If you have any difficulties, search in Google.*NOTE: If you have any difficulties, search in Google.39