Presentation is loading. Please wait.

Presentation is loading. Please wait.

From C++ to C#. Web programming The course is on web programming using ASP.Net and C# The course is on web programming using ASP.Net and C# ASP.Net is.

Similar presentations


Presentation on theme: "From C++ to C#. Web programming The course is on web programming using ASP.Net and C# The course is on web programming using ASP.Net and C# ASP.Net is."— Presentation transcript:

1 From C++ to C#

2 Web programming The course is on web programming using ASP.Net and C# The course is on web programming using ASP.Net and C# ASP.Net is typically programmed using Visual Basic (VB.Net) or C# ASP.Net is typically programmed using Visual Basic (VB.Net) or C# C# has been chosen for this course as: C# has been chosen for this course as: it is the latest language from Microsoft it is the latest language from Microsoft is considered to be technically superior to VB.Net is considered to be technically superior to VB.Net Once you know C# you can easily pick up VB.Net on your own. 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# Before we get to ASP.Net and the web we need to learn C#

3 C# C# is pronounced as ‘See Sharp’ C# is pronounced as ‘See Sharp’ Is a Modern, Object-Oriented, Type safe language Is a Modern, Object-Oriented, Type safe language Created by Microsoft but specification is in the open domain (ISO standard) Created by Microsoft but specification is in the open domain (ISO standard) After C++ came Java and then came C# After C++ came Java and then came C# C# is quite similar to Java. C# is quite similar to Java. Very easy for C# programmer to migrate to Java Very easy for C# programmer to migrate to Java

4 From C++ to C# All of you have already done courses in C and C++ programming All of you have already done courses in C and C++ programming Now you should be able to migrate to any programming language quickly. Now you should be able to migrate to any programming language quickly. We will migrate from C++ to C# in two to three weeks. We will migrate from C++ to C# in two to three weeks. Quick exposure and less or even no assignments Quick exposure and less or even no assignments But you have to try out example programs But you have to try out example programs You will definitely have C# prog. Assignments when we get to ASP.Net You will definitely have C# prog. Assignments when we get to ASP.Net

5 Course material The course material for this migration is Chapter 1 of the document ‘C# Language Specification Version 1.2’ from Microsoft. 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 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 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 Chapter 1 of the document has some errors which I will inform you at the appropriate time

6 Hit the ground running Chapter 1 of the C# spec document is not a step-by-step explanatory chapter. 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. 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 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. After two or three readings of the chapter things will fall into place.

7 C# Features Component-Oriented language Component-Oriented language Supports Properties, Methods and Events Supports Properties, Methods and Events Garbage collection Garbage collection Automatically reclaims memory of unused objects Automatically reclaims memory of unused objects So, no need to worry about deleting and destroying objects manually!!! Big Relief So, no need to worry about deleting and destroying objects manually!!! Big Relief Exception Handling Exception Handling Like in C++ Like in C++ Type-Safe (Like C++ but with some additional safety) Type-Safe (Like C++ but with some additional safety)

8 C# Features contd. Unified Type System Unified Type System All C# types inherit from a single root type called object All C# types inherit from a single root type called object Even primitive types like int and double inherit from object Even primitive types like int and double inherit from object So all types share the operations of type object So all types share the operations of type object Values of any type can be viewed as a reference to base class object Values of any type can be viewed as a reference to base class object

9 C# Features contd. Value type and Reference type Value type and Reference type Reference type is for dynamic allocation of objects Reference type is for dynamic allocation of objects Value type is inline storage (on stack) Value type is inline storage (on stack)

10 Hello World in C# See section 1.1 of C# Spec. See section 1.1 of C# Spec.

11 Value type and Reference type Two kinds of types in C#: Two kinds of types in C#: Value type Value type Reference type Reference type

12 Value Type int i = 1; int i = 1; i is a value type i is a value type Variables of value type directly contain their data 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 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) See valuetypes.cs (Not in C# Spec. doc)

13 Reference Type Variables of reference type store references to their data (called as object) Variables of reference type store references to their data (called as object) Assuming simp is a class type Assuming simp is a class type simp i = new simp(1); simp i = new simp(1); Variable i points to object of type simp in dyanmic memory which has been initialized using its constructor 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 i.e. i refers to the object in dyanmic memory It is possible for two variables to reference the same object. 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. 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) See referencetypes.cs (Not in C# Spec. doc)

14 Reference in C# and C++ The primary feature of reference is same in C# and C++, which is: The primary feature of reference is same in C# and C++, which is: The reference variable refers to an object The reference variable refers to an object The reference variable itself is not the object The reference variable itself is not the object But lots of differences between C# and C++ 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 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++] C# reference variable can be defined without referring to anything (null reference). [Not allowed in C++]

15 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. 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); simp i = new simp(1); You cannot define an object (of class type) on the stack You cannot define an object (of class type) on the stack simp j; simp j; In C#, above statement defines a variable containing a null reference. No object of type simp is created. 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!!! In C++, the above statement would have created an object of type simp called j and called its default constructor!!!

16 String class in C# String is a reference type String is a reference type But it is a special reference type But it is a special reference type It behaves like a value type for assignment operations and comparisons 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) 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) 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!! 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!!

17 Value type and Reference type contd. See Category/Description table in Section 1.3 See Category/Description table in Section 1.3 See the following table summarizing C#’s numeric types See the following table summarizing C#’s numeric types

18 Boxing and Unboxing Every C# type directly or indirectly derives from object Every C# type directly or indirectly derives from object Instances of Reference types can be directly viewed using an object reference Instances of Reference types can be directly viewed using an object reference Instances of value types are treated as object by boxing Instances of value types are treated as object by boxing

19 Boxing and Unboxing … int i = 123; int i = 123; object o = i; //Boxing object o = i; //Boxing When an instance of value type is converted to type object: When an instance of value type is converted to type object: an object instance called a ‘box’ is created an object instance called a ‘box’ is created and the value is copied into it. and the value is copied into it.

20 Boxing and Unboxing … int j = (int) o; //Unboxing int j = (int) o; //Unboxing Conversely when an object reference is cast to a value type, 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 a check is made that the referenced object is a box of the correct value type and if the check succeeds and if the check succeeds the value in the box is copied out the value in the box is copied out

21 Boxing and Unboxing See boxing.cs (not in spec. code) See boxing.cs (not in spec. code) See boxing2.cs (not in spec. code) See boxing2.cs (not in spec. code)

22 Program Structure A C# program consists of types A C# program consists of types Classes and interfaces are examples of types Classes and interfaces are examples of types Each type contains members. Members can be: fields, methods, properties and events Each type contains members. Members can be: fields, methods, properties and events The types of a program can be organized into namespaces The types of a program can be organized into namespaces Compilation of C# programs gives an assembly Compilation of C# programs gives an assembly.exe assembly is an application.exe assembly is an application.dll assembly is a library.dll assembly is a library

23 Program Structure … Refer section 1.2 (stack class, user program as well as explanations) Refer section 1.2 (stack class, user program as well as explanations) Assignment 1: Assignment 1: Read strings from user till end of file and push each string onto the stack class shown above. 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. String s = Console.ReadLine() will read a string from console. After end of file, print out all the strings on the stack. After end of file, print out all the strings on the stack. Do not modify stack class Do not modify stack class

24 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) 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)


Download ppt "From C++ to C#. Web programming The course is on web programming using ASP.Net and C# The course is on web programming using ASP.Net and C# ASP.Net is."

Similar presentations


Ads by Google