Main method
In C++, The first character of the main() function must be smalle.g. int main () { int a = 100; int b = 200; }In C#, The first character of the Main() function must be capitalized and should return either int or void
e.g. static int Main(string[] args) { int a = 110; int b = 510; }
Environment
C++ was designed to be a low-level platform-neutral object-oriented programming language.C# was designed to be a higher-level component-oriented language.
Namespace/HeaderFile
C++ support #includeC# does not support #include, it support using (not same as #include, both are different)
Compilation
C++ code compiles into assembly language.C# compiles into Intermediate language (IL) which converted into executable code through the process called Just-In-Time compilation.
Faster
C++ codes not compiled into a bytecode, they generate machine code, so its faster.C# programs are compiled down to a bytecode which causes a JIT to occur when the programs are executed which takes time i.e it is slow.
Pointer/delegates
C++ has the concept of function pointers.void main () { int value = 2; // actual variable. int *p; // pointer variable p = &value; // store address of value in pointer variable cout << "Value of value variable: "; cout << value << endl; cout << "Address stored in p pointer variable: "; cout << p << endl; cout << "Value of *p variable: "; cout << *p << endl; }C# does not have the concept of function pointers. C# has a similar concept called Delegates.
public delegate int DE(int a); public class C1 { public int p(int x) { return x; } } class Program { static void Main() { C1 c11 = new C1(); DE d1 = c1.p; int i = d1(10); Console.WriteLine(i); } }
Variable
In C++, uninitialized variables undetected thus result is unpredictable output.C# checks for uninitialized variables and gives error message at compile time.
Memory manegement:-
In C++, the memory that is allocated in the heap dynamically has to be explicitly deleted.In C#, memory management is automatically handled by garbage collector.
Exception:-
In C++, The exception can throw by any class.in C#, The exception can only throw by a class that is derived from the System.Exception class.
Finally Block
C++ does not have finally block exception handling mechanism.e.g try { // code here } catch (int ex1) { cout << "int exception"; } catch (char ex2) { cout << "char exception"; } catch (...) { cout << "default exception"; }C# has finally block in exception handling mechanism which contains code that is always executed at the end of try block.
e.g try { // code here } catch (ApiConnectionSecurityException ex1) { Console.WriteLine("Exception caught: {0}", ex1) //using console application } catch DivideByZeroException ex2) { lblMsg.Text= ex2.Message; } catch (Exception ex3) { lblMsg.Text= ex2.Message; }Note:- The finally block is executed as soon as control leaves a catch or try block, and typically contains cleanup code for resources allocated in the try block, C# allow order of catch blocks correctly.
Access modifiers
in C++ supports three access modifiers public, private, protected.in C# supports five access modifiers public, private, protected, internal and protected internal.
By Class
In C++, the end of the class definition has a closing brace followed by a semicolon.class ClassCplus { static void Main() { } }; // close with semicolonIn C#, the end of the class definition has a closing brace alone.
class ClassCsharp { static void Main() { } } // not close with semicolon
By Struct
C++ struts behave like classes except that the default access is public instead of private.C# struts can contain only value types The struts is sealed and it cannot have a default no-argument constructor.
By Foreach
C++ does not contain foreach statement.C# supports foreach statement.
class ForEachSample { static void Main() { int[] ListOfArrays = new int[] { 0, 1, 23, 30, 4, 33, 54, 71, 8, 19 }; foreach (int num in ListOfArrays) { System.Console.WriteLine(num); } System.Console.ReadLine(); } }
By Switch
When break statement is not present after statement in switch case than fall through go to next case (in both case code present or not in case statement).int main () { char Security= 'Z'; switch(Security) { case 'A' : cout << "For P.M." << endl; break; case 'C' : //break statement is not present, switch case than fall through go to next case case 'D' : cout << "For D.M" << endl; break; case 'Z' : cout << "For President"<< endl; break; default : cout << "Invalid Security" << endl; } cout << "Your Security is " << Security << endl; return 0; }In C++ Switch Statement, the test variable cannot be a string.
int main() { switch(std::string("MaaShardaTechnology")) //Compilation error - switch expression of type illegal { case "Opt": } } //Compilation error Because C/C++ doesn't really support strings as a type.When break statement is not present after statement in switch case than fall through not go to next case (in both case code present or not in case statement).
//It does support the idea of a constant char array but it doesn't really fully understand the notion of a string.
class Program { static void Main() { string Security= 'Z-Type'; switch(Security) { case 'A_Type' : Console.WriteLine(For P.M.); break; case 'C_Type' : //break statement is not present, switch case than not go to next case case 'D_Type' : Console.WriteLine(For D.M); break; case 'Z_Type' : Console.WriteLine(For President); break; default : Console.WriteLine(Invalid Security); } Console.WriteLine(Your Security is {0}, Security); Console.ReadLine(); } }In C# Switch Statement, the test variable can be a string. above example
By Inheritance
C++ supports multiple inheritance.C# does not supports multiple inheritance.
By Macros
C++ supports macros.#includeBut, C# does not supports macros because One of our main design goals for C# is to keep the code very readable#define MAX(X,Y) (((X)>(Y)) ? X : Y) int main () { int i, j; i = 510; j = 205; cout <<"The Maximum value is " << MAX(i, j) << endl; return 0; }
By Type-Safe
C++ is not type-safe.C# is language type-safe.
By Arrays
C++ arrays are of value type.C# arrays are of reference type. The tokens "[]" appear following the array type in C#.
By bit Field
C++ supports bit fields.#includeBut, C# does not supports bit fields.#include struct { unsigned int i; unsigned int j; } s1; struct { unsigned int i: 1; unsigned int j: 1; } s2; int main( ) { printf( "Memory size occupied by s1 : %d\n", sizeof(s1)); printf( "Memory size occupied by s2 : %d\n", sizeof(s2)); return 0; }
By Data Type
long data type is 32 bits in c++.Type | Bit Repersent | Range |
---|---|---|
long | 8bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Type | Bit Repersent | Range |
---|---|---|
long | 64-bit signed integer type | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
0 comments:
Post a Comment