Documente Academic
Documente Profesional
Documente Cultură
? What is C# ?
? Some C# FAQs
C# - An Introduction ? C# Features
? Windows Forms
? Web Forms
- Parag Godkar
Agenda
? What is C# ?
? Some C# FAQs
Demo: First Program ? C# Features
? Windows Forms
? Web Forms
Keeping in line with the tradition –
The Hello World Program
1
Some C# FAQs Some C# FAQs
? Is C# a new version of C++ ?
? No, It is a totally new object oriented ? Does C# support RAD ( Rapid
language built from the ground up to Application Development ) like VB ?
support .Net Framework. ? Yes, it does. It supports drag, drop and
? The syntax is similar to that of C++. build features of VB.
? How does C# relate to Java ? ? Where do I get a C# Compiler ?
? The .Net Framework SDK includes it.
? Java Programmers will find it easy to
migrate to C#. ? It is not yet distributed individually.
? Third Parties are free to write their own
? The concepts are similar to Java.
compilers.
National Centre for Software National Centre for Software
10/26/2002 Technology 7 10/26/2002 Technology 8
2
C# Features C# Features
Type System Type System
? Present Scenario –
? Different languages have their own ? In .Net and therefore in C#, types are
implementation of data types. grouped into two -
? This difference makes software ? Value types
interoperability very difficult. ? Reference types
? .Net Framework – ? Value and Reference types differ into
two characteristics –
? Standard representation and range limit for
? Where they are stored in memory.
a data type for all languages supporting
.Net Framework. ? How they behave in the context of the
assignment statements.
National Centre for Software National Centre for Software
10/26/2002 Technology 13 10/26/2002 Technology 14
C# Features
Type System C# Features Type System - Value Types
? Value types are stored on the stack, ? Allows you to store values in a variable
and assignment statements between with a certain type.
two values variables result in two
? It is a reserved space in memory and
separate, but identical, copies of the
value in memory. your program directly manipulates data
stored in that space.
? Reference types are stored on the
heap, and an assignment statement ? Value types include signed and
C# Features Type System - Value Types C# Features Type System - Reference Types
? Value types are further grouped into five - ? In C#, all objects are defined as reference
? Integral type: sbyte, byte, short, ushort, int, uint, types.
long, ulong and char ? Although technically it is still a pointer, you
? Floating point types: float and double are prevented to manipulate it like a pointer.
? Decimal type: decimal You can forget your pointer maths in C#.
? Bool type: bool ? They are type -safe pointers meaning that
? Enumeration type: enum instead of merely being an address, which
might or might not point to what you think it
? E.g. – does, a reference (when not null) is always
enumerators, structures and primitive types guaranteed to point to an object that is of the
type specified and that has already been
allocated on the heap.
National Centre for Software National Centre for Software
10/26/2002 Technology 17 10/26/2002 Technology 18
3
C# Features Type System - Reference Types C# Features Everything is an Object
? Boxing: ? Boxing:
Conversion of Value Type to a Reference Type.
?
? E.g.:
? UnBoxing:
int n = 4; // value type
? Conversion of Reference Type to a Value Type.
object obj = n; // ‘n’ is boxed to
? All types in C# are objects.
‘obj’
? They are objects only when they need to be,
thereby avoiding the overhead required if
? When the compiler finds a value type
everything actually were an object. where it needs a reference type, it
creates an object “box” into which it
places the value of the value type.
National Centre for Software National Centre for Software
10/26/2002 Technology 21 10/26/2002 Technology 22
C# Features
C# Features Boxing and UnBoxing object and string
? UnBoxing: ? E.g.:
? E.g.: int n = 4;
int n = 4; // value type
string s = n. ToString() // call
object obj = n; // create a ‘box’ to hold n
Object. ToString ()
int m = (int)obj; // unbox n
? C# provides with two different reference types
? We need an explicit cast while unboxing as
– object and string. All other classes are based
‘obj’ could be referring to any kind of object
on object even if they don’t claim inheritance
and the compiler must verify the type.
from any other class.
4
C# Features C# Features
Classes Classes
C# Features C# Features
Classes Properties
C# Features C# Features
Properties Properties
5
C# Features C# Features
Properties Arrays
? A property can be decorated with ? In C#, Arrays are objects that have
virtual, overide and abstract modifiers, System.Array defined as their base class.
enabling a derived class to inherit and ? So defining an Array causes instantiation of
overide properties. System.Array class inheriting all it’s members.
? Declaring an Array –
? E.g. – int[] number
? Because properties provide a generic ? An Array is not actually created until you
and intuitive way of accessing data instantiate it just like a class.
members – object.field – they are
sometimes called as Smart Fields.
National Centre for Software National Centre for Software
10/26/2002 Technology 31 10/26/2002 Technology 32
C# Features C# Features
Arrays Arrays
C# Features C# Features
Indexers Indexers
? C# specific feature that enables one to ? Being related to properties, it makes sense
programmatically treat objects as to have similar syntax.
though they were arrays. ? Defining Indexers is like defining Properties
? Elaboration of properties. with two differences –
Indexer takes an index argument.
? Used where one wants to access some
?
6
C# Features C# Features
Indexers Indexers
C# Features C# Features
Interfaces Interfaces
C# Features C# Features
Interfaces Interfaces
7
C# Features C# Features
Delegates Delegates
? C# specific feature which basically serves the ? Delegate defines a single method without
same purpose as function pointers in C++. implementing it.
? However, delegates are type-safe, secure ? Unlike Interfaces, Delegates refer only to
managed objects.
single methods and are defined at run time.
? Which means that the CLR guarantees that a
? Delegate object will simply delegate the
delegate points to a valid method, which
further means that you get all the benefits of actual processing to the method we pass it.
function pointers without any of the ? Delegates have two main usages in C#
associated dangers, such as an invalid programming: callbacks and event handling.
address or a delegate corrupting the memory
of other objects.
C# Features C# Features
Delegates Events
C# Features C# Features
Events Events
? There are some strict rules concerning a ? The class that wants to use events defines
delegate that's used in this fashion – callback functions as delegates, and the
? First, the delegate must be defined as listening object then implements them.
taking two arguments.
? Second, these arguments always represent ? Demo
two objects: the object that raised the
event (the publisher) and an event
information object.
? Additionally, this second object must be
derived from the .NET Framework's
EventArgs class.
National Centre for Software National Centre for Software
10/26/2002 Technology 47 10/26/2002 Technology 48
8
C# Features
Attributes C# Automatic Memory Management
(Garbage Collection)
? Items of declarative information that can be
declared by the programmer and attached to ?
Every program uses resources of one sort or
elements in your code – classes, methods, another—memory buffers, screen space,
data members or properties. network connections, database resources,
? They can be queried at runtime by anyone and so on.
making use of your class by using reflection.
?
? A number of standard attributes are provided In fact, in an object-oriented environment,
with C#. Most of them allow C# code to work every type identifies some resource available
along with unmanaged code. for your program's use. To use any of these
resources requires that memory be allocated
to represent the type.
National Centre for Software National Centre for Software
10/26/2002 Technology 49 10/26/2002 Technology 50
9
C# Automatic Memory Management C# Automatic Memory Management
(Garbage Collection) (Garbage Collection)
? Resource Allocation – When a process is Managed Heap
?
The Microsoft® .NET Common Language initialized -
?
The CLR reserves a contiguous
Runtime (CLR) requires that all resources region of address space that
be allocated from the managed heap. initially has no storage
? allocated for it.
You never free objects from the managed
?
The address space region is the
heap—objects are automatically freed by managed heap.
the GC when they are no longer needed by ?
The heap also maintains a
the application by following an algorithm. pointer.
itself up properly when the garbage collector ? When the GC sees that the object is in
decides to free the resource's memory. garbage, and sees that it has a finalise
method, it calls it.
10
C# Automatic Memory Management C# Automatic Memory Management
(Garbage Collection) (Garbage Collection)
? In the case of releasing scarce
resources, Microsoft proposes a solution
based on the Dispose design pattern.
? This design pattern recommends that
the object expose a public method,
called something generic like Close or
Dispose,that the user is then instructed
to call when finished using the object.
? It's then up to the class's designer to do
any necessary cleanup in that method.
National Centre for Software National Centre for Software
10/26/2002 Technology 61 10/26/2002 Technology 62
C# Pointers C# Pointers
? Pointers allow direct manipulation of memory. ? Why would you want to bypass the safe
? But, they conflict with the operation of the memory management of C#?
Import some C/C++ code containing pointers.
Garbage Collector (GC).
?
C# Pointers C# Pointers
? E.g.- ? SideNote – Unsafe and Unmanaged Code
? Unmanaged Code is code that is not
public class Test
managed by the CLR. E.g. - VB6 executables
{
unsafe public void DoSomething() or standalone VC++ programs without
{ ---- } managed extensions.
? Unsafe Code is managed by the CLR; it just
public void DoSomethingelse() escapes the memory management
{
unsafe { // do something unsafe here }
mechanism (GC) of the C# compiler.
}
}
11
C# Exception Handling
C# Exception Handling
? One of the main roles of the CLR –
? Avoid runtime errors through automatic memory ? Exceptions are error conditions that arise
and resource management when using managed when the normal flow of a code path —that is,
code. a series of method calls on the call stack—is
? Catch errors at compile time ( by a strongly typed impractical or imprudent.
system ). ? Most exceptions also involve another
? However, certain errors can be caught only at problem: context.
run time, and therefore a consistent means of ? This is the entire reason for the existence of
dealing with errors must be used across all exception handling: one method determines
the languages that comply with the Common that an exception condition has been reached
Language Specification (CLS). and happens not to be in the correct context
? Error-handling system implemented by the to deal with the error.
CLR —Exception Handling.
National Centre for Software National Centre for Software
10/26/2002 Technology 67 10/26/2002 Technology 68
? Demo
National Centre for Software National Centre for Software
10/26/2002 Technology 69 10/26/2002 Technology 70
Miscellaneous
12