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.

Slides:



Advertisements
Similar presentations
Introduction to Java Objects CSIS 3701: Advanced Object Oriented Programming.
Advertisements

George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Chapter 10.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#
Miscellaneous OOP topics Java review continued. Simple data types & wrapper classes Simple data types are the built-in types provided as part of the Java.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
ASP.NET Programming with C# and SQL Server First Edition
Abstract Data Types and Encapsulation Concepts
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
A First Program Using C#
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
Module 1: Introduction to C# Module 2: Variables and Data Types
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
Adapted from Prof. Necula UCB CS 1641 Overview of COOL ICOM 4029 Lecture 2 ICOM 4029 Fall 2008.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
11 Web Services. 22 Objectives You will be able to Say what a web service is. Write and deploy a simple web service. Test a simple web service. Write.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
JavaScript, Fourth Edition
C#C# Introduction CS3260 Dennis A. Fairclough Version 1.0 Introduction CS3260 Dennis A. Fairclough Version 1.0.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
C# EMILEE KING. HISTORY OF C# In the late 1990’s Microsoft recognized the need to be able to develop applications that can run on multiple operating system.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Types in programming languages1 What are types, and why do we need them?
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Session 7 Methods Strings Constructors this Inheritance.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Object-Oriented Programming Chapter Chapter
1 9/6/05CS360 Windows Programming CS360 Windows Programming.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
ISBN Object-Oriented Programming Chapter Chapter
Introduction to Object-Oriented Programming Lesson 2.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Object Oriented Software Development 4. C# data types, objects and references.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Classes, Interfaces and Packages
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
1 Introduction to Object Oriented Programming Chapter 10.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
FASTFAST All rights reserved © MEP Make programming fun again.
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
INTRODUCTION BEGINNING C#. C# AND THE.NET RUNTIME AND LIBRARIES The C# compiler compiles and convert C# programs. NET Common Language Runtime (CLR) executes.
Object Lifetime and Pointers
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Java Primer 1: Types, Classes and Operators
CS360 Windows Programming
MIS Professor Sandvig MIS 324 Professor Sandvig
Tutorial C#.
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Java Programming Language
ICOM 4029 Fall 2003 Lecture 2 (Adapted from Prof. Necula UCB CS 164)
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
Introduction to Classes and Objects
Presentation transcript:

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 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#

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

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

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

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.

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)

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

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)

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

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

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)

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)

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++]

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!!!

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!!

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

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

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.

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

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)

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

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

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)