37

I'm looking for a new language to learn after C++ and Java. I was going to try C#, but a bunch of people say its really slow because its a high level language. So why would anybody use C#? Isn't C++ much faster? Does it make development easier, but just have a slower final product?

Also, what can C# be used for? You use it with a lot of .NET stuff on windows, and with ASP.NET, but what are other situations where one would use C#? Will there be a lot of job opportunities for it?

gliderkite
  • 8,828
  • 6
  • 44
  • 80
Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
  • 21
    I disagree with the "blatantly offensive" closure. It isn't offensive to question something they have heard. I find no fault in the question. +1 from me (and a vote to re-open), for asking what is actually a sensible question. – Marc Gravell Apr 24 '09 at 20:15
  • 8
    I don't believe I have been adequately offended by this question. – spoulson Apr 24 '09 at 20:15
  • 1
    I didn't choose blatantly offensive, but I still clicked on the wrong reason for closure. Problem is, it's easy to miss the one you want and you can't rectify it once clicked. – Kent Boogaart Apr 24 '09 at 20:16
  • 5
    I voted to close as S&A. The question assumes the answer to another question: "Is C# slow". It's like asking if you still beat your wife. – Jon B Apr 24 '09 at 20:16
  • Sorry! I didn't mean to offend anyone! I just read something stupid on a website, I didn't mean to insult the language or anybody on this website! – Gordon Gustafson Apr 24 '09 at 20:17
  • 4
    Why was this closed? This is a question that a lot of people search for and the stackoverflow community has a lot of intelligent responses to why you would use C# over C++. I have personally spent hours looking for C++ and .NET benchmarks and would have benefitted from the responses given. – Dave L Apr 24 '09 at 20:17
  • 1
    Agreed. It's certainly not "blatantly offensive". – Brian Sullivan Apr 24 '09 at 20:18
  • C++ is also a "high level language". – James McMahon Apr 24 '09 at 20:18
  • As an aside: in terms of "high level" - it is directly comparable to Java: an intermediate code (not CPU bytecode) that runs with the help of a runtime and a sandbox/VM. Is Java fast enough? – Marc Gravell Apr 24 '09 at 20:19
  • 5
    @CrazyJugglerDrummer - don't worry about it - I don't think you've upset anyone really. We're actually a fairly friendly bunch, so don't get defensive. Just sometimes we get a little trigger-happy ;-p Welcome to StackOverflow; I hope you find it educational and useful. – Marc Gravell Apr 24 '09 at 20:20
  • 1
    Thanks Mark! That makes me feel a lot better. I will admit that I read a bunch of people dissing C# on a MacForums website, and I didn't mean to make the assumption that C# was slow or bad in any way. I promise to check my sources and do more research in the future! – Gordon Gustafson Apr 24 '09 at 20:24
  • @CrazyJugglerDrummer, don't let them sweat you for asking a question that seems pretty targeted toward getting an objective opinion. SO is a question and answer site. Consider asking a question as part of your research. I'd probably just put more qualifying statements in the question rather than not asking it altogether. – Michael Meadows Apr 24 '09 at 20:51

11 Answers11

77

Who exactly is this "bunch of people"? What are they comparing it against?

For the vast majority of things, C++ is not "much faster" than C#. It certainly has benefits in various situations, particularly where you want more deterministic memory handling, but in my experience the bottleneck in most applications isn't in places where C++ would help. As spoulson says, a lot of performance is in the design instead of the exact implementation - and there, it helps to be able to try different designs easily.

Why would we use C# when it's a bit slower than C++? Because it's generally reckoned (i.e. some disagree :) to be a lot easier to develop in without shooting yourself in the foot.

As for what C# can be used for... what do you want to use it for? Unless you want to develop drivers and kernels, it may well be fine for you. (Even OS development has some folks using C#...)

Job opportunities? Loads.

Downsides? Well, .NET itself is only available on Microsoft platforms. There's Mono, but it doesn't have quite the same degree of portability as Java (no doubt another "slow" language according to the same bunch of people).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 13
    I've also read some cases where C# can out-perform C++ in computation speed due to runtime optimizations done by .NET. – Will Eddins Apr 24 '09 at 20:19
  • 6
    You should add security to this list, buffer overflows are the biggest risk, that is how Mac/Linux and Windows boxes get compromised..immutable strings are a great thing – SQLMenace Apr 24 '09 at 20:21
  • 1
    Case in point by Guard: .NET's JIT compilation of MSIL is optimized for the installed platform. When compiling C++, it's optimized only for minimum requirements. – spoulson Apr 24 '09 at 20:22
  • @hasen j serverside Java is not slow, the GUI part (Swing etc) is what could be improved – SQLMenace Apr 24 '09 at 20:22
  • 9
    I'm not sure Jon Skeet should be allowed to post anymore, he's hogging all the Rep (good answer BTW) – Greg B Apr 24 '09 at 20:59
  • @SQLMenace: Immutable vs. non-immutable strings have nothing to do with buffer overflows. Managed access to memory, on the other hand... – Derrick Turk Feb 08 '10 at 22:53
  • @WillEddins I think these benchmarks are not reliable enough. In case of C++ it really depends on the compiler and the compiler features/options turned on. – Balázs Szántó Dec 23 '13 at 14:56
  • I disagree about C# development is easier than in C++. I am just rewrote/ported a little app to upload an update from C++/GNU/Linux. The things that drove me crazy: • structs! Impossible to get a size of a struct (do not tell about `Marshal.SizeOf` which wrongly works with an arrays) • Sizes! I can't take a size of an element, only a size of a type! If I changed type of element, I need to trace all the `sizeof`s • strings! It is impossible to change encoding, code just infested with conversion to UTF8! And why the default is fat UTF16? Also perhaps I didn't recall some minor problems. – Hi-Angel Dec 25 '14 at 10:03
  • 4
    @Hi-Angel: It sounds like you were trying to port the C++ code very directly into C#. That's a bad idea - they're different languages, with different idioms. Likewise if I tried to port a C# program directly into C++ I'd find all kinds of things missing or different. That doesn't say anything about the usefulness of C++ or C# - it just says that trying to use the idioms of one language in another is a bad idea. – Jon Skeet Dec 25 '14 at 11:02
  • @JonSkeet no, not even one I mentioned is C++ specific. How I can do not think of size of a struct when I am getting a bytes that needs to be converted to a struct and vice versa? How do not think of size of a element when e.g. because of the first problem I need to implement an own static method in struct to return its size? – Hi-Angel Dec 25 '14 at 11:11
  • 4
    @Hi-Angel: But in idiomatic C#, I can't remember the last time I *wanted* to get the size of a struct. For serialization purposes there are all kinds of other ways of doing things. If you're just dumping the raw bytes in C++, I wouldn't personally do that anyway, as it's rather brittle in the face of running on a different architecture etc. I'd either use something like Protocol Buffers for the data objects, or explicit serialization "by hand". Basically there are lots of solutions to serialization which *don't* require knowing the size of the struct. – Jon Skeet Dec 25 '14 at 11:16
  • In the face of running on a different architecture I have a packed structures with a known sizes of an each element. The server application is running on ARM btw :Ь Okay, I'll read about a serializationin in C# way. – Hi-Angel Dec 25 '14 at 12:09
  • 2
    @Hi-Angel: Known *size* doesn't help if values are stored in a different endianness though, does it? (Think about transferring from one machine to another.) Basically, dumping the contents of memory to disk (or network) has always been a brittle way of storing data - and also one which makes it harder to evolve the data structure. It's not particularly well supported as an approach in C#, because it's a bad idea :) – Jon Skeet Dec 25 '14 at 12:16
41

Code written in assembly can be blazingly fast. Why not just write in assembly?

Don't believe everything you hear. C# has been plenty fast for all my projects. Typically, performance is more a factor of design than raw platform performance.

spoulson
  • 21,335
  • 15
  • 77
  • 102
  • 34
    No way, that's too slow! Build everything in hardware! ;) – JP Alioto Apr 24 '09 at 20:12
  • 1
    no man, punch cards is the way of the future :-) – SQLMenace Apr 24 '09 at 20:18
  • 7
    On the side, I do all my computing in Rube Goldberg machines. – spoulson Apr 24 '09 at 20:19
  • 1
    I write everything in F# and it gets compiled down suitable for Parallel FPGAs, so it's essentially built in hardware :) (meant as a joke. I don't do that) – Mehrdad Afshari Apr 24 '09 at 20:25
  • When I saw the initial post, your response is almost verbatim what I thought in my head. Everything has a trade off here and there, and speed isn't always 100% important. If you have two cars where one maxes at 220mph and the other maxes at 200mph... there isn't really a problem there. Sure one is slower, but not by much. Meanwhile the 200mph car is easier to drive, more available, and costs less than the other. What's the tradeoff really worth? Well, that depends on what you need. – Matt Kenefick Apr 18 '17 at 18:27
  • Real programmers code in hex. – rxantos Oct 31 '17 at 02:34
23

My last company was founded by 5 C++ veterans with 15+ years experience each. They spent over a month building a certain Windows service. One of them found and dabbled in C#. Within a week he'd gotten further than the collective had in their month. Shortly after, they all switched to C#.

Why C# if it may perform slower: what price do you put on that degree of rapid development?

Dinah
  • 52,922
  • 30
  • 133
  • 149
22

I'd have to say the people you were talking to simply don't know what they're talking about. Plain and simple.

Many enterprise level applications are built on top of C# and other .Net languages. There is nothing inherently slow about them. Yes the tend to have slower startup times but that's pretty much where it ends.

I noticed you mentioned Java in the list of languages. If you're comfortable with the speed of Java, C# will not present any issues. Generally speaking C# performs at least as well as Java on many different types of bench marks.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
7

Why should it be slow ? Indeed, C# is compiled to 'Intermediate Language', which is JIT'ed at runtime, but this can give you a performance advantage, since the runtime can generate the most optimized for the platform the code is running on ...

Depending on the application that you want to write, the 'speed' of the language will have a minor impact. The performance of your application will mostly be determined by the way you design your application, if you make good uses of the tools / technologies you use , etc ... Sure, C# is not a silver bullet, and there are projects where you shouldn't use it, simply because it is not the right tool for the job, but it will do just fine for most of the business / enterprise app's.

Frederik Gheysels
  • 56,135
  • 11
  • 101
  • 154
  • It will give you development time and debugging advantage. But it will not give you any performance advantage over C/C++. – rxantos Oct 31 '17 at 02:35
6

I haven't found many instances where C# isn't a good choice of languages, and .Net (or Mono) isn't a decent platform. Notable exceptions being kernel level development, or drivers. There are plenty of areas where low level, and raw performance is needed. For most, if no all business, or Enterprise applications C# is one of the better choices for development. It's well supported, works with many other systems, libraries, communications channels and components already available not to mention being a fairly nice language (esp. 3.5) to work with.

ASP.Net wasn't a bad platform, I generally find the object/control stack to be one of the short comings for complex interactions. I think ASP.Net MVC fits better for more scalable web based applications. Just the same, it's better than many other systems I've worked with in the past.

In terms of service layers, and even GUI development it's pretty nice. I've got a lot more experience in web based applications, and service/communications/business layers than with desktop GUI applications, so can't comment much there. I feel a lot of GUI development is more about the IDE/Toolkit, than the particular language.

As to slow, you specifically list Java, in most instances, C#/.Net is as fast or faster than Java. IMHO development specifically goes smoother with C# (Visual Studio) over Java (Eclipse). For web-based apps, I like ASP.Net MVC (and even ASP.Net) over Swing. That's just me though.

Tracker1
  • 19,103
  • 12
  • 80
  • 106
  • Just want to comment, that I would seriously consider Go or Rust in addition to C#, Node/JavaScript and Python in terms of systems I would use today. I'm more inclined to reach for Node/JS first and Rust second these days given the option. – Tracker1 Sep 04 '19 at 21:06
4

The people you talked to don't know what they are talking about. C# is a very similar language to Java, all told; it has most of the same benefits and drawbacks. The way it all works is pretty similar (Java/C# is compiled into an intermediate language/bytecode that is interpreted or JIT compiled to native code, with various similar optimizations that you don't need to worry about as a programmer). It's used in a lot of the same situations as Java, and is really aimed for the same market. It's moving a lot faster and bringing in a lot of innovation as a language, but it's (in practice) pretty much Windows-only, if that's a concern of yours. The job market is similar. Both are very popular languages.

As for a language to learn, I would suggest something DIFFERENT. You say you know C++ and Java; C# shouldn't be hard to pick up. Potential employers will know this. Try Scala or Python. Both will give you some new perspectives on things (C# not so much), and make you a better programmer by teaching you new ways to think, rather than just adding another tool to your box.

Adam Jaskiewicz
  • 10,934
  • 3
  • 34
  • 37
3

C# is not always slower - in many cases, it can perform just as well as any language you listed. Usually the algorithm has more to do with the speed than the choice of language.

However, C# is very expressive, and has a great base class library to work with, and super-fast compilation. This means that it's very easy to work with, and can allow you to be much more productive than many other languages, especially C++. For example, I just had a small project that I would have budgeted 1 man-week for in C++, and we finished it up in less than a day in C#, mainly because the base class libraries simplified so many of the tasks.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
0

one big reason to use C# over C++.........look at all those buffer overflow problems that C++ has where people take over your machine because they inject executable code in your strings or memory leaks....garbage collector is quite handy IMHO

SQLMenace
  • 132,095
  • 25
  • 206
  • 225
0

Even if C++ is faster to run, the difference isn't big on today's computers. That's why on mobile devices where cpu and memory are rather limited, C/C++ is still widely used.

But think about development times instead. If it takes 2 months using C++ and 1 month using C#, which would you go for? And when big modifications/refactoring need to be done, a higher level language makes it significantly faster and easier!

Lauri Larjo
  • 314
  • 1
  • 8
0

I Would definitely use C#. It is actually average speed, and if you liked java, it's almost exactly the same, as they are both based off C++.

Flynn
  • 21