Post on 26-Jun-2018
10/9/2012
1
dCreating and Running Your First C# Program
Text Book : C# Programming From Problem Analysis to Program design, Barbara Doyle
Grading : Homeworks 20%
Lecture Presentation 20%◦ Lecture Presentation 20%◦ Final : % 20◦ Project : 40%
2
1. What is Computer Programming?2. Your First C# Program3. What is .NET Framework?4. What is Visual Studio?
Wh i MSDN Lib ?5. What is MSDN Library?
3
Computer programming: creating a sequence of instructions to enable the computer to do
hisomething
5
Definition by GoogleDefinition by Google
Define a task/problem Plan your solution◦ Find suitable algorithm to solve it◦ Find suitable data structures to use
Write codeFi (b )
= = SpecificationSpecification= = DesignDesign
Fix program error (bugs)
Make your customer happy
6
= Implementation= Implementation
= Testing & Debugging= Testing & Debugging= Deployment= Deployment
10/9/2012
2
Sample C# program:using System;
class HelloCSharp{
static void Main()static void Main(){
Console.WriteLine("Hello, C#");}
}
8
using System;using System;
class HelloCSharpclass HelloCSharp{{
Include the Include the standard standard
namespace namespace ""SystemSystem""
Define a class Define a class called called
""HelloCSharpHelloCSharp""Define Define the the Main() Main()
method method –– the the program entryprogram entry
9
{{static void Main()static void Main(){{
Console.WriteLine("Hello, C#");Console.WriteLine("Hello, C#");}}
}}
program entry program entry pointpoint
Print a text on the console Print a text on the console by calling the method by calling the method
""WriteLineWriteLine" of the class " of the class ""ConsoleConsole""
using System;using System;
class HelloCSharpclass HelloCSharp{{
The The {{ symbol symbol should be should be alone on alone on
Class names should Class names should useusePascalCasePascalCase and start and start with with a a
CAPITALCAPITAL letter.letter.
10
{{static void Main()static void Main(){{
Console.WriteLine("Hello, C#");Console.WriteLine("Hello, C#");}}
}}
a new line.a new line.
The block after The block after the the {{ symbol symbol
should be should be indented by a indented by a
TABTAB
The The }} symbol symbol should be under should be under
the corresponding the corresponding {{..
using using SystemSystem
Such Such formatting formatting makes the makes the
source code source code unreadable.unreadable.
11
yy;;
classclass HelloCSharpHelloCSharp {{static static
void void Main(Main( )) {{ ConsoleConsole.. WriteLineWriteLine ("Hello, C#"("Hello, C#" )) ;;Console.Console.
WriteLineWriteLine (( "Hello"Hello againagain"")) ;};}}}
Programming language◦ A syntax that allow to give instructions to the
computer C# features:◦ New cutting edge language◦ New cutting edge language◦ Extremely powerful◦ Easy to learn◦ Easy to read and understand◦ Object-oriented
12
10/9/2012
3
Live Demo-Example 01-01, 01-02
Environment for execution of .NET programs Powerful library of classes Programming model Common execution engine for many
i lprogramming languages◦ C#◦ Visual Basic .NET◦ Managed C++◦ ... and many others
15
ASP.NETASP.NETWeb Web Forms, MVC, AJAXForms, MVC, AJAXMobile Internet ToolkitMobile Internet Toolkit
WindowsWindowsFormsForms
WPFWPF SilverlightSilverlight
C#C# C++C++ VB.NETVB.NET J#J# F#F# JScriptJScript PerlPerl DelphiDelphi ……
Building blocks of .NET Framework
Operating Operating System (OS)System (OS)
Common Language Common Language Runtime (CLR)Runtime (CLR)
Base Class Base Class Library (BCL)Library (BCL)
ADO.NET, LINQ ADO.NET, LINQ and and XML (Data Tier)XML (Data Tier)
WCF and WWF (Communication and Workflow Tier)WCF and WWF (Communication and Workflow Tier)
Mobile Internet ToolkitMobile Internet Toolkit
16
FCLFCL
CLRCLR
Common Language Runtime (CLR)◦ Managed execution environment Executes .NET applications Controls the execution process
◦ Automatic memory management (garbageCLRCLR
Automatic memory management (garbage collection)◦ Programming languages integration◦ Multiple versions support for assemblies◦ Integrated type safety and security
17
Framework Class Library (FCL)◦ Provides basic functionality to developers: Console applications WPF and Silverlight rich-media applicationsg pp Windows Forms GUI applications Web applications (dynamic Web sites) Web services, communication and workflow Server & desktop applications Applications for mobile devices
18
10/9/2012
4
Visual Studio – Integrated Development Environment (IDE)
Development tool that helps us to:◦ Write code◦ Design user interface◦ Design user interface◦ Compile code◦ Execute / test / debug applications◦ Browse the help◦ Manage project's files
20
Single tool for:◦ Writing code in many languages (C#, VB, …)◦ Using different technologies (Web, WPF, …)◦ For different platforms (.NET CF, Silverlight, …)
Full integration of most development activities (coding, compiling, testing, debugging, deployment, version control, ...)
Very easy to use!
21 22
Compiling, Running and Debugging C# Programs
1. File New Project ...2. Choose C# console application3. Choose project directory and name
24
10/9/2012
5
4. Visual Studio creates some source code for youNamespace Namespace not not
requiredrequired
Some imports Some imports are not are not
requiredrequired
25
Class Class name name
should be should be changedchanged
The process of compiling includes:◦ Syntactic checks◦ Type safety checks◦ Translation of the source code to lower level
language (MSIL)◦ Creating of executable files (assemblies)
You can start compilation by◦ Using BuildBuild-->Build Solution/Project>Build Solution/Project◦ Pressing [[F6]F6] or [Shift+Ctrl+B[Shift+Ctrl+B]]
26
The process of running application includes:◦ Compiling (if project not compiled)◦ Starting the application
You can run application by:pp y◦ Using DebugDebug-->Start>Start menu◦ By pressing [F5][F5] or [Ctrl+F5][Ctrl+F5]
* NOTE: Not all types of projects are able to be started!
27
The process of debuggingapplication includes:◦ Spotting an error◦ Finding the lines of code that cause the
error◦ Fixing the code◦ Testing to check if the error is gone
and no errors are introduced Iterative and continuous process
28
Visual Studio has built-in debugger It provides:◦ Breakpoints◦ Ability to trace the code execution◦ Ability to inspect variables at runtimeAbility to inspect variables at runtime
29
10/9/2012
6
Complete documentation of all classes and their functionality◦ With descriptions of all methods, properties, events,
etc.◦ With code examplesp
Related articles Library of samples Use local copy or the Web version at
http://http://msdn.microsoft.com/msdn.microsoft.com/
31
Questions?Questions?
1. Create, compile and run a “Hello C#” console application.
2. Modify the application to print your name.3. Write a program to print the numbers 1,
101 and 1001.4. Create console application that prints your
first and last name.
33
6. Create a console application that prints the current date and time.
7. Create a console application that calculates and prints the square of the number 12345.
8. Write a program that prints the first 10 members of the sequence: 2 3 4 5 6members of the sequence: 2, -3, 4, -5, 6, -7, ...
9. Write a program to read your age from the console and print how old you will be after 10 years.
34