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!
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.
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*).
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.
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.
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.
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).
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 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.
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.
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).
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.
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.