Introduction
C# is a distinct language from C++. C++ is designed for general object oriented programming in the days when the typical computer was a standalone machine running a command line-based user interface. C++ is a general-purpose programming language with high-level and low-level capabilities. It is a statically typed, free-form, multi-paradigm, usually compiled language supporting procedural programming, data abstraction, object-oriented programming, and generic programming.
C++ is regarded as a mid-level language. This indicates that C++ comprises a combination of both high-level and low-level language features.C# is designed specifically to work with the .Net and is geared to the modern environment of Windows and mouse-controlled user interface, networks and the internet.
Microsoft has defined C# as follows
C# is a simple, modern, object oriented, and type-safe programming language derived from C and C++. C# (pronounced C sharp) is firmly planted in the C and C++ family tree of languages, and will immediately be familiar to C and C++ programmers. C# aims to combine the high productivity of Visual Basic and the raw power of C++.
However it is also undeniable that the two languages are very similar in both their syntax and in that they are both designed to facilitate the same paradigm of programming, in which code is based around hierarchies of inherited classes.
Below I will briefly summarize the overall differences and similarities between the two languages.
Environment
C++ was designed to be a low-level platform-neutral object-oriented programming language. C# was designed to be a somewhat higher-level component-oriented language. The move to a managed environment represents a sea change in the way you think about programming. C# is about letting go of precise control, and letting the framework help you focus on the big picture.
With the managed environment of .NET, you give up that level of control. When you choose the type of your object, the choice of where the object will be created is implicit. Simple types (int, double, and long) are always created on the stack (unless they are contained within other objects), and classes are always created on the heap. You cannot control where on the heap an object is created, you can't get its address, and you can't pin it down in a particular memory location. (There are ways around these restrictions, but they take you out of the mainstream.)
The very structure of C# reflects the underlying framework. There are no multiple inheritances and there are no templates because multiple inheritances are terribly difficult to implement efficiently in a managed, garbage-collected environment, and because generics have not been implemented in the framework.
Compile Target
C++ code usually compiles to assembly language. C# by contrast compiles to Intermediate language (IL), which has some similarities to java byte code. The IL is subsequently converted to native executable code by a process of Just-In-Time compilation. The emitted IL code is stored in a file or a set of files known as an assembly. An assembly essentially forms the unit, in which IL code id packaged, corresponding to a DLL, or executable file that would be created by C++ compiler.
Memory Management
C# is designed to free the developers from the task of doing memory management. It means in C# you do not have to explicitly delete memory that was allocated dynamically on the heap, as you could in C++. Rather, the garbage collector periodically cleans up memory that is no longer needed.
- int j = 30;
- Myclass *pMine=new Myclass
- Int j=30;
- Myclass mine=new Myclass()
The big difference between C++ and C# is that C# doesn't allow you to choose how to allocate memory for a particular instance. For example, in C++ you wished to do this:
- Int* pj=new int(30);
- Myclass Mine;
The other difference is that there is no equivalent toC++ delete operator in C#. Instead, with C#, the .Net garbage collector periodically comes in and scans through the references in your code in order to identify which areas of the heap are currently in use by our program. It is then automatically able to remove all the objects that are no longer in use. This technique effectively saves you from having to free up nay memory yourself on the heap.
Program Flow
Program flow is similar inC# to C++. In particular, the following statements works exactly the same way as they do in C++ and have the same syntax :
For, Return, Goto, Break, Continue
New in C# : for each
(1)If Else Condition
If statement works exactly the same way and has exactly the same syntax in C# as in C++, apart from one point. The condition in each if and else clause must evaluate to a bool. For example, assuming x is an integer data types, not a bool, the following C++ code would generate a compilation error in C#.
- if(x)
- { }
If(x != 0)
{ }
This shows that how additionally type safety in C# traps error early.
While and do While
- Int x;
- While(x) //wrong
- While(x != 0) //OK
(2)Switch
The switch statement serve the same purposes in C# as it does in C++. It is however; more powerful in C# since you can use a string as the test variables, something that is not possible in C++.
In C# a switch statement may not "fall through" to the next statement if it does any work. Thus, while the following is legal in C++, it is not legal in C#:
- switch (i)
- {
- case 4:
- CallFuncOne();
- case 5: // error, no fall through
- CallSomeFunc();
- }
- switch (i)
- {
- case 4:CallFuncOne();
- goto case 5;
- case 5:
- CallSomeFunc();}
- switch (i)
- {
- case 4: // fall through
- case 5: // fall through
- case 6:
- CallSomeFunc();
- }
C# provides an additional flow control statement, for each. For each loops across all items in array or collection without requiring explicit specification of the indices.
- Foreach(double someElement in MyArray)
- {
- Console.WriteLine(someElement);
- }
In some cases you might wish to treat a value type as if it was a reference type. This is achieved by a process know as boxing.
Syntactically this just means casting the variable to an object.
- Int j = 10;
- Object boxobj = (object) j;
The usual reason for boxing a value in order to pass it to a method that expects a reference type as a parameter. You can also unbox value, simply by casting it back to its original type.
- Int j = 10;
- Object boxobj = (object) j;
- Int k = (int) boxobj;
(5) Structs
Structs are significantly different inC#. In C++ a struct is exactly like a class, except that the default inheritance and default access are public rather than private.
InC# structs are very different from classes. Structs in C# are designed to encapsulate lightweight objects. They are value types (not reference types), so they're passed by value. In addition, they have limitations that do not apply to classes. For example, they are sealed, which means they cannot be derived from or have any base class other than System.ValueType, which is derived from Object. Structs cannot declare a default (parameter less) constructor.
(6) Value Types and reference types
C# distinguishes between value types and reference types. Simple types (int, long, double, and so on) and structs are value types, while all classes are reference types, as are Objects. Value types hold their value on the stack, like variables in C++, unless they are embedded within a reference type. Reference type variables sit on the stack, but they hold the address of an object on the heap, much like pointers in C++. Value types are passed to methods by value (a copy is made), while reference types are effectively passed by reference.
(7)Classes
Classes in C# follow much the same principles as in C++, though there are a few differences in both features and syntax.
- Class MyClass : MyBaseClass
- {
- Private string SomeFiels;
- Public in SomeMethod()
- {
- Return;
- }
There is no access modifier on the name of the base class. Inheritance is always public.
A class can only be derived from one base class. If no base class is explicitly specified, then the class will automatically be derived from System.Object, which will give the class all the functionality of System.Object, the most commnly used of which is ToString().
InC++, the only types of class members are variables, functions, constructors, destructors and operator overloads, C# also permits delegates, events and properties.
The access modifiers public, private and protected have the same meaning as inC++ but there are two additional access modifiers available:
- Internal
- Protected internal
C++ requires a semicolon after the closing brace at the end of a class definition. This is not required in C#.
(8) Destructors
C# implements a very different programming model for destructors to C++. This is because the garbage collection mechanism in C# implies that:
Since it is not possible to predict when the garbage collector will actually destroy a given object, if you do supply a destructor for a class, it is not possible to predict precisely when that destructor will be executed. Because memory is cleaned up behind the scenes of C#, you will find that only a small proportion of your classes actually require destructors. C# has two-stage destruction mechanism:
The class should derive from IDisposable interface and implements Dispose () method.
The class should separately implements at destructor, which is viewed as a reserve mechanism in case a client doesn't need to call Dispose()
C# destructor looks, syntactically much like a C++ destructor, but it is totally different. The C# destructor is simply a shortcut for declaring a Finalize method that chain up to its base class. Thus writing
- ~MyClass()
- {
- // do work here
- }
is identical to writing
- MyClass.Finalize()
- {
- // do work here
- base.Finalize();
- }
In C# the programmer's decision to override a virtual method must be made explicit with the override keyword.
To see why this is useful, assume that a Window class is written by Company A, and that List Box and Radio Button classes were written by programmers from Company B using a purchased copy of the Company A Window class as a base. The programmers in Company B have little or no control over the design of the Window class, including future changes that Company A might choose to make. Now suppose that one of the programmers for Company B decides to add a Sort method to ListBox:
- public class ListBox : Window
- {
- public virtual void Sort(){"}
- }
- public class Window
- {
- // "
- public virtual void Sort() {"}
- }
"\class1.cs(54,24): warning CS0114: 'ListBox.Sort()' hides inherited member 'Window.Sort()'.
To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
To remove the warning, the programmer must indicate what he intends. He can mark the ListBox Sort method new, to indicate that it is not an override of the virtual method in Window:
- public class ListBox : Window
- {
- public new virtual void Sort(){"}
- }
(10)C# requires definite assignment
C# requires definite assignment, which means that the local variables, age, ID, and yearsServed must be initialized before you call GetStats. This is unnecessarily cumbersome; you're just using them to get values out of GetStats. To address this problem, C# also provides the out keyword, which indicates that you may pass in uninitialized variables and they will be passed by reference. This is a way of stating your intentions explicitly:
- public class MyClass
- {
- public void GetStats(out int age, out int ID, out int yearsServed) { }
- }
- MyClass.GetStats(out age,out ID, out yearsServed);
There is no conversion between the bool type and other types (specifically int).
C# Boolean values can not be treated as integer If you write a code like this
- if(BoolReturnFunction()) {}
However using assignment versus equality is not allowed, if you write:
- if( x = 4 ) {}
Constant value '4' cannot be converted to a 'bool'
If you write like this:
- if(Convert.ToInt32(x)=4) {}
Cannot implicitly convert type 'int' to 'bool'
(12) Exceptions
Exceptions are used in the same way in C# as in C++, apart from the following two differences:
C# defines the finally block, which contains code that is always executed at the end of try block irrespective of whether any exception was thrown. The lack of this feature in C++ has been a common cause of complaint among C++ developers. 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.
InC++, the class thrown in the exception may be any class. C#, however, requires that the exception be a class derived from System.Exception.
C++ syntax for catch:
- Catch () {}
- Catch { }
- Try {}
- Catch (MyException e) {}
- Finally {}
C# does not support function pointers. However a similar effect is achieved by wrapping references to methods in special forms of class know as delegates.
Delegates can be passed around between methods, and used to call the methods to which they contains reference, in the same way that function pointers can be in C++. Conceptually delegates can be used in a similar way to an interface with a single method. The main practical difference is that with an interface the method name is fixed, whereas with a delegate only the signature is fixed - the method name can be different
- // delegate declaration
- delegate void MyDelegate(int i);
- class Program
- {
- public static void Main()
- {
- TakesADelegate(new MyDelegate(DelegateFunction));
- }
- public static void TakesADelegate(MyDelegate SomeFunction)
- {
- SomeFunction(21);
- }
- public static void DelegateFunction(int i)
- {
- System.Console.WriteLine("Called by delegate with number: {0}.", i)
- }
- }
(14) Properties
MostC++ programmers try to keep member variables private. This data hiding promotes encapsulation and allows you to change your implementation of the class without breaking the interface your clients rely on. You typically want to allow the client to get and possibly set the value of these members, however, so C++ programmers create accessor methods whose job is to modify the value of the private member variables.
InC#, properties are first-class members of classes. To the client, a property looks like a member variable, but to the implementer of the class it looks like a method. This arrangement is perfect; it allows you total encapsulation and data hiding while giving your clients easy access to the members.
Properties are defined with property declarations. The first part of a property declaration resembles a field declaration. The second part includes a Get accessor and/or a Set accessor. In the example below, a class Employee defines a property Age.
- public class Employee
- {
- private static int age;
- public int Age
- {
- get (return age);
- set (age= value);
- }
- }
(15)Attributes and Metadata.
One significant difference between C# and C++ is that C# provides inherent support for metadata: data about your classes, objects, methods, and so forth. Attributes come in two flavors: those that are supplied as part of the CLR and attribute you create for your own purposes. CLR attributes are used to support serialization, marshaling, and COM interoperability. A search of the CLR reveals a great many attributes. As you've seen, some attributes are applied to an assembly, others to a class or interface. These are called the attribute targets.
Attributes are applied to their target by placing them in square brackets immediately before the target item. Attributes may be combined, either by stacking one on top of another.
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile(".\\keyFile.snk")]
or by separating the attributes with commas.
[assembly: AssemblyDelaySign(false),assembly: AssemblyKeyFile (".\\keyFile.snk")]
Custom Attributes - Custom attributes are user-defined attributes that provide additional information about program elements. For example, you might define a custom security attribute that specifies the permissions required by the caller to execute a procedure.
- [AttributeUsage(AttributeTargets.All)]
- public class DeveloperAttribute : System.Attribute
- {
- private string name;
- public DeveloperAttribute(string name)
- {
- this.name = name;
- }
- public virtual string Name
- {
- get (return name);
- }
- }
While there are a number of subtle traps waiting for the unwary C++ programmer, the syntax of C# is not very different from C++ and the transition to the new language is fairly easy. The interesting part of working with C# is working your way through the new common language runtime library, which provides a host of functionality that previously had to be written by hand. This article could only touch on a few highlights. The CLR and the .NET Framework provide extensive support for threading, marshaling, Web application development, Windows-based application development, and so forth.
Johan LennartssonPosted Mar 27, 2020, 6:14 AM
No such cross over course as mentioned below?
Johan LennartssonPosted Mar 9, 2020, 6:22 AM
I have problem with exactly this. I'm good at C++ but my current job requires C#. I would like a cross over course but can't find any in this direction, I found a few C# to C++ but that doesn't help me. I certainly disagree with the notion that "the transition to the new language is fairly easy".
Psuedon Y'MousPosted Jun 6, 2013, 3:41 AM
Oh, and also C# is essentially useless anywhere but Windows, and follows typical Microsoft "You're not smart enough to do this, so I'll do it for you without giving you the option to show me your credentials or shoot me in the face" philosophy.
kinjal patelPosted May 24, 2013, 2:28 AM
what is the differences between visual c# and visual C++ 2010? please tell me as soon as possible and mail on id [email protected]
Shashwat RohillaPosted Mar 19, 2011, 6:57 AM
nice article Gaurav! I want to ask you something. Does an algorithm implemented in C++ and C# have different speeds? Does C++ being lower in level makes it faster than C#? I am a user of www.topcoder.com. We have to create quick algorithms restricted to code size and memory allocation. I saw that around 90% of the coders have used C++ for their code, others were using Java. I was using C#. Why is it so? Can you please mail me at [email protected]
Rajesh PotnuruPosted Jun 17, 2010, 1:38 AM
what is meant by modelview control?
asma farrukhPosted Aug 22, 2009, 12:50 AM
i want to attend training classes of c sharp.did u tell me training classes timing?i m waiting for ur reply.
asma farrukhPosted Aug 22, 2009, 12:24 AM
hi me asma u wrote very good about wt's the difference b/w c sharp and c++.
raj unixPosted Oct 2, 2008, 1:11 AM
hi gaurav i'm ur new friend. can u tell me how can i make a good security hacker pls help