Post on 11-Mar-2018
9/18/2017
EEE-425 Programming Languages (2013) 1
2
Computer programming: creating a sequence of instructions to enable the computer to do something
3
Programmers do not use machine language when creating computer programs. Instead, programmers tend to use high-level programming languages
Each high-level language has its own syntaxand limited set of vocabulary that is translated into machine code by a compiler
Define a task/problem
Plan your solution◦ Find suitable algorithm to solve it
◦ Find suitable data structures to use
Write code
Fix program error (bugs)
Make your customer happy
4
= Specification
= Design
= Implementation
= Testing & Debugging
= Deployment
Wikipedia.org definition for C#.◦ Object-oriented.
◦ Primarily imperative or procedural.
LINQ (Language Integrated Query) adds some functional programming language capabilities.
◦ Structured (as opposed to monolithic).
◦ Strongly typed.
◦ ISO(International Organization for Standardization)and ECMA(European Computer ManufacturersAssociation) standardized.
5
Programming language
◦ 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
6
9/18/2017
EEE-425 Programming Languages (2013) 2
7
Object-oriented programming is an extension of procedural programming, which in addition to variables and procedures contains: ◦ objects, ◦ classes, ◦ encapsulation, ◦ interfaces, ◦ polymorphism, and inheritance
Objects are object-oriented components
Attributes of an object represent its characteristics
A class is a category of objects or a type of object
An instance refers to an object based on a class
8
For example:◦ An Automobile is a class whose objects have the following
attributes: year, make, model, color, and current running status
◦ Your 1997 red Chevrolet is an instance of the class that is made up of all Automobiles
Methods of classes are used to change attributes and discover values of attributes◦ The Automobile class may have the following methods:
getGas(), accelerate(), applyBreaks()
9
Sample C# Console Application program:
using System;
namespace ConsoleApplication1{class HelloCSharp{
static void Main(string[] args){
Console.WriteLine("Hello, C#");}
}}
10
11
using System;
class HelloCSharp{
static void Main(){
Console.WriteLine("Hello, C#");}
}
Include the standard
namespace "System"
Define a class called
"HelloCSharp"
Define the Main() method – the
program entry point
Print a text on the console by
calling the method "WriteLine" of the class "Console"
12
using System;
class HelloCSharp{
static void Main(){
Console.WriteLine("Hello, C#");}
}
The { symbol should be alone on a new line.
The block after the {symbol should be
indented by a TAB.The } symbol should
be under the
corresponding {.
Class names should useHelloCsharpand start with a CAPITAL letter.
9/18/2017
EEE-425 Programming Languages (2013) 3
13
using System
;
class HelloCSharp {static
void Main( ) { Console. WriteLine ("Hello, C#" ) ;Console.
WriteLine ( "Hello again") ;}}
Such formatting makes the source code
unreadable.
using
◦ brings in namespaces
namespace
◦ disambiguation of names◦ like Internet hierarchical names and C++ naming
class
◦ like in C++ or Java◦ single inheritance up to object
static void Main()◦ Defines the entry point for an assembly.◦ Four different overloads – taking string
arguments and returning int’s. Console.WriteLine(Write)◦ Takes a formatted string: “Composite
Format”◦ Indexed elements: e.g., {0}
can be used multiple times only evaluated once
◦ {index [,alignment][:formatting]}
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
16
Operating System (OS)
Common Language Runtime (CLR)
Base Class Library (BCL)
ADO.NET, LINQ and XML (Data Tier)
WCF and WWF (Communication and Workflow Tier)
ASP.NETWeb Forms, MVC, AJAXMobile Internet Toolkit
WindowsForms
WPF Silverlight
C# C++ VB.NET J# F# JScript Perl Delphi …
Building blocks of .NET Framework
18
FCL
CLR
9/18/2017
EEE-425 Programming Languages (2013) 4
The common classes that are used in many programs◦ System.Console.WriteLine◦ XML, Networking, Filesystem, Crypto, containers◦ Can inherit from many of these classes
Many languages run on .NET framework◦ C#, C++, J#, Visual Basic◦ even have Python (see IronPython)
Common Language Runtime (CLR)◦ Managed execution environment
Executes .NET applications
Controls the execution process
◦ Automatic memory management (garbage collection)
◦ Programming languages integration
◦ Multiple versions support for assemblies
◦ Integrated type safety and security
21
CLR
Class Loader
MSIL to NativeCompilers (JIT)
CodeManager
GarbageCollector (GC)
Security Engine Debug Engine
Type Checker Exception Manager
Thread Support COM Marshaler
Base Class Library Support
From MSDN
CLR via C#, Jeffrey Richter
24
CLR is actually an implementation by Microsoft of the CLI (Common Language Infrastructure) .
CLI is an open specification.
CLR is really a platform specific implementation.
9/18/2017
EEE-425 Programming Languages (2013) 5
Framework Class Library (FCL)
◦ Provides basic functionality to developers:
Console applications
WPF(Windows Presentation Framework) and
Silverlight rich-media applications
Windows Forms GUI applications
Web applications (dynamic Web sites)
Web services, communication and workflow
Server & desktop applications
Applications for mobile devices
25 26
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
27
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!
28
29
1. File New Project ...2. Choose C# console application3. Choose project directory and name
30
9/18/2017
EEE-425 Programming Languages (2013) 6
4. Visual Studio creates some source code for you
31
Namespace not required
Class name should be
changed
Some imports are not required
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]
32
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]
33 34
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
35
Visual Studio has built-in debugger
It provides:◦ Breakpoints
◦ Ability to trace the code execution
◦ Ability to inspect variables at runtime
36
9/18/2017
EEE-425 Programming Languages (2013) 7
Live Demo-Examples
37 38
A computer program is a set of instructions that you write to tell a computer what to do
Procedural Programming involves creating computer memory locations, called variables, and a set of operations, called procedures. In object-oriented programming, you envision program components as objects that are similar to concrete objects in the real world
The C# language was developed as an object-oriented and component-oriented language
39
To write a C# program that produces a line of console output, you must pass a literal string as a parameter to the Console.Out.WriteLine() method
You can define a C# class or variable by using any name or identifier that begins with an underscore or a letter, that contains only letters or digits, and that is not a C# reserved keyword
To create a C# program, you can use the Microsoft Visual Studio environment or any text editor
40
After you write and save a program, you must compile the source code
Program comments are nonexecuting statements that add to document a program or to disable statements
As an alternative to using the command line, you can compile and write your program within the Visual Studio IDE
When you need to repeatedly use a class from the same namespace, you can shorten the statements you type by using a clause that indicates a namespace where the class can be found
Questions?
41
1. Create console application that prints your first and last name.
2. Write a program to print the numbers 1, 101 and 1001.
3. Create a console application that prints the current date and time.
4. Create a console application that calculates and prints the square
of the number 1234.
5. Write a program that prints the first 10 members of the sequence:
2, -3, 4, -5, 6, -7, ...
6. Write a program to read your age from the console and print how
old you will be after 10 years.
42