Why C#

  • Thread starter Thread starter Bruce F
  • Start date Start date
Bruce said:
Why should I use C# programming rather than C++?
Does C# have advantages over C++?

C# is object oriented and strictly typed. It's used in the .NET
framework so it uses managed code and a garbage collected memory management.

If you like these features, C# would be a better choise.
 
C# is object oriented and strictly typed. It's used in the .NET
framework so it uses managed code and a garbage collected memory management.

If you like these features, C# would be a better choise.

Techincally, C++ is also object-oriented and strongly typed. It also
has some third-party GC frameworks available (e.g. Boehm GC).

And then of course there's C++/CLI...
 
Pavel said:
Techincally, C++ is also object-oriented

Yes, but it's not "pure" object oriented in the same manner as C# where
every data type is object oriented and code only exists inside classes.
and strongly typed.

But not strictly typed...
 
MC said:
I should add that if you are doing low-level systems programming, as
someone else pointed out, C++ (which would actually be barely more than
plain C in that kind of situation) would be advantageous.

The design philosophy of C# is actually derived more from Pascal than from
C. If you like raw pointers and unexplained crashes, look somewhere else
:)

"raw pointers and unexplained crashes"...well, C# can use pointers in
marked, unsafe code...

Now, unexplained crashes....what about those exceptions where the message is
simply, "Catastrophic Failure"..? Can't get an unexplanation better than
that! :)

Mythran
 
Yes, but it's not "pure" object oriented in the same manner as C# where
every data type is object oriented and code only exists inside classes.

Technically, in C# (and CLR in general), "everything is an object" is
still not true - unsafe pointers aren't objects, for one thing :)
But not strictly typed...

Yes, and neither is Java or C#. Any language which allows anything
like bad_cast or InvalidCastException at run-time is not strictly
typed.
 
Mythran said:
"raw pointers and unexplained crashes"...well, C# can use pointers
in
marked, unsafe code...

True, but here I think C# hits the sweet spot.

In C, it's almost impossible to avoid potentially unsafe constructs
like pointers.

In Java, you can't use a raw pointer even when you'd really like to.

In C#, you have the option, but you're explicitly aware that you're
doing something dangerous and can make the tradeoff between safety and
performance i an informed fashion.
 
MC said:
C# is vastly easier to use than C++ and vastly less error-prone,
without sacrificing computational power.

C++ is a rough prototype of a C-like object-oriented language; Java
and C# are the real thing, and I'll leave it others to explain the
(comparatively minor) advantages of C# over Java.

C++ isn't the ideal OO language, because it isn't 100% object oriented. OO
is just one of many programming styles C++ supports. So of course it
doesn't have that "clean, forces you down the blessed OO path" smell which
languages designed for, well, forcing the programmer down a particular path,
do.

If you need to run on more than just Windows or more than just PCs, C++. If
you need to talk to hardware, C++. If you need to crunch bits, C++ (C#
doesn't even know that bitwise-and generates a result the same size as its
input).

If you want to do frilly WPF or web stuff or database queries, C#. The
language is simpler which makes it much easier to design tools to work with
it, so there are many more wizards for C#.
 
While I mostly agree with this analysis, there are some clarifications
to be made to that.

If you need to run on more than just Windows or more than just PCs, C++.

Mostly true, however, sometimes Mono provides reasonable cross-
platform capabilities, and high-level stuff is so much easier (and
faster) to do in C# that it is often worth it. Be wary of UI, though,
as in my experience WinForms support in Mono is still rather flaky
(and WPF is nonexistent).
 If you need to talk to hardware, C++.  If you need to crunch bits, C++ (C#
doesn't even know that bitwise-and generates a result the same size as its
input).

Also generally true, however, it's not that often that bit crunching
is all a piece of software has to do; and when it's just a single
routine in a large and otherwise high-level (and therefore C#) app,
it's often less trouble to code it directly in C#. Language provides
all necessary constructs for low-level performance tweaks (unchecked
pointers, unions, and stackalloc - this essentially covers the
entirety of what C has to offer), so it is quite possible to do
creative low-level optimization if needed. Of course, for a large low-
level library, it still makes more sense to do it in C++ (and wrap
using C++/CLI for consumption from C#).
If you want to do frilly WPF or web stuff or database queries, C#.  The
language is simpler which makes it much easier to design tools to work with
it, so there are many more wizards for C#.

At the same time, when the app is mostly doing low-level stuff, and
the UI is a very thin and marginally small layer, C++/CLI is often
good enough - it has a WinForms designer, the syntax is somewhat more
verbose but clear enough, and overall it can do everything C# can do.
Personally, I often use C++/CLI with WinForms for prototype stuff and
small throwaway utilities which have to deal with Win32 and COM APIs a
lot - writing a WinForms UI in C++/CLI is far easier and more
convenient than writing it in plain C++ and MFC, and the hassle of
dragging in something decent such as Qt is often just not worth it.
 
Mike Schilling said:
In C, it's almost impossible to avoid potentially unsafe constructs like
pointers.

I second that!

This thread got me inspired to read 'C traps and pitfalls' again.
If you have not read it yet, I highly recommend it.

http://www.literateprogramming.com/ctraps.pdf

In comparison, it makes Kernighans paper 'Why pascal is not my favorite
language' look like a simple nosebleed.

Both essays serves to remind me why I like C# so much. :)

- Michael Starberg
 
Bruce F said:
Why should I use C# programming rather than C++?
Does C# have advantages over C++?
oooooooooooooooooooooooooooooooooooooo
You will be up to speed much quicker with C#.
Put a query on a C++ use group asking whether
C++ is just C plus OO added on, and watch them
all get hysterical, because that is just what it is. It
is like driving a rally in a T-Ford.

Zach.
 
Michael said:
Yes, and neither is Java or C#. Any language which allows anything
like bad_cast or InvalidCastException at run-time is not strictly
typed.

What about "as" (or some other TryCast operation)?
 
Pavel said:
While I mostly agree with this analysis, there are some clarifications
to be made to that.



Mostly true, however, sometimes Mono provides reasonable cross-
platform capabilities, and high-level stuff is so much easier (and
faster) to do in C# that it is often worth it. Be wary of UI, though,
as in my experience WinForms support in Mono is still rather flaky
(and WPF is nonexistent).

I guess I was using "need" in a stronger sense of "it HAS to work", which
implies some level of commercial support for the toolchain. That doesn't
have to mean proprietary, but it does rule out half-finished technology
demonstrations like mono.
Also generally true, however, it's not that often that bit crunching
is all a piece of software has to do; and when it's just a single
routine in a large and otherwise high-level (and therefore C#) app,
it's often less trouble to code it directly in C#. Language provides
all necessary constructs for low-level performance tweaks (unchecked
pointers, unions, and stackalloc - this essentially covers the
entirety of what C has to offer), so it is quite possible to do

But C++ templates beat the .NET JIT optimizer hands down in this area. C#
hasn't even got a complex number type, and it's impossible to have a fast
complex generic.
creative low-level optimization if needed. Of course, for a large low-
level library, it still makes more sense to do it in C++ (and wrap
using C++/CLI for consumption from C#).


At the same time, when the app is mostly doing low-level stuff, and
the UI is a very thin and marginally small layer, C++/CLI is often
good enough - it has a WinForms designer, the syntax is somewhat more
verbose but clear enough, and overall it can do everything C# can do.

I thought the C++/CLI Forms Designer was dead... maybe we're thinking of
different versions of Visual Studio. Anyway the Forms Designer is incapable
of generating code that meets any sort of C++ code organization practices
(because it's designed for partial classes, not interface/implementation) so
I consider it moot.
 
MC said:
As I often sum it up, "C# is C++ gone sane." :)

My understanding is that even the designer of C++ intended it only as
a rough prototype for trying out ideas, and expected it to be
superseded by better-designed object-oriented C-like languages.

Someone's been reading Bjarne's April Fools paper without noticing the date
;) Or choosing to quote from it anyway.
 
Zach said:
oooooooooooooooooooooooooooooooooooooo
You will be up to speed much quicker with C#.
Put a query on a C++ use group asking whether
C++ is just C plus OO added on, and watch them
all get hysterical, because that is just what it is. It
is like driving a rally in a T-Ford.

Zach.

C++ adds a lot more to C than just object oriented syntactic sugar. And
don't even try to say that C# generics are the same as C++ templates.
 
Why should I use C# programming rather than C++?
Does C# have advantages over C++?


C# code is strictly-typed, object-oriented and has a well-defined
grammar unlike C++.

Well-written C# code is easy to understand and serves as the living-
documentation of the system.

Regards,
 
strictly-typed? Yes unless you turn it off, but same with C++
object-oriented? True
well-defined grammar unlike C++? False, C++'s grammar is perfectly
well-defined (although more complex)

well-written code is easy to understand? True for both C# and C++.
code is sufficient documentation? False for both C# and C++


Why should I use C# programming rather than C++?
Does C# have advantages over C++?


C# code is strictly-typed, object-oriented and has a well-defined
grammar unlike C++.

Well-written C# code is easy to understand and serves as the living-
documentation of the system.

Regards,
 
Mark Rae said:
Yep, can't argue with any of that...

And I can't resist posting a few funny quotes...

C++ has its place in the history of programming languages. Just as Caligula
has his place in the history of the Roman Empire. - Robert Firth

C++ has shown that if you slowly bloat up a language over a period of years,
people don't seem to mind as much. - James Hague

If C++ has taught me one thing, it's this: Just because the system is
consistent doesn't mean it's not the work of Satan. - Andrew Plotkin

If you think C++ is not overly complicated, just what is a protected
abstract virtual base pure virtual private destructor and when was the last
time you needed one? - Tom Cargill

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when
you do, it blows away your whole leg. - Bjarne Stroustrup

Happy Coding
- Michael Starberg
 
Back
Top