C# vs C++/CLI : Is there any reason not to use C# for Windows applications?

Chaotic42

Lifer
Jun 15, 2001
34,545
1,707
126
Yes, I know... it's another C++ thread from me.

I'm messing around with an OpenGL application on Windows and I've been looking at C#, which I have a little experience in. I was originally going to write it in C++, but now I'm not sure if there's any real reason to use C++ in a Windows environment.

I'm still new to serious programming, and I may be oversimplifying things, but it seems to me that if you need speed, you would use C and if you need an easy development environment, you'd use C#. Am I crazy? Other than the fact that most CS students are comfortable with C++, is there any reason to mess around with C++/CLI when you could just use C#? And if you really had to have screaming code, wouldn't the natural choice be C or assembly?

Assuming that there was no learning curve, why would someone choose C++/CLI?
 

Absolution75

Senior member
Dec 3, 2007
983
3
81
If you're using OpenGL, I going to guess you need an application that performs well.

C++ is the way to go then. C++ is just as fast as writing C code (performance wise) and usually it is usually faster to write C++ and maintain it. Not a lot of assembly is written these days except for math libraries which tend to use intrinsics.


Last time I checked, there wasn't even a way to use C# with OpenGL without writing your own wrapper.
 

OneOfTheseDays

Diamond Member
Jan 15, 2000
7,052
0
0
C++/CLI is primarily intended to be used as a bridging language between higher level manage code (C#) and lower level C/C++ code.
 

Train

Lifer
Jun 22, 2000
13,583
80
91
www.bing.com
If you're using OpenGL, I going to guess you need an application that performs well.

C++ is the way to go then. C++ is just as fast as writing C code (performance wise) and usually it is usually faster to write C++ and maintain it. Not a lot of assembly is written these days except for math libraries which tend to use intrinsics.


Last time I checked, there wasn't even a way to use C# with OpenGL without writing your own wrapper.

Are you kidding? There have been C# wrappers for OpenGL for years. When I was doing some 3D rendering way back in 2006 there were like a dozen to choose from.

There were tests showing that you can get 98% of the speed of C++ using a C# wrapper. I'd imagine today the performance loss is insignificant.

Unless you are trying to write the next version of Crysis, C# is just fine for OpenGL work.
 

Graze

Senior member
Nov 27, 2012
468
1
0
Yes, I know... it's another C++ thread from me.

I'm messing around with an OpenGL application on Windows and I've been looking at C#, which I have a little experience in. I was originally going to write it in C++, but now I'm not sure if there's any real reason to use C++ in a Windows environment.

I'm still new to serious programming, and I may be oversimplifying things, but it seems to me that if you need speed, you would use C and if you need an easy development environment, you'd use C#. Am I crazy? Other than the fact that most CS students are comfortable with C++, is there any reason to mess around with C++/CLI when you could just use C#? And if you really had to have screaming code, wouldn't the natural choice be C or assembly?

Assuming that there was no learning curve, why would someone choose C++/CLI?


I would love to know what modern game engine that has been written in C or assembly?
I am willing to bet the answer to that is none!
 

Chaotic42

Lifer
Jun 15, 2001
34,545
1,707
126
I would love to know what modern game engine that has been written in C or assembly?
I am willing to bet the answer to that is none!

I think id used C up to Quake 3, but my question was more about general Windows apps as opposed to video games. Forget the OpenGL thing, my question isn't really about OpenGL as much as Windows programming in general. Let's say a web browser, or a map viewer, or a word processor.
 

Red Squirrel

No Lifer
May 24, 2003
70,155
13,566
126
www.anyf.ca
C# restricts you to one platform (yea yeah know there's mono and stuff, but not the same) while C/C++ if done right should compile in all platforms and run natively. No emulation, no virtual machine, none of that stuff. Native.

For that reason I stick with C++ and standard libraries like the STL. You also get more control over things like memory management.
 
Last edited:

BrightCandle

Diamond Member
Mar 15, 2007
4,762
0
76
C# being a higher level language is both more productive and quite possibly faster in a lot of scenarios. Modern JIT VMs are actually surprisingly good at extracting performance that a C++ compiler can not based on runtime knowledge. So its not universally true that C/C++ will outperform C#.

But what C and C++ both have in common is being truly native. This allows you to provide system DLLs, drivers, hook DLLs and programs for the use of other native applications. I wouldn't chose C++ for a general application, for that C# is much easier to develop and can easily achieve similar performance most of the time, no where I use C is where I can't use C#. There are quite a few specialist programs that aren't practical or are actually impossible in pure C# due to the fact its running in a VM.
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
I think the best thing to do is determine the type of application you want, then find the language and/or framework that bet fits your business needs.

C# with WPF is probably the standard way to write Windows applications. Notice I did not mention web app, or game, etc. Many people are saying WPF is dead, and I'm uncertain of it's future, but honestly it's still the best option today.

The only time I've seen C++/CLI used is if there is an SDK built for C++ and it needs to be used from Managed code (C#). For example SlimDX, the DirectX Wrapper for .Net. It's written in C++/CLI because DirectX SDK is only available for C++, and its using CLI to make it managed. But I can't see too many uses for C++/CLI. But it has its uses.

I couldn't see using C++/CLI for just a standard managed windows app. C# or VB.net is a better choice.
 

Graze

Senior member
Nov 27, 2012
468
1
0
I think id used C up to Quake 3, but my question was more about general Windows apps as opposed to video games. Forget the OpenGL thing, my question isn't really about OpenGL as much as Windows programming in general. Let's say a web browser, or a map viewer, or a word processor.

Quake 3 was developed a decade and a half ago. I wouldn't call it modern.
If you are developing for Windows I would go with C#. This language is a beauty to work with.


edit:// Don't use VB .Net for anything, ugh.
 

Train

Lifer
Jun 22, 2000
13,583
80
91
www.bing.com
C# restricts you to one platform (yea yeah know there's mono and stuff, but not the same) while C/C++ if done right should compile in all platforms and run natively. No emulation, no virtual machine, none of that stuff. Native.

Are there actually any C++ apps in the wild today that can truly be compiled across windows, OSX, and Linux without having branches in the codebase for each?

Only thing I can think of is maybe a console app that does nothing but read & write to the command line. If it needs file IO, each OS is different, if it needs a GUI, it's all different again.
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
If you are developing for Windows I would go with C#. This language is a beauty to work with.

edit:// Don't use VB .Net for anything, ugh.

Yep, you should only use C# for everything because Graze says so.
 

Merad

Platinum Member
May 31, 2010
2,586
19
81
C# is fine for OpenGL programs. Besides OGL wrappers I believe there are a few 3D engines which can be used with C# (OGRE is the only one I know of offhand).

Other than the fact that most CS students are comfortable with C++

That's a laugh. The average CS student might be able to write C++ code that compiles and perhaps seems to work, but I've met essentially none who write decent C++ code.

Your average CS student probably isn't comfortable with any language other than Java, and many aren't even competent with it.

And if you really had to have screaming code, wouldn't the natural choice be C or assembly?

I'm guessing that you have never really used C or assembly. Assembly is very difficult to work with. Using it on any large or complex program is a total nightmare. You'd only use it if you had absolutely no other choice. AFAIK the last games written entirely in assembly came out in the mid/late 90s.

C is also rather difficult to work with (compared to higher level languages). C++ has quite a lot of features that are quite helpful in complex projects - templates, the ability to do OOP/polymorphism, RAII, smart pointers, etc. IMO there isn't a lot of reason to stick with C unless your platform dictates that you have to (microcontrollers, etc). Of course the linux kernel is still entirely in C, but that's because Linus is a dick. I believe that the majority of the Windows codebase has been C++ for quite some time. Essentially all AAA game titles have been written in C++ for 10+ years.
 

Graze

Senior member
Nov 27, 2012
468
1
0
Yep, you should only use C# for everything because Graze says so.

Is there something wrong with your reading comprehension?

I said don't use VB.net and your interpretation was " use C# for everything"?
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
You said C++/CLI, so managed code, so you might as well use C# unless there is some existing block of C/C++ code that you want to port to .NET.
 

Chaotic42

Lifer
Jun 15, 2001
34,545
1,707
126
Your average CS student probably isn't comfortable with any language other than Java, and many aren't even competent with it.

Interesting. I've taken Java, but the CS program I'm in (this question isn't for school, I'm still in the basics) is very C++ oriented.

I'm guessing that you have never really used C or assembly. Assembly is very difficult to work with. Using it on any large or complex program is a total nightmare. You'd only use it if you had absolutely no other choice. AFAIK the last games written entirely in assembly came out in the mid/late 90s.

Well, I wasn't talking about writing entire programs in assembly. I understand that it's needed for embedded programming, but I was referring to specifically including assembly code for certain routines or algorithms where the programmer thought they could do better than the compiler.

C is also rather difficult to work with (compared to higher level languages). C++ has quite a lot of features that are quite helpful in complex projects - templates, the ability to do OOP/polymorphism, RAII, smart pointers, etc. IMO there isn't a lot of reason to stick with C unless your platform dictates that you have to (microcontrollers, etc). Of course the linux kernel is still entirely in C, but that's because Linus is a dick. I believe that the majority of the Windows codebase has been C++ for quite some time. Essentially all AAA game titles have been written in C++ for 10+ years.

I like C, but I've never been involved with a large project. Mostly GIS applications which don't have to be coded particularly well, and none of those were in C. I've read Linus's argument about C vs C++ in the kernel and he does come across as an ass (big surprise), but I don't know enough about the subject to feel one way or the other.
 

Merad

Platinum Member
May 31, 2010
2,586
19
81
Well, I wasn't talking about writing entire programs in assembly. I understand that it's needed for embedded programming, but I was referring to specifically including assembly code for certain routines or algorithms where the programmer thought they could do better than the compiler.

Inline assembly is a bit of a gamble, at least on PCs. First, the compiler can't do any optimizations on your asm, so you have to really know what you're doing to actually make code that ends up better than what the compiler can generate (compilers are generally much, much better at optimization than humans). Second, CPUs are not consistent. Your hand-coded asm may be blazingly fast on Intel architectures but slow on AMD. Or perhaps you tune your code on Ivy Bridge CPUs, but then discover when Haswell is released that Intel has made some changes so that your code chugs on those newer CPUs. Third, asm is ugly and maintaining it will not be pleasant. Plus, it's pretty likely that you'll only have a couple of people on any given team who really have the knowledge and ability to write decent asm.

I imagine that that degree of low level optimization is somewhat more common in the console gaming arena, where you're targeting a fixed platform.

tl;dr - most of the time there are probably tons of optimizations that are better than inline asm.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,620
4,537
75
edit:// Don't use VB .Net for anything, ugh.
But that's the beauty of .NET, isn't it? Once your routine is compiled to bytecode, nobody needs to care whether it was written in C#, VB.NET, F#, or Iron Python. (Unless they need to change it, of course.)

The next level up from inline assembly is "intrinsics". You provide the (usually SIMD) assembly commands, but the compiler manages registers and memory. I generally find intrinsics easier to read and use, unless you're very low on registers.

If you think you can do better than the compiler on assembly that doesn't use SIMD, you're likely wrong. ;)
 

Graze

Senior member
Nov 27, 2012
468
1
0
But that's the beauty of .NET, isn't it? Once your routine is compiled to bytecode, nobody needs to care whether it was written in C#, VB.NET, F#, or Iron Python. (Unless they need to change it, of course.)


Yes that is the beauty of it(or the curse, VB gets to stick around). Would you recommend people learn and code in VB today? I don't think so.
I recently worked on a project with a client who had his team coding in VB.NET.
It was not a pleasant experience having to be subjected to reading and sometimes writing code in that language.
 

exdeath

Lifer
Jan 29, 2004
13,679
10
81
If you think you can do better than the compiler on assembly that doesn't use SIMD, you're likely wrong. ;)

Done any VU1 or Cell SPU or any kind of SoC code lately?

Love people who say C/assembly is dead. Must be nice to use ready made drivers, libraries, SDKs, operating systems, etc with no idea where they came from...

Are smart phone firmware images built in Java, VB, C#? Lol
 
Last edited:

Chaotic42

Lifer
Jun 15, 2001
34,545
1,707
126
OP specifically stated Windows.
Perhaps you two had a private conversation about the DS that the rest of this thread was not made aware of :p

We did not ;)

I'm specifically talking non-game Windows apps. If I were to ever write a game, it would be in OpenGL in a GNU/Linux environment. It just seems like it's worth knowing how to write proper Windows apps and I'm wondering if it's worth learning C# or sticking with C++.

To be honest I'm not enjoying this whole managed code thing one bit. Dealing with these forms and .NET issues and whatnot isn't exciting, but I need to suck it up and learn. I like IntelliSense, but VS2010 doesn't have it for C++/CLI, so that makes it even worse. :p
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
Yes that is the beauty of it(or the curse, VB gets to stick around). Would you recommend people learn and code in VB today? I don't think so.
I recently worked on a project with a client who had his team coding in VB.NET.
It was not a pleasant experience having to be subjected to reading and sometimes writing code in that language.

Yes, I would. VB.net is perfectly fine. Maybe you don't like it, but there are many programmers who prefer it. Just as you prefer to use C#.