Introduction To
Introduction To
UIIT
PMAS Arid Agriculture University, Rawalpindi
2
Distribution - Tentative
• Grades breakdown: according to UIIT rules
• Assignments and Quizzes
• Midterm and Final
• Project details will be out at the halfway mark
‒ Groups of 3-4
‒ Propose a project idea, write a brief specification
‒ Implement it and present to the class
3
Programming
4
Programming
• Telling a computer how to do certain things by giving it
instructions.
• Two main steps:
‒ Algorithm design
‒ Coding
• Requirements:
‒ A machine
‒ A set of tools
‒ A compiler
‒ And of course, a programming language
5
Programming language
• Written language that tells the computer what to do in
order to solve a problem.
• Programming paradigms:
‒ Imperative: Programmer instructs how to change the state
‒ Declarative: Programmer tells only the required results but not how to
compute it.
‒ Object-Oriented: Based on concept of objects which may contain data (as
fields or attributes) and code (as methods).
Objects can communicate with each other.
C# provides full support for object-oriented programming.
6
Algorithms and Data Structures
• Algorithm: a procedure for solving a problem or computing
something.
– sorting: putting data in ordered form
– searching: finding data in some kind of index
– finding primes and generating random numbers
– graphics: drawing lines, arcs, and other geometric objects
• Data structure: a way to store data.
– arrays
– linked lists
– Stacks
Data Structures organize the data and algorithm use that
organization.
7
Introduction to .Net
Framework
8
Overview
• The .NET Framework is a technology that supports building
and running the next generation of apps on the internet, on
the desktop and on mobile devices.
‒ To provide a consistent object-oriented programming environment whether:
◦ object code is stored and executed locally
◦ executed locally but Internet-distributed, or
◦ executed remotely
9
Influences
• .Net is the result of many influences.
10
Multi-language framework
• .Net not only supports C# but it also supports VB, C++, J#.
Development tools
and environment
App.exe
11
.Net is cross platform
• Compiled .Net app can run on any supported platform
App.exe
12
.Net is cross-platform
• Cross-platform execution realized in two ways:
– Apps are written against Framework Class Library (FCL), not
underlying OS
– Compilers generate generic assembly language which must be
executed by the Common Language Runtime (CLR)
• Framework Class Library
‒ 1000's of predefined classes
‒ common subset across all platforms & languages
‒ networking, database access, XML processing, GUI, Web, etc.
‒ Goal? FCL is a portable
13
.Net architecture
VB VC++ VC# JScript … Common Language
Specification
Visual Studio.NET
ASP.NET: Web Windows
Services and Web Forms
Forms
ADO.NET: Data and XML
Base Class Library
14
Base Class Library (BCL)
• Has classes for IO, threading, database, text, graphics,
console, sockets/web/mail, security, cryptography, COM,
run-time type discovery/invocation, assembly generation.
• Similar to Java’s System namespace.
15
Base Class Library (BCL)
16
Common Language Runtime (CLR)
• The common language runtime (CLR) is the engine that runs
and “manages” executing code.
17
CLR – Compilation and Verification
• It is responsible for :
– Building Managed Code: The Common Type System (CTS)
– Compiling Managed Code: MSIL and Metadata
– Organizing Managed Code: Assemblies
– Executing Managed Code : JIT compilation
18
CLR – Common Type System (CTS)
• Common Type System (CTS) is a standard that specifies
how type definitions and specific values of types are
represented in computer memory (e.g. short and int in C# maps
to Int16 and Int32 in .Net).
19
CLR – MSIL and Metadata
• Managed code is the code written using .Net.
• When a managed code is compiled, two things are produced:
– Code in Microsoft Intermediate Language (MSIL).
– Metadata, information about those instructions and the data they
manipulate.
20
CLR – MSIL and Metadata
• Microsoft Intermediate Language (MSIL) is a CPU-
independent set of instructions that can be efficiently
converted to the native code.
21
CLR - Assemblies
• An assembly or an executable or a DLL compiled using one of
the .Net Framework’s many language compilers.
22
CLR - JIT
• All the Microsoft .NET languages are converted to CPU
independent intermediate language (not the machine language)
by compiler.
23
CLR - JIT
• When a method is JIT compiled, it’s also check for type
safety, This process, called verification.
24
CLR - JIT
25
Types of JIT Compilers
26
Types of JIT Compilers
• In Microsoft .NET there are three types of JIT compilers:
– Pre-JIT :- compiles complete source code into native code in a
single compilation cycle. This is done at the time of
deployment of the application.
– Econo-JIT :- compiles only those methods that are called at
runtime. However, these compiled methods are removed
when they are not required.
27
Types of JIT Compilers
‒ Normal-JIT :- compiles only those methods that are called at
runtime. These methods are compiled the first time they are
called, and then they are stored in cache. When the same
methods are called again, the compiled code from cache is used
for execution.
28
CLR- Security
• When compiling MSIL to native code, code must pass a
verification process.
• Verification examines MSIL and metadata to find out
whether the code is type safe.
• The CLR implements two different types of security for
assemblies: code access and role-based security
29
CLR- Security
30
Memory Management
• Program Objects/Data occupy memory
• How does the runtime system efficiently create and
recycle memory on behalf of the program?
31
Dynamic memory allocation and
reclamation
• Heap contains dynamically allocated objects
• Object allocation: malloc, new
• Deallocation:
– Manual/explicit: free, delete
– Automatic: garbage collection
32
Dynamic memory allocation and
reclamation
33
Explicit Memory Management
• More code to maintain
• Efficiency can be very high
• Gives programmers “control”
34
Garbage Collection - Automatic memory
management
• Reduces programmer burden
• Eliminates sources of errors
• Integral to modern object-oriented languages, i.e., Java,
C#, VB.Net etc.
• Challenge:
‒ performance efficiency
35
Key Issues For Memory Management
• For both (Automatic And Explicit)
– Fast allocation
– Fast reclamation
– Low fragmentation (wasted space)
– How to organize the memory space
• Garbage Collection
– Discriminating live objects and garbage
36
CLR- Memory Management
• Objects are automatically freed from the managed heap
when they are no longer required by the application.
‒ Garbage Collection
37
CLR- Memory Management
• Garbage Collection
– Every instance of a reference type is allocated on the heap. As
the application runs, the memory allotted to the heap fills up.
Before new instances can be created, more space must be
made available. The process of doing this is called garbage
collection.
– When CLR notices that the heap is full, it will automatically run
the garbage collector.
– Garbage objects can appear anywhere in the heap.
– Garbage collection can reposition the contents of the heap.
– The garbage collection view objects in generations.
38
GC- Generations
• Is a feature of GC that enhances its performance
• There are 3 Generations.
• Generations 0:
– When an object is initialized, its in generation 0. These are new
objects that have never been played around by the GC.
– As and when more objects get created, the process of Garbage
Collection is invoked by the CLR.
39
GC- Generations
• Generation 1:
– The objects that survives the garbage collection process are
considered to be in generation 1.
– These are the old objects
• Generation 2:
– As more new objects get created and added to the memory,
the new objects are added to generation 0, generation 1 old
objects become older, and so are considered to be in
generation 2.
40
GC- Generations
• Generation 2 is the highest level generation in the
garbage collection process.
• Any further garbage collection process occurring causes
the level 1 objects promoted to level 2, and the level 2
objects stay in level 2 itself, as this level is the highest.
• This process helps in categorizing the memory
heap as to where the de-allocation needs to be done
first and where next.
41
GC- Generations
42
GC- Techniques
• Reference Counting
• Tracing
‒ Mark-sweep
‒ Mark-compact
43
GC: Reference Counting
• A Reference Count for each object
‒ No of incoming pointers
‒ Incremental
‒ Count = 0 (unreachable, considered garbage)
• Concerns
‒ Space for reference count
‒ Efficiency (Reference count adjustments)
44
GC: Mark-Sweep
• How it works
‒ Tracing to “mark” live objects
‒ Sweep to reclaim dead or “unmarked ”objects
‒ Marked status set to Unmarked for subsequent invocations of
algorithms
• Concerns
‒ Fragmentation
‒ Cost proportional to heap size
‒ The normal program execution may get suspended while this
algorithm runs.
45
GC: Mark-Compact
• How it works
‒ Tracing to “mark” live (reachable) objects
‒ Compacts the heap by moving all live objects into contiguous
memory locations
‒ The memory left unused after compaction is recycled
• Concerns
‒ Fragmentation solved
‒ Cost - Multiple scans of live objects
46
Xamarin
47
Xamarin
• Bringing open source .NET to mobile development, enabling every
developer to build truly native apps for any device in C# and F#.
• Providing native app for Apple iOS, Google Android, and Microsoft
Windows smartphones forces you to write three different apps using
three different languages.
• Building app using single programming language (C# or F#) using .net
framework.
48