from c++ to c#. web programming the course is on web programming using asp.net and c# the course is...
TRANSCRIPT
From C++ to C#
Web programming The course is on web programming using ASP.Net
and C# ASP.Net is typically programmed using Visual Basic
(VB.Net) or C# C# has been chosen for this course as:
it is the latest language from Microsoft is considered to be technically superior to VB.Net Once you know C# you can easily pick up VB.Net on your
own. Before we get to ASP.Net and the web we need to
learn C#
C#
C# is pronounced as ‘See Sharp’ Is a Modern, Object-Oriented, Type safe
language Created by Microsoft but specification is in the
open domain (ISO standard) After C++ came Java and then came C# C# is quite similar to Java.
Very easy for C# programmer to migrate to Java
From C++ to C#
All of you have already done courses in C and C++ programming
Now you should be able to migrate to any programming language quickly.
We will migrate from C++ to C# in two to three weeks.
Quick exposure and less or even no assignments But you have to try out example programs You will definitely have C# prog. Assignments when
we get to ASP.Net
Course material
The course material for this migration is Chapter 1 of the document ‘C# Language Specification Version 1.2’ from Microsoft.
It is available in directory C:\Program Files\Microsoft Visual Studio 8\VC#\Specifications\1033
Note that the document we will use is version 1.2 and not version 2.0
Chapter 1 of the document has some errors which I will inform you at the appropriate time
Hit the ground running
Chapter 1 of the C# spec document is not a step-by-step explanatory chapter.
Terms may be referred to without prior explanation. The terms could be explained later on in the document.
Don’t expect 100 % understanding as you go through the document the first time
After two or three readings of the chapter things will fall into place.
C# Features
Component-Oriented language Supports Properties, Methods and Events
Garbage collection Automatically reclaims memory of unused objects So, no need to worry about deleting and destroying objects
manually!!! Big Relief Exception Handling
Like in C++ Type-Safe (Like C++ but with some additional
safety)
C# Features contd.
Unified Type System All C# types inherit from a single root type called object
Even primitive types like int and double inherit from object
So all types share the operations of type object Values of any type can be viewed as a reference to
base class object
C# Features contd.
Value type and Reference type Reference type is for dynamic allocation of
objects Value type is inline storage (on stack)
Hello World in C#
See section 1.1 of C# Spec.
Value type and Reference type
Two kinds of types in C#: Value type Reference type
Value Type
int i = 1; i is a value type Variables of value type directly contain their
data So operation on one variable will change only
that variable’s data and not affect any other variable’s data
See valuetypes.cs (Not in C# Spec. doc)
Reference Type Variables of reference type store references to their data
(called as object) Assuming simp is a class type
simp i = new simp(1); Variable i points to object of type simp in dyanmic memory which has
been initialized using its constructor i.e. i refers to the object in dyanmic memory
It is possible for two variables to reference the same object. So operations changing the object referred to by one variable
will result in all variables referring that object seeing the change.
See referencetypes.cs (Not in C# Spec. doc)
Reference in C# and C++
The primary feature of reference is same in C# and C++, which is: The reference variable refers to an object The reference variable itself is not the object
But lots of differences between C# and C++ C# reference variable can be changed to point to another
object. [Not allowed in C++] C# reference variable can be defined without referring to
anything (null reference). [Not allowed in C++]
Reference in C#
The only way to define an object of a class (reference type) is by creating a reference variable and assigning to a ‘new’ object of that class. simp i = new simp(1);
You cannot define an object (of class type) on the stack simp j; In C#, above statement defines a variable containing a null
reference. No object of type simp is created. In C++, the above statement would have created an object
of type simp called j and called its default constructor!!!
String class in C# String is a reference type But it is a special reference type It behaves like a value type for assignment operations and
comparisons Assigning one string variable to another creates a copy of the
string. See refstringtype.cs (Not in C# Spec. doc) Two string objects with the same content but different
locations in memory return true when tested for equality. See refstringcomp.cs (Not in C# Spec. doc)
This is different for ordinary reference variables. See referencetypecomp.cs (Not in C# Spec. doc). The reference variables will be equal only if both point to the same object!!
Value type and Reference type contd.
See Category/Description table in Section 1.3 See the following table summarizing C#’s
numeric types
Boxing and Unboxing
Every C# type directly or indirectly derives from object
Instances of Reference types can be directly viewed using an object reference
Instances of value types are treated as object by boxing
Boxing and Unboxing …
int i = 123; object o = i; //Boxing When an instance of value type is converted to
type object: an object instance called a ‘box’ is created and the value is copied into it.
Boxing and Unboxing …
int j = (int) o; //Unboxing Conversely when an object reference is cast to
a value type, a check is made that the referenced object is a box
of the correct value type and if the check succeeds the value in the box is copied out
Boxing and Unboxing
See boxing.cs (not in spec. code) See boxing2.cs (not in spec. code)
Program Structure
A C# program consists of types Classes and interfaces are examples of types Each type contains members. Members can be: fields,
methods, properties and events The types of a program can be organized into
namespaces Compilation of C# programs gives an assembly .exe assembly is an application .dll assembly is a library
Program Structure …
Refer section 1.2 (stack class, user program as well as explanations)
Assignment 1: Read strings from user till end of file and push
each string onto the stack class shown above. String s = Console.ReadLine() will read a string
from console. After end of file, print out all the strings on the
stack. Do not modify stack class
Assignment 2
Add method IsEndofStack() to stack class and use that method to solve assignment 1 problem (you can first copy your assignment 1 solution as assignment 2 solution)