Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Difference between C# OVER C++?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Soap360   Members   -  Reputation: 100

Like
0Likes
Like

Posted 17 January 2011 - 11:13 PM

I've been writing programs from quite a while now, just recently I began learning game programming with a few buddies. I'm looking forward to start XNA, which uses C#, instead of SDL, which uses C++. So I bought myself a C# book... and everythings the same as C++? I've been reading through 1/4 and skimming through 2/4.. and most of the concepts appear to be the same. Like, I know there's console.writeline() over cout<< and all that but the OOP concepts are almost identical.

So my question is, what concepts should I learn in C#. Should I even go through the book if I'm good with C++? Sorry for rambling, the point Im trying to get across is, what does C# have that C++ doesn't that is vital in object oriented programming.

All help is greatly appreciated :)

Sponsor:

#2 CRYP7IK   Members   -  Reputation: 440

Like
0Likes
Like

Posted 17 January 2011 - 11:30 PM

So my question is, what concepts should I learn in C#. Should I even go through the book if I'm good with C++? Sorry for rambling, the point Im trying to get across is, what does C# have that C++ doesn't that is vital in object oriented programming.

All help is greatly appreciated :)


Well, they are both OOP languages, what did you expect?

The only major difference between them is one is managed and writing code that uses pointers is considered 'unsafe' and therefore has a lot less bug potential. Oh and getters and setters are easier to write in C#.
To accomplish great things we must first dream, then visualize, then plan...believe... act! - Alfred A. Montapert
Gold Coast Studio Manager, Lead Programmer and IT Admin at Valhalla Studios Bifrost.

#3 laztrezort   Members   -  Reputation: 788

Like
0Likes
Like

Posted 17 January 2011 - 11:31 PM

Maybe this would be a good read?:
C# FAQ for C++ Programmers

#4 Dan Mayor   Members   -  Reputation: 1626

Like
0Likes
Like

Posted 17 January 2011 - 11:36 PM

Hey Soap,
I also struggled with this exact same issue when I first migrated into the C# Programming language from C++. There is no real short answer to your question but maybe I can help with a few small theories.

Firstly,
C# is a programming language designed to look feel and operate almost identically to the C++ syntax. C# keeps most if not all of the same syntax and basic theory of C++ but wraps it all into a simple and effective object hierarchy. These core objects are provided through standard libraries, such as the System objects and third party libraries such as Microsoft's .NET and XNA Libraries. To kinda bring this into perspective think of C++ Header files. Including conio.h gave you cout, in C# you Reference the System library and simply call "using System" which gives you access to the Console object.

Brings us to Library Referencing,
Next, an important difference is how you link to external libraries. In C++ You are used to including header files which contain decelerations and either linking a dynamic library or compiling the source file that defined the libraries functionality. C# takes library linking (referencing) into account in it's core design to ease this entire concept. There are no longer any header files, in C# you simply add a reference to the dynamic library that contains the methods you want and use the name space in your source. This works for linking thousands of standard and Microsoft libraries that will help you with all of your Microsoft Platform game writing needs. If and when you need to write a reusable library it is much easier then C++. You no longer need to worry about importing and exporting memory locations of the functions you are looking for. The C# Compiler will automatically write all that into your executable for you.

Dynamic Memory Allocation,
If you have ever done some serious coding you would also be familiar with allocating and releasing dynamic memory. Again this is all accounted for in the core concept of the C# programming language. The C# programming language has a built in garbage collecting system that will automatically release memory blocks that are no longer referenced by any other objects. The easiest way to explain this from one coder to another is with an example. Take C++ and say you where building a character object which requires a dynamic "Weapon" member. When your character equips a weapon you create a new object in memory and save a pointer to that location. When the player removes the weapon you want to delete that weapon object from memory, so you would delete the object and set the pointer to null. This is common practice and as such has been built directly into C#. C# No longer uses pointers, instead ALL objects are treated internally as references to dynamic memory. Therefore simply creating the object automatically creates it in dynamic memory, the variable that you used to create this object IS the pointer. When you set that object to null the dynamic memory is automatically free'd by the garbage collector. In C++ you need to worry about WHEN you free dynamic memory and not to leave any lingering pointers to a dead memory slot. C# again handles all of this for you. To pass a "pointer" to your variable you simply pass the variable itself, since ALL objects are dynamic in nature you are always passing a reference to the single instance in memory. You can then set any references to null when you are done with them, the garbage collector will only free memory that is no longer referenced by any other objects.

That's really the main things that you will need to come to terms with to start becoming more efficient with the C# Programming language. If you (or any other readers for that matter) have any questions please feel free to send me an email and I will see what I can do to help you out.

E-Mail: Dan@Digivance.com

Digivance Game Studios Founder:

Dan Mayor - Dan@Digivance.com
 www.Digivance.com


#5 MrDaaark   Members   -  Reputation: 3503

Like
0Likes
Like

Posted 18 January 2011 - 05:17 AM

Read Petzold's free E-Book, Dot Net Book Zero. It deals with moving from C++ to C#. There are differences. C# is not C++.

#6 Mike.Popoloski   Members   -  Reputation: 2444

Like
3Likes
Like

Posted 18 January 2011 - 09:22 AM

They are not even remotely the same language. They share a superficial similarity in syntax, and certainly some programming patterns look almost identical in both, but idiomatic code between the two is radically different.
Mike Popoloski | Journal | SlimDX

#7 RivieraKid   Members   -  Reputation: 218

Like
0Likes
Like

Posted 18 January 2011 - 09:56 AM

... C# No longer uses pointers, instead ALL objects are treated internally as references to dynamic memory. Therefore simply creating the object automatically creates it in dynamic memory, the variable that you used to create this object IS the pointer. When you set that object to null the dynamic memory is automatically free'd by the garbage collector. In C++ you need ...



to be more specific. The garbage collection will clean up when you least expect it. On xbox 360 and win phone 7 the .net garbage collector fires everytime 1 MB of memory is allocated. GC Collect is an expensive operation so you should be just as careful when creating objects as in C++, perhaps more so than C++ because of the additional overhead of the GC.

#8 Josh Petrie   Moderators   -  Reputation: 2287

Like
5Likes
Like

Posted 18 January 2011 - 10:39 AM

C# No longer uses pointers, instead ALL objects are treated internally as references to dynamic memory

That's not true -- pointers are available in unsafe code (but their use should be discouraged). I don't precisely know what you intended by saying "dynamic memory," but there is a heap and a stack just like in C++ and not all objects are located on the managed heap (which is where the relocatable objects that can be moved by the GC are stored).

the variable that you used to create this object IS the pointer.


A pointer in C++ is implemented as an address, but in C# a reference is not an address.

When you set that object to null the dynamic memory is automatically free'd by the garbage collector.


This is perhaps the most dangerous statement. Setting a reference to null may or may not have any impact on the collectability of the object being referenced. It is an implementation issue; it is certainly not a language guarantee that nulling out a reference triggers the collector or puts the object in a list of available collectable objects. In .NET, for example the GC operates by finding paths from known root objects -- anything not rooted is collectable. Most CLR GCs operate in this fashion. In .NET, it is true that in this code sample:
void f() {
object o = new object();
Console.Write("Hello, World!");

}
'o' is eligible for collection immediately after the semicolon on the first line, since it is no longer referenced. Setting o to null is not required and will in fact have no impact on the collector.


EDIT: stupid code editor

Josh Petrie | Lead Tools Engineer, ArenaNet | Microsoft C++ MVP


#9 deepdene   Members   -  Reputation: 292

Like
0Likes
Like

Posted 18 January 2011 - 04:53 PM

From a more general point of view that isn't a language specific difference, Visual Studio provides alot of tools for doing GUI editing, database hookups etc. So you can create forms in WPF/Winforms which are GUI frameworks, can create entity framework hookups which is a object relational tool etc, plus many other tools I can't think of.

C++/CLI has some of these tools but its not as tightly fitted. C++/CLI being a standard produced from Microsoft to make some additions to C++ to provide better syntax for integration into a managed environment. The .NET framework is massive, it takes the kitchen sink approach while in C++ the standards committee took the approach a 3rd party would provide additional functionality outside a core set of functionality.

Also from a language point of view, there have been many extensions lately towards being a hybrid functional/procedural language with frameworks like Linq being added, lamda functions etc. Functional language features making it possible to add some constructs to potentially make it easier to scale certain things. i.e. they added the parallel tasks library and parallel linq to provide features such as iterating over large datasets concurrently using a lambda function etc.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS