# C#, C++ and C

**URL:** <https://boards.straightdope.com/t/c-c-and-c/369957>\
**Category:** Factual Questions\
**Created:** [August 23, 2006, 11:05pm UTC](https://boards.straightdope.com/t/c-c-and-c/369957 "2006-08-23T23:05:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaxTheVool](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@MaxTheVool](https://boards.straightdope.com/u/MaxTheVool)\
**Post date:** [August 23, 2006, 11:05pm UTC](https://boards.straightdope.com/t/c-c-and-c/369957/1 "2006-08-23T23:05:51Z")

</div>

I’ve been programming professionally for 10 years or so, most of that in C, with a fair amount of C++ more recently, but, up until my present job, I’ve worked entirely on video game consoles. Now I’m working on Windows, and I’d like to write a simple little tool with some buttons and pulldown menus and so forth. Various smart people say that things like that are VERY easy to do in C#. However, (a) I don’t know a thing about C#, and (b) this tool would need to use our pre-existing networking and utility code, which is in C.

So:  
(1) How easy is it to learn C#, coming from a C/C++ background?  
(2) How easy is it to write a tool where the interface is in C# but it calls some number of C functions, or perhaps vice versa?  
Thanks

---

<div class="post-metadata">

**Author:** ![Taran](https://avatars.discourse-cdn.com/v4/letter/t/edb3f5/32.png) [@Taran](https://boards.straightdope.com/u/Taran)\
**Post date:** [August 23, 2006, 11:22pm UTC](https://boards.straightdope.com/t/c-c-and-c/369957/2 "2006-08-23T23:22:17Z")

</div>

C# is easy for someone who already knows a C-like language. The syntax is similar, the object model is basically C++ with managed memory, and it has all Microsoft’s various Visual tools behind it, so yay. It has more syntax than C, but less than C++. It is _allegedly_ easy to call into C; one of MS’s stated design goals for C# was to make writing native code easy on Windows (to differentiate it from Java and its miserable JNI, I presume). They have about a million tutorials on it on MSDN, anyway.

---

<div class="post-metadata">

**Author:** ![CandidGamera](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/candidgamera/32/2878_2.png) [@CandidGamera](https://boards.straightdope.com/u/CandidGamera)\
**Post date:** [August 24, 2006, 12:43am UTC](https://boards.straightdope.com/t/c-c-and-c/369957/3 "2006-08-24T00:43:41Z")

</div>

Can’t speak for #2 at all, but #1 - I agree. My background was C/C++ and I had no trouble picking up C# in a five-day training course. Syntax was easy. Development environment and the structure may take a little getting used to - Visual Basic experience would help a bit with that aspect.

---

<div class="post-metadata">

**Author:** ![Revtim](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/revtim/32/1042_2.png) [@Revtim](https://boards.straightdope.com/u/Revtim)\
**Post date:** [August 24, 2006, 12:50am UTC](https://boards.straightdope.com/t/c-c-and-c/369957/4 "2006-08-24T00:50:15Z")

</div>

I just went from a C/C++ background to C#, and I love it. The hardest part is learning all the stuff that C# already does that you had to do “by hand” with C/C++. There’s just assloads of functions built-in to the language, like XML and http functions.

Right now I’m working on a project whereby I need to make a .NET “wrapper” around functions in a device API that was written for C. Got it working today mostly, it wasn’t difficult. Basically, I made a managed C++ dll project in Visual Studio that contained an unmanaged class to call the C API functions, and I made a managed Class that created an instance of the unmanaged class, and called the functions of that unmanaged class. Then my C# project referenced the dll, instantiated the managed class and called its methods (which in turn called the unmanaged methods of the unmanaged class).

I think that sounds a lot more complicated than it actually is. Plus, there very well might be simpler and more direct ways of doing it; I’m pretty new to C#/.NET and would not be surprised one bit if I missed an easier solution.

---

<div class="post-metadata">

**Author:** ![TimeWinder](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@TimeWinder](https://boards.straightdope.com/u/TimeWinder)\
**Post date:** [August 24, 2006, 1:12am UTC](https://boards.straightdope.com/t/c-c-and-c/369957/5 "2006-08-24T01:12:16Z")

</div>

Also remember that if you’re using Visual Studio 2005, [C++.NET](http://C++.NET) is also an option (and a pretty good one). It gets all of the whiz-bang Rapid GUI and automation stuff in a language that’s only slightly different from what you’re used to (the .NET objects are garbage-collected, so many C++ construction/destruction metaphors don’t work with them), plus calls into C/C++ are basically free. It really is the best of both worlds if you’re already comfortable in C++.

The 2003 version of C++ is much less functional, and there are big changes between the two.

C# is much closer to Java than C++, but wouldn’t be hard to learn. [VB.NET](http://VB.NET) is also pretty close to Java, and more fun to learn. You can mock the: “Overridable MustOverride Private Sub Foobar(ByValue variablename as VariableType)…End Sub” over-the-top verbose syntax while still understanding all of the concepts. Plus VB has better (ok, “more”) Intellisense-style stuff in it: you can almost program-by-pulldown, and it will highlight syntax errors BEFORE you compile, which C# is weaker at.

But I’d still stick with [C++.NET](http://C++.NET)

---

<div class="post-metadata">

**Author:** ![cmkeller](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/cmkeller/32/118_2.png) [@cmkeller](https://boards.straightdope.com/u/cmkeller)\
**Post date:** [August 24, 2006, 1:32am UTC](https://boards.straightdope.com/t/c-c-and-c/369957/6 "2006-08-24T01:32:37Z")

</div>

I’ll echo the others - it’s very easy to program in C# if you know C/C++. The UI side is easier if you’re familiar with VB as well, but not too hard to get if you’re not.

---

<div class="post-metadata">

**Author:** ![Ms2001](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ms2001/32/18463_2.png) [@Ms2001](https://boards.straightdope.com/u/Ms2001)\
**Post date:** [August 24, 2006, 4:48am UTC](https://boards.straightdope.com/t/c-c-and-c/369957/7 "2006-08-24T04:48:30Z")

</div>

[QUOTE=Taran]  
The syntax is similar, the object model is basically C++ with managed memory  
[/quote]

Well, it’s really closer to Java. For example, in C# all class instances are allocated on the heap. There’s no stack-based allocation with automatic construction and destruction (thus no RAII pattern). There’s no multiple inheritance, only interfaces. Also, IIRC, class identity never changes during construction, whereas in C++ an object “grows” through every one of its base classes as the constructors are called, which makes a difference if any of the base constructors call virtual methods.

---

<div class="post-metadata">

**Author:** ![Taran](https://avatars.discourse-cdn.com/v4/letter/t/edb3f5/32.png) [@Taran](https://boards.straightdope.com/u/Taran)\
**Post date:** [August 25, 2006, 2:02am UTC](https://boards.straightdope.com/t/c-c-and-c/369957/8 "2006-08-25T02:02:56Z")

</div>

[QUOTE=Mr2001]  
Well, it’s really closer to Java. For example, in C# all class instances are allocated on the heap. There’s no stack-based allocation with automatic construction and destruction (thus no RAII pattern). There’s no multiple inheritance, only interfaces.  
[/quote]  
Heh, good point. “With managed memory” covers an astonishing variety of stuff. No copy constructors, no real destructors (finalizers don’t count), and of course no MI.

> [@](#):
>
> Also, IIRC, class identity never changes during construction, whereas in C++ an object “grows” through every one of its base classes as the constructors are called, which makes a difference if any of the base constructors call virtual methods.

:eek: I never knew it did that. How appalling. Thanks.
