0% found this document useful (0 votes)
78 views4 pages

Csharp

The document discusses several topics related to C# programming including: - The history and evolution of programming languages leading up to C#. - How C# code interacts with the .NET runtime environment and other languages. - Key concepts of C# like memory management using the garbage collector, threads, exceptions, and type systems. - Specific features of C# like value types vs. reference types, boxing and unboxing, and the role of types in ensuring program correctness.

Uploaded by

Supriya Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views4 pages

Csharp

The document discusses several topics related to C# programming including: - The history and evolution of programming languages leading up to C#. - How C# code interacts with the .NET runtime environment and other languages. - Key concepts of C# like memory management using the garbage collector, threads, exceptions, and type systems. - Specific features of C# like value types vs. reference types, boxing and unboxing, and the role of types in ensuring program correctness.

Uploaded by

Supriya Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C# Genealogy

Whats New In C#

Eiffel

Fortran

Algol 60

C++

C#

Cobol

PL/I

Pascal

Ada 95

Java

Ada 83
Spring 2003

CS340

C# And .Net

The Simple Stuff

o Languages like C# are not isolated


entities
o They interoperate in two ways:

o Most of C# is pretty similar to


languages you are used to:

n By being part of a system written in


more than one language
n By accessing services and operating on a
distributed environment

n Declarations
n Expressions
n Assignment and control statements

o Other elements are quite similar:

o Requires support from run time:

n Classes
n Functions

n .Net and the Common Language Runtime


Spring 2003

CS340

Major Topics To Discuss


o
o
o
o
o
o
o

CS340

CS340

Memory Layout

Memory system and pointers


Execution time environment
Threads
Exceptions
Type system
Identifier scope system
Interfaces

Spring 2003

Spring 2003

Heap

Stack

Pointers
f()

g() h() k()

Function Call & Return

Spring 2003

Garbage Collector

CS340

C# Memory Management

C# Memory Management

o Static vs. dynamic


o Dynamic storagestack and heap
o Stack (Dynamic):

o Allocation using new


o Deallocation by Garbage Collection
o Garbage collection:

n Managed algorithmically by
implementation of function calls

o Heap (Dynamic)
n Mostly managed by system
n Provision for management by programmer
Spring 2003

CS340

n Track objects that are accessible


n Free storage associated with objects that
are inaccessible
n Garbage collector is a system provided
service that runs periodically
n Deals with fragmentation
Spring 2003

CS340

Garbage Collector Pros & Cons

Some Specifics of C#

o Pros:

o Object destruction via Object.Finalize:

n Programmer does not have to implement


n Memory management done right

o Cons:
n No guarantee when it runs, hence no
control
n Takes processor resources
n Does not delete storage if it is still
reachable even if you dont want it
n Memory leaks can (and do) still occur
Spring 2003

CS340

o Pointersyes, they are provided:


n Syntax like C++, code marked unsafe
n Objects managed by GC or userpinned
9

Loader
CS340

CS340

10

Binary
Program

11

Source
Program

Interpreter

TARGET
Run-time Support
Spring 2003

MSIL
Program

Compiler

JiT Compiler

TARGET
Run-time Support
CS340

TARGET
Run-time Support

Machine Instructions For Multiple Targets

Spring 2003

Object
Program

Machine Instructions For Specific Target

Object Code
Libraries
Linkage
Editor

Spring 2003

More Flexible Compilation

Compiler

Object
Program

o Garbage collector available via GC


class:
n Runs via separate thread
n Various methods available for access

Traditional Compilation
Source
Program

n Inherited from Object type


n Override to destroy object as desired

12

C# Threads

o Threads vs. processes/tasks


o C# supports threads

o System.Threading namespace
o Facilities include:

Thread

Who is
running
and when?

Communication
Thread

Thread

Thread

What exactly are the problems here?

Concurrency

n Thread creation, destruction


n Child thread management, e.g. join()
n Thread scheduling, priority, timing

o Synchronization:
n Monitors
n Semphores (mutex class)
n Lockserialization of statement block

Data
Spring 2003

CS340

13

Spring 2003

CS340

14

Exceptions

One Thing Is For Sure

o Why do we need exceptions?

o Exceptions are NOT for dealing with


errors
o They are a mechanism for changing
the flow of control from sequential to
a branch if certain conditions exist
o They always indicate expected
circumstances. Otherwise they could
not possibly be generated

o How should they be made available in


programming languages?
o What benefits do they provide?
o What problems could they cause for
us?
Spring 2003

CS340

15

Exceptions in C#

Spring 2003

o Type should be consistent:


n Predefined and user-defined

o All C# types derive from System.Object


o Single rooted hierarchy
o Provides four standard methods:
n
n
n
n

CS340

16

Type System

o Throw raises an exception


o Catch defines a block that handles
the exception
o Etc.

Spring 2003

CS340

17

bool Equals
int GetHashCode
Type GetType
String ToString

Spring 2003

CS340

These dont
necessarily
mean what
you think

18

Types Of Types

Value vs. Reference

o Value types and reference types


o Value types:

o Note the special status of primitive types


o System.Int32 myInt = 42;
System.String myStr = Hello World;
Circle c;
c = new Circle(...);

n Program variables have a value


n Space allocated on stack
n
n
n
n

Program variable is just a reference


Allocated space on stack
Reference is a type-safe pointer
Data space allocated on heap

Spring 2003

CS340

19

Boxing And Unboxing

Boxed
myInt
object o
Spring 2003

myStr

address

address

Spring 2003

Be careful
with
deallocation
of the space

Hello World
Circle object
CS340

20

n The ability to create new things


that are more useful (usually more
abstract) than the basic machine
resources
n To allow us to build mechanical
checks to stop us from hurting
ourselves

Heap

42

42

The Role Of A Type System

myInt = 42;
o
= myInt;
ymInt = (int)o;

Stack
myInt

myInt

o What do we need from a type


system?

o Conversion between value variable


and reference variable
o System.Int32
object
int

Heap

Stack

o Reference types:

42

o The notion of type is artificial


CS340

21

Spring 2003

CS340

22

The Role Of A Type System


o Types across languages:
n Consistency
n Compatibility

o Type safety on steroids:


n Checking for meaningful statements
n Add speed to distance?
n Assembly language vs. C vs Java and C#
vs Ada

o Type needs to support application


semantics
Spring 2003

CS340

23

You might also like