Introduction to Programming
Creating and Running Your First C# Program
Table of Contents
1. What is Computer Programming?
2. Your First C# Program
3. What is .NET Framework?
4. What is Visual Studio?
5. What is MSDN Library?
2
What is Computer Programming?
Define: Computer Programming
creating a sequence of instructions to
enable the computer to do something
Definition by Google
4
Programming Phases
Define a task/problem = Specification
Plan your solution
◦ Find suitable algorithm to solve it
◦ Find suitable data structures to use
Write code
= Design
Fix program error (bugs)
Make your customer happy
= Implementation
= Testing & Debugging
5
Your First C# Program
First Look at C#
Sample C# program:
using System;
class HelloCSharp
{
static void Main()
{
Console.WriteLine("Hello, C#");
}
}
7
C# Code – How It Works?
Include the standard Define a class called
namespace "System" "HelloCSharp"
using System; Define the Main()
class HelloCSharp
method – the
{ program entry point
static void Main()
{
Console.WriteLine("Hello, C#");
}
}
Print a text on the console by
calling the method "WriteLine"
of the class "Console"
8
C# Code Should Be Well Formatted
Class names should use PascalCase
and start with a CAPITAL letter.
using System;
class HelloCSharp
The { symbol should be
{ alone on a new line.
static void Main()
{
Console.WriteLine("Hello, C#");
}
}
The } symbol should The block after the
be under the { symbol should be
corresponding {. indented by a TAB.
9
Example of Bad Formatting
Such formatting
makes the source
using
code unreadable.
System
;
class HelloCSharp {
static
void Main( ) { Console
. WriteLine ("Hello, C#" )
;Console.
WriteLine ( "Hello again"
) ;}}
10
What is "C#“ ?
◦ A syntax that allow to give instructions to the computer
C# features:
◦ New cutting edge language
◦ Extremely powerful
◦ Easy to learn
◦ Easy to read and understand
◦ Object-oriented
11
What You Need to Program?
Knowledge of a programming language C# -
Task to solve
Development environment, compilers, SDK
◦ Visual Studio, .NET Framework SDK
Set of useful standard classes
◦ Microsoft .NET Framework FCL
Help documentation MSDN Library
12
Your First C# Program
What is .NET
Framework?
What is .NET Framework?
Environment for execution of .NET programs
Powerful library of classes
Programming model
Common execution engine for many programming languages
◦ C#
◦ Visual Basic .NET
◦ Managed C++
◦ ... and many others
15
Inside .NET Framework
Building blocks of .NET Framework
C# C++ VB.NET J# F# JScriptPerl Delphi …
ASP.NET Windows
Web Forms, MVC, AJAX WPF Silverlight
Mobile Internet Toolkit Forms
WCF and WWF (Communication and Workflow Tier) FCL
ADO.NET, LINQ and XML (Data Tier)
Base Class Library (BCL)
Common Language Runtime (CLR)
CLR
Operating System (OS)
16
CLR – The Heart of .NET Framework
Common Language Runtime (CLR)
◦ Managed execution environment
◦ Executes .NET applications CLR
◦ Controls the execution process
◦ Automatic memory management (garbage collection)
◦ Programming languages integration
◦ Multiple versions support for assemblies
◦ Integrated type safety and security
17
Framework Class Library
Framework Class Library (FCL) ◦ Web applications (dynamic Web sites)
◦ Provides basic functionality to ◦ Web services, communication and
developers: workflow
◦ Console applications ◦ Server & desktop applications
◦ WPF and Silverlight rich-media ◦ Applications for mobile devices
applications
◦ Windows Forms GUI applications
18
What is Visual Studio?
Visual Studio
Visual Studio – Integrated Development Environment (IDE)
Development tool that helps us to:
◦ Write code
◦ Design user interface
◦ Compile code
◦ Execute / test / debug applications
◦ Browse the help
◦ Manage project's files
20
Benefits of Visual Studio
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
Visual Studio – Example
22
Visual Studio
COMPILING, RUNNING AND DEBUGGING C#
PROGRAMS
Creating New Console Application
File New Project ...
Choose C# console application
24
Creating New Console Application (2)
Choose project directory and name
Visual Studio creates some source code for
you
25
Creating New Console Application (2)
Namespace
not required Some imports
are not required
Class name
should be
changed
26
Compiling Source Code
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 Build->Build Solution/Project
◦ Pressing [F6] or [Shift+Ctrl+B]
27
Running Programs
The process of running application includes:
◦ Compiling (if project not compiled)
◦ Starting the application
You can run application by:
◦ Using Debug->Start menu
◦ By pressing [F5] or [Ctrl+F5]
* NOTE: Not all types of projects are able to be started!
28
Debugging The Code
The process of debugging application 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
29
Debugging in Visual Studio
Visual Studio has built-in debugger
It provides:
◦ Breakpoints
◦ Ability to trace the code execution
◦ Ability to inspect variables at runtime
30
Visual Studio
COMPILING, RUNNING AND DEBUGGING C#
PROGRAMS
What is MSDN
Library?
What is MSDN Library?
Complete documentation of all classes and their functionality
◦ With descriptions of all methods, properties, events, etc.
◦ With code examples
Related articles
Library of samples
Use local copy or the Web version at http://msdn.microsoft.com/
33
MSDN Library
34
How to Use MSDN Library?
Offline version
◦ Use the table of contents
◦ Use the alphabetical index
◦ Search for phrase or keyword
◦ Filter by technology
◦ Browse your favorite articles
Online version
◦ Use the built-in search
35
MSDN Library
BROWSING AND SEARCHING DOCUMENTATION
Live Demo
Introduction to Programming
Questions?
Exercises
1. Familiarize yourself with:
◦ Microsoft Visual Studio
◦ Microsoft Developer Network (MSDN) Library Documentation
◦ Find information about Console.WriteLine() method.
2. Create, compile and run a “Hello C#” console application.
3. Modify the application to print your name.
4. Write a program to print the numbers 1, 101 and 1001.
38
Exercises (2)
5. Install at home:
1. Microsoft .NET Framework
2. Microsoft Visual Studio (or Visual C# Express)
3. Microsoft Developer Network (MSDN)
6. Create console application that prints your first and last name.
7. Create a console application that prints the current date and
time.
8. Create a console application that calculates and prints the square
of the number 12345.
39
Exercises (3)
9. Write a program that prints the first 10 members of
the sequence: 2, -3, 4, -5, 6, -7, ...
10.Provide a short list with information about the most
popular programming languages. How do they differ
from C#?
11.Describe the difference between C# and .NET
Framework.
12.* Write a program to read your age from the
console and print how old you will be after 10 years.
*NOTE: If you have any difficulties, search in Google. 40