C# and C++

closed account (D3pGNwbp)
I've been wondering; what are the differences between C# and C++? And how different is C# from C++? IDK, but it looks to me to be pretty much the same(same syntax, it uses classes, and most other things that C++ uses). So, what does C# offer or do that makes it different from C++? Just kinda curious, and thanks in advance!
There are quite a few syntax differences between the two, a quick google gives me this http://msdn.microsoft.com/en-us/library/yyaad03b(v=vs.71).aspx
As for what features one offers over the other I don't know, (other than memory management I suppose).
Mostly the same but it feels like java mixed with c. Memory is auto-managed and there are no explicit pointers.
closed account (D3pGNwbp)
Thanks guys! Would you say that C# is more difficult to learn than C++, about the same, or easier?

Edit: What would you say a good second language would be to learn when it comes time? I just know a little bit of C++ at the moment.
Last edited on
C# is a completely different beast compared to C++, and I would say that if you want to learn both, go in assuming they have no relation at all. if you go in thinking they're related, it will only make things hard for you.
@DeXecipher
C# does support pointers, but methods and blocks using them have to be marked with the 'unsafe' keyword. There is also the IntPtr class struct (class and struct have semantic difference in C#). Code that uses it doesn't have to be marked unsafe. It's useful for interfacing with unmanaged code, it can be treated like a pointer-to-void (void*).
Last edited on
closed account (3qX21hU5)
class and struct have semantic difference in C#


For example Structs are value types where as class's are reference types. So all assignments to a variable of struct type creates a copy of the value being assigned. Also the this keyword is different in structs along with other things.

Like others have said don't go into learn C# thinking it is like C++ because it will cause you some troubles. Because on the surface everything looks about the same but there are subtle and major differences between the two.
Last edited on
C# and C++ are two different languages and should be treated as such. The syntax may look similar, however, they are very different.

Just to give you a rough idea:
- C# mostly uses delegates as oppose to function pointers. However, there are similar things you can use for function parameters. like the ref and out keywords.
- C# has its own memory management called a garbage collector. You don't use destructors either and calling Dispose() on an object is considered bad practice. If you need destructor functionality, you use a finalize block on our try / catch block.
- In C++, classes and structs are only different in their default accessors. in C# they are different in the way they get allocated in the memory; (C# classes are ref types going on the heap, structs are value types going on the stack).
- Both language have very different libraries with parts that cater to much different things.
- They have some very different features. For example; C# has the linq syntax and functionality, while C++ has backward compatibility with C.
- In C# everything is an object, everything... including: int, char, double, etc. They all inherit the functions; Equal(), ToString(), GetType() from their base class.
- In C++ a class interface is where you declare the class members, however there is no compiler restrictions. In C#, interface is a keyword and the compiler will not let you do anything other then declarations within it. However this also takes part on how the inheritance works.
- C++ supports multiple inheritance, while C# does not, it uses interfaces instead. However they are technically very similar. In practice, you should always declare your class members in a interface and implement them somewhere else. In C++ you do this by using a header file and a cpp file. In C# you use the interface keyword (also usually in a separate file) and do the implementation in the class using said interface.

I could go on, though I think that's enough to see how different they are.

Edit:
The real thing to learn is not the language... that's just a tool. and you should know how to use many tools. The main thing to learn is; how to program; like OOP, design patterns, and so forth.
Last edited on
Oria wrote:
there are similar things you can use for function parameters. like the ref and out keywords.

I may be misunderstanding, but ref and out are not used for passing functions in parameters, they're used to pass writeable references to a function to allow for output parameters (they differ in that out parameters don't have to be initialised before use).

You don't use destructors [in C#]

I think you're thinking about Java. C# supports destructors for reference types in much the same way that C++ does, although in C# the destructor is converted to a Finalize method: http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx

In practice, you should always declare your class members in a interface and implement them somewhere else

Again, I might be misunderstanding you, but I don't agree that you should have a separate interface for each class, if that's what you're suggesting. Only when more than one class should implement the same behaviour (but it doesn't matter how: if it matters how, you use a base class). I mostly use interfaces to describe what can be done to an object; for example, my genetic algorithm class operates on Evolvable objects; in a game I had a Controllable class for the player, etc.
I am currently learning C#...About 7-8 months ago, I started to teach myself HTML/CSS. A couple months later, started to learn JavaScript, as well as Jquery, JSON, etc. I also dabbled a bit in Python, and PHP. Then, about a couple months ago, I started learning C# (Also going to eventually try to learn Java, since it doesn't seem extremely different from C#)...

I know how powerful and important C++ is, but I don't know what it is about it, it just gives me hangups whenever I've gotten into it in the past. I had a summer school class in C++ when I was in 9th or 10th grade (I'm in my mid-20's right now) that I had issues with (still passed though).

I'm hoping that getting better at the other languages will get me better prepared when I want to get into this one seriously.
closed account (o1vk4iN6)
C# and C++ are two different languages and should be treated as such. The syntax may look similar, however, they are very different.


This.

See so many people go from C# to C++ creating memory leaks using the new operator cause they just assume that they behave the same way.
C++ supports OO(object oriented)
C# supports OO too, but its better
C++ has memory leaks
C# has high memory usage
C# is easier and best for making games

This is what I got from my knowledge, correct me if I am wrong.
C# can leak memory and C++ can have high memory usage. Also, C# might be "easier" sometimes, but C++ is more commonly used for games. As for whether C++ or C# OOP is better, I think that's subjective, although certainly there are things from both languages that are done better in one than in the other (e.g. C# has interfaces, C++ has multiple inheritance). You can't really make general, absolute statements like these about programming languages.
chrisname wrote:
I may be misunderstanding, but ref and out are not used for passing functions in parameters

You did misunderstand me for that part. I meant as in replacement to C++ pointers and references in function parameters. Like you would do: void something (int& value, double* othervalue). In C# you could do: void something (ref int value, out double othervalue).

chrisname wrote:
I think you're thinking about Java. C# supports destructors for reference types in much the same way that C++ does

Your right about the destructors, you can have them. However, you should only ever do when you really need to. Just like I said earlier about calling Dispose(). Most of the time, destructors are to do a final clean up, however, you shouldn’t be cleaning up your resources in C#, you should let the GC do that. If you wish for it to call a final function member; do that in the finalize block of the try/catch.

chrisname wrote:
Again, I might be misunderstanding you, but I don't agree that you should have a separate interface for each class

I’ve learn to do the implementation away from the declaration when I can in C++. But yeah in C# you don’t always do that, mainly that you cannot do the implementation outside of a class. Though on a bigger project, you should be using interfaces frequently.
Topic archived. No new replies allowed.