Differences between C and C Dr Catherine Stringfellow

Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter

. NET Framework Class Library and Namespaces ¢ In both C++ and C#, one can develop Console and Windows applications ¢ Window apps in C++ use MFCs, in C# use the FCL for the. NET platform ¢ FCL is composed of many namespaces using System. Windows. Forms;

Control Structures if, if-else, switch similar to C++ ¢ Loops similar to C++ ¢ l But there is a foreach for arrays

Math Class ¢ Class Math is located in namespace System (unnecessary to add an assembly reference) ¢ Using methods of static classes ¢ l Class. Name. Method. Name( argument 1, arument 2, … ) l Example: Math. Sqrt (900. 0) Constants l Math. PI = 3. 1415926535…

Type Promotion ¢ Implicit Conversion l ¢ Coercion of arguments to a higher type when passed to methods or in mixed-type expressions; Explicit Conversion l Done with cast or class Convert in namespace System l Cast Example: int result = Square ( (int ) y );

Value and Reference Types ¢ Value types Contain data of the specified type l Built in types (int, float, double, …) l Programmer created - structs and enumerations l ¢ Reference types Contain an address l Built-in (array, object and string) l Programmer created – Classes, Interfaces and Delegates l

Passing Arguments by Value vs. by Reference ¢ Value types are passed by value and reference types are passed by reference by default ¢ To pass a value type by reference so you can modify the original variable? l Use the ref keyword • with variables already initialized l Use the out keyword • when the called method will initialize it

Declaring Arrays ¢ Must use new operator to allocate dynamically the number of elements in the array int[] x; x = new int[10]; // declare reference to an array // dynamically allocate array

Array Methods and Properties ¢ Since sorting data is important in many applications, . NET Framework includes high-speed sorting capabilities // sort elements in array a Array. Sort( x ); // Determine number of elements in x by property x. Length

Multiple-Subscripted Arrays ¢ Rectangular arrays – syntax a little different from C++ ¢ Jagged Arrays l An array of arrays of different lengths
![// declaration of rectangular array int[, ] array 1 = new int[5, 10]; // // declaration of rectangular array int[, ] array 1 = new int[5, 10]; //](http://slidetodoc.com/presentation_image_h2/697c50f92f18ae0ef2216c10ba7e8840/image-11.jpg)
// declaration of rectangular array int[, ] array 1 = new int[5, 10]; // declaration and initialization of jagged array int [][] array 2 = new int[ 3 ][]; array 2[ 0 ] = new int[] { 1, 2 }; array 2[ 1 ] = new int[] { 3 }; array 2[ 2 ] = new int[] { 4, 5, 6 };

foreach Repetition Structure ¢ ¢ ¢ The foreach repetition structure is used to iterate through values in arrays No counter A variable is used to represent the value of each element foreach ( int grade in grade. Array ) { if ( grade < low. Grade ) low. Grade = grade; }

Initializing Class Objects: Constructors ¢ If the constructor does not explicitly initialize data members, the data members are initialized by default l l l Primitive numeric types are set to 0 Boolean types are set to false Reference types are set to null

Properties ¢ Public properties allow clients to: l Get (obtain the values of) private data • and may control formatting and display l Set (assign values to) private data • and may scrutinize attempts to modify value

class Time { private int hour; // property Hour public int Hour { get { return hour; } set { hour = ( ( value >= 0 && value < 24 ) ? value : 0 ); } } } Use it in caller as cout << time. Hour; or time. Hour = 5;

Garbage Collection ¢ When objects are no longer referenced, the CLR performs garbage collection ¢ Use finalizers in conjunction with the garbage collector to release resources (database connections, file access, etc. ) explicitly

To. String ¢ Everyone in C#. NET community uses To. String to obtain an object’s string representation. //Method of class Point to return string representation of Point public override string To. String ( ) { return “(" + x + ", " + y + “)"; } // call method to display new point value string output += "nn. The new location of point is " + point;

Other “interesting” variations from familiar C++ constructs ¢ ¢ ¢ abstract classes use keyword abstract sealed classes that cannot be overridden Interfaces use inheritance notation Delegates provide mechanism for passing method references Exception handling includes a finally block to release resources
- Slides: 18