1 / 18

Differences between C# and C++

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

kirby
Télécharger la présentation

Differences between C# and C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. .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;

  3. Control Structures • if, if-else, switch similar to C++ • Loops similar to C++ • But there is a foreach for arrays

  4. Math Class • ClassMath is located in namespace System (unnecessary to add an assembly reference) • Using methods of static classes • ClassName.MethodName( argument1, arument2, … ) • Example: Math.Sqrt (900.0) • Constants • Math.PI = 3.1415926535…

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

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

  7. 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? • Use the ref keyword • with variables already initialized • Use the out keyword • when the called method will initialize it

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

  9. 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

  10. Multiple-Subscripted Arrays • Rectangular arrays – syntax a little different from C++ • Jagged Arrays • An array of arrays of different lengths

  11. // declaration of rectangular array int[,] array1 = newint[5,10]; // declaration and initialization of jagged array int [][] array2 = newint[ 3 ][]; array2[ 0 ] = newint[] { 1, 2 }; array2[ 1 ] = newint[] { 3 }; array2[ 2 ] = newint[] { 4, 5, 6 };

  12. 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 gradeArray ) { if ( grade < lowGrade ) lowGrade = grade; }

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

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

  15. classTime { 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;

  16. 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

  17. ToString • Everyone in C#.NET community uses ToString to obtain an object’s string representation. //Method of class Point to return string representation of Point public overridestring ToString ( ) { return“(" + x + ", " + y + “)"; } // call method to display new point value string output += "\n\nThe new location of point is " + point;

  18. 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

More Related