473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

"Poll" Has C# Generally Replaced C++

Is it your general opinion that C# is generally designed/intended/ready to
replace C++? I know the answer is not black and white but please repond YES
or NO (add any comments if you would like)

I just thought it might be interesting to get a tally. This is related to a
previous thread. So far we have two NOs from "aa" and Bo Persson.

--
Greg McPherran
www.McPherran.com
Dec 10 '05 #1
71 3070
By the way my vote is YES even though I know C++ can have advantages and is
still valuable/required for many purposes.

--
Greg McPherran
www.McPherran.com
"Greg" wrote:
Is it your general opinion that C# is generally designed/intended/ready to
replace C++? I know the answer is not black and white but please repond YES
or NO (add any comments if you would like)

I just thought it might be interesting to get a tally. This is related to a
previous thread. So far we have two NOs from "aa" and Bo Persson.

--
Greg McPherran
www.McPherran.com

Dec 10 '05 #2
"Greg" <gm@mcpherran.com> wrote in message
news:EE**********************************@microsof t.com...
Is it your general opinion that C# is generally designed/intended/ready to
replace C++? I know the answer is not black and white but please repond
YES
or NO (add any comments if you would like)


No, it is not.

In a few years it may be the language most often used to _build_
applications for Windows, but the operating system and its drivers will
continue to be written in C or C++ for the near term at least.

Regards,
Will
Dec 10 '05 #3
Greg wrote:
Is it your general opinion that C# is generally
designed/intended/ready to replace C++? I know the answer is not
black and white but please repond YES or NO (add any comments if you
would like)

I just thought it might be interesting to get a tally. This is
related to a previous thread. So far we have two NOs from "aa" and Bo
Persson.


No. C# is a language designed, intended and ready to replace Java (and well
on it's way to doing so).

C++ is still better at nearly all the things C++ has always been better at.

-cd
Dec 10 '05 #4

"Greg" <gm@mcpherran.com> wrote in message
news:EE**********************************@microsof t.com...
Is it your general opinion that C# is generally designed/intended/ready to
replace C++? I know the answer is not black and white but please repond
YES
or NO (add any comments if you would like)

I just thought it might be interesting to get a tally. This is related to
a
previous thread. So far we have two NOs from "aa" and Bo Persson.

--
Greg McPherran
www.McPherran.com


From a pure language point of view....
Designed & Intended to: NO
Ready to: NO
Some might say it's "ready to", but I guess this is only a personal
interpretation and only reflects their own requirements. There are simply
too many problem domains that aren't covered yet (although, this is not
strictly a language issue) , and I don't even consider the lack of platforms
availbility.

Willy.
Dec 10 '05 #5
5 NOs and only one YES (mine).

I guess my next question is then why would Microsoft call it C# which is:
C++
++ ?

The implication is that C# is to C++ as C++ was to C.

--
Greg McPherran
www.McPherran.com
"Greg" wrote:
Is it your general opinion that C# is generally designed/intended/ready to
replace C++? I know the answer is not black and white but please repond YES
or NO (add any comments if you would like)

I just thought it might be interesting to get a tally. This is related to a
previous thread. So far we have two NOs from "aa" and Bo Persson.

--
Greg McPherran
www.McPherran.com

Dec 10 '05 #6
Hi Greg!
Is it your general opinion that C# is generally designed/intended/ready to
replace C++? I know the answer is not black and white but please repond YES
or NO (add any comments if you would like)

I just thought it might be interesting to get a tally. This is related to a
previous thread. So far we have two NOs from "aa" and Bo Persson.


Your question is a bit confusing...
You need to ask the "right" question to get the "right" answer!

For example: Will C# replace C++ in some small areas like UI?
--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Dec 10 '05 #7
No. C# is really VB with curly braces. C++ is a very different language.

Dec 10 '05 #8
JAL
For application programming C++ seems to be losing some ground according to
InfoWorld's Developer's survey:

java +34%
C# +23%
C/C++ -3%

"Greg" wrote:
Is it your general opinion that C# is generally designed/intended/ready to
replace C++? I know the answer is not black and white but please repond YES
or NO (add any comments if you would like)

I just thought it might be interesting to get a tally. This is related to a
previous thread. So far we have two NOs from "aa" and Bo Persson.

--
Greg McPherran
www.McPherran.com

Dec 10 '05 #9
"=?Utf-8?B?R3JlZw==?=" <gm@mcpherran.com> wrote in news:92F79F27-998F-4CF7-
98***************@microsoft.com:
5 NOs and only one YES (mine).

I guess my next question is then why would Microsoft call it C# which is:
C++
++ ?


Perhaps your next question should really be will C++/CLI replace C#? :))))
Dec 10 '05 #10

"JAL" <JA*@discussions.microsoft.com> skrev i meddelandet
news:90**********************************@microsof t.com...
For application programming C++ seems to be losing some ground
according to
InfoWorld's Developer's survey:

java +34%
C# +23%
C/C++ -3%
And they can't differentiate between the languages either. Who knows,
perhaps this means C -10%, C++ +7%?
Bo Persson


"Greg" wrote:
Is it your general opinion that C# is generally
designed/intended/ready to
replace C++? I know the answer is not black and white but please
repond YES
or NO (add any comments if you would like)

I just thought it might be interesting to get a tally. This is
related to a
previous thread. So far we have two NOs from "aa" and Bo Persson.

--
Greg McPherran
www.McPherran.com

Dec 11 '05 #11
JAL wrote:
For application programming C++ seems to be losing some ground
according to InfoWorld's Developer's survey:

java +34%
C# +23%
C/C++ -3%


I would suspect a poll in InfoWorld to be very highly skewed towards IT-shop
development and not representative of commercial application development
which is still dominated very strongly by C++.

-cd
Dec 11 '05 #12
William DePalo [MVP VC++ ] wrote:
"Greg" <gm@mcpherran.com> wrote in message
[...]
No, it is not.

In a few years it may be the language most often used to _build_
applications for Windows, but the operating system and its drivers will
continue to be written in C or C++ for the near term at least.
And yet Microsoft experiments with a research operating system written
in C#.
Regards,
Will


Regards,
Andre
Dec 11 '05 #13
On Sat, 10 Dec 2005 08:55:03 -0800, Greg wrote:
Is it your general opinion that C# is generally designed/intended/ready to
replace C++? I know the answer is not black and white but please repond YES
or NO (add any comments if you would like)


No.

It was designed to compete with Java, not C++. A different horse on a
very different course.

--
Bob Moore
http://bobmoore.mvps.org/
(this is a non-commercial site and does not accept advertising)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~
Do not reply via email unless specifically requested to do so.
Unsolicited email is NOT welcome and will go unanswered.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~
Dec 11 '05 #14
"Andre Kaufmann" <an****************@t-online.de> wrote in message
news:e6**************@TK2MSFTNGP11.phx.gbl...
And yet Microsoft experiments with a research operating system written in
C#.


Yup. I've seen at least a couple of videos on MSDN's Channel 9 on the
subject of ongoing research into operating systems and driver development
with managed code.

Still, my guess is that a manged o/s for some real-world purpose is a
half-decade away and for general purposes it is likely to be at least twice
as far off as that.

Of course, no one important shares his long range plans with me so I could
be all wrong. ;-)

Regards,
Will


Dec 11 '05 #15
Greg wrote:

I guess my next question is then why would Microsoft call it C# which
is: C++
++ ?


Are you *really* trying to understand marketing naming woodoo from a
pragmatic ant technical point of view ;-)

Arnaud
MVP - VC
Dec 11 '05 #16
For .NET 2.0, C++/CLI lets you do everything C# can + a lot more - so it's
definitely your better option.

For Longhorn, I am not so sure. There's no direct XAML support for C++. So
you are going to have to hand-code your UIs (and unless all your apps have
simple dialogs with 1-3 controls on them, that's a pain in the neck). Of
course, maybe they are working on XAML support for C++ too, and if so,
there's no reason to use C# :-)

--
Regards,
Nish [VC++ MVP]
"Greg" <gm@mcpherran.com> wrote in message
news:EE**********************************@microsof t.com...
Is it your general opinion that C# is generally designed/intended/ready to
replace C++? I know the answer is not black and white but please repond
YES
or NO (add any comments if you would like)

I just thought it might be interesting to get a tally. This is related to
a
previous thread. So far we have two NOs from "aa" and Bo Persson.

--
Greg McPherran
www.McPherran.com

Dec 12 '05 #17
I think C# is a general replacement for C++ even though C++ has its place.

C was a quick "throw-together" language at Bell Labs that took hold. C++ was
built on top of C an carried the syntax.

C# has superior syntax. In C/C++ both the pointer declaration and the object
of the pointer use a * . Also C# array are declared as a type rather than
type/context:

e.g. int[] x; v.s. int x[];

C# has the advantage of practical "non-rushed" thinking in its design.

Again. I don't deny that C++ has its place and can have advantages. However,
I think C# is the "general" replacement for C++. People may not fully realize
it yet. That is my opinion.
Dec 12 '05 #18

"Greg" <gm@mcpherran.com> skrev i meddelandet
news:F4**********************************@microsof t.com...
I think C# is a general replacement for C++ even though C++ has its
place.

C was a quick "throw-together" language at Bell Labs that took hold.
C++ was
built on top of C an carried the syntax.

C# has superior syntax. In C/C++ both the pointer declaration and
the object
of the pointer use a * .
C++ also introduced the reference, to reduce the use of pointers.

I also object to the use of C/C++ to denote a single language. :-)

They are *very* different.

Also C# array are declared as a type rather than
type/context:

e.g. int[] x; v.s. int x[];
std::vector<int> x;

C# has the advantage of practical "non-rushed" thinking in its
design.
I have the highest regards for Anders Hejlsberg, but believe he
designed a Java killer with the best from Delphi, not a C++
replacement.

http://msdn.microsoft.com/vcsharp/ho...g/default.aspx
Again. I don't deny that C++ has its place and can have advantages.
However,
I think C# is the "general" replacement for C++. People may not
fully realize
it yet. That is my opinion.


Ok, then count me in with the "People".
Bo Persson
Dec 12 '05 #19
"Bo Persson" wrote:

std::vector<int> x;


std::vector<int> x;

vs.

int[] x;

I rest my case :-)

Dec 12 '05 #20
"Greg" <gm@mcpherran.com> wrote in message
news:94**********************************@microsof t.com...
"Bo Persson" wrote:

std::vector<int> x;


std::vector<int> x;

vs.

int[] x;

I rest my case :-)


std::vector<int> x;

vs.

System.Collections.Generic.List<int> x;

Better to compare apples to apples...

-cd

Dec 12 '05 #21
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:OX**************@TK2MSFTNGP11.phx.gbl...

Still not apples to apples - how 'bout:

std::vector<int> x;

vs.

System.Collections.Generic.List<int> x = new
System.Collections.Generic.List<int>();

-cd
Dec 12 '05 #22

"Bo Persson" <bo*@gmb.dk> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
<SNIP> Ok, then count me in with the "People".
Bo Persson


Count me as thinking just like Bo does.

-Michael Viking
Dec 12 '05 #23
Greg wrote:
e.g. int[] x; v.s. int x[];
int[] x;
vs
cli::array<int> x;

I agree, the original designers of C could have separated the type a
little bit better. But nobody uses types like int[] anymore for function
arguments -- you simply pass int*.
C# has the advantage of practical "non-rushed" thinking in its design.


And yet it doesn't support such basic features as constants and constant
member functions. No destructor (!!!) means no RAII, which is big
problem when an object holds a resource. You can't create a class on the
stack. No operator overloading, no templates. You can't even declare a
pointer and increment it. And so on, I could continue.

Tom
Dec 13 '05 #24
JAL
This is as expected in a garbage collected runtime, but you can still do
deterministic cleanup:

http://www.geocities.com/Jeff_Louie/oop26.htm

"Tamas Demjen" wrote:
No destructor (!!!) means no RAII, which is big
problem when an object holds a resource. Tom

Dec 13 '05 #25
JAL wrote:
"Tamas Demjen" wrote:
No destructor (!!!) means no RAII, which is big
problem when an object holds a resource. Tom
This is as expected in a garbage collected runtime,


It's really not a consequence of the runtime being garbage collected, but
rather a consequence of the by-reference way in which objects are handled in
C# (or Java). The lifetime of the object is de-coupled from the lifetime of
the reference by the indirection. This can be a "Good Thing" if the object
contains no resources other than memory, but it's a detriment when the
object holds other kinds of resources.
but you can still do deterministic cleanup:

http://www.geocities.com/Jeff_Louie/oop26.htm


Yes you can. The problem is, you have to remember to do it. In C# it's a
coding idiom. In C++ it's the natural language semantics.

-cd

Dec 13 '05 #26
JAL wrote:
This is as expected in a garbage collected runtime, but you can still do
deterministic cleanup:

http://www.geocities.com/Jeff_Louie/oop26.htm


I don't want that. try/catch can't do

std::vector<boost::shared_ptr<Resource> > items;

I need my destructor, not only for deterministic cleanup in a scope, but
also for deterministric cleanup in containers. If you store resources
that require destruction (non-memory-only resources) in a container, you
still want to make sure that when the container goes out, all its items
go out automatically. Maybe if you program in a fully managed
environment you don't use destructors that often, but I essentially wrap
unmanaged code to managed interfaces, and therefore all of my classes
requires Dispose() to release unmanaged memory in a timely manner. You
could say they'll eventually be garbage collected, but I'm saying it's
too late. Some resources are very critical and must be destroyed
deterministically, and unmanaged memory must be deallocated as soon as
possible too.

Just imagine storing high resolution color images wrapped in a very thin
managed class. I don't care if the garbage collector doesn't kick in for
a while for the 16-byte unmanaged part, but my 50 MB unmanaged data had
better be deallocated when they're not used anymore. I can't wait until
the garbage collector runs out of managed memory, because my unmanaged
memory will run out much earlier. My example shows that a single missed
call to Dispose can be just as catastrophic in the managed world as it
is in the unmanaged world, except the unmanaged world has tools
(boost::shared_ptr) to deal with that situation. And I believe C++/CLI
is prepared to deal with those cases too. C# is not. Correct me if I'm
wrong.

You could say I'm just spoiled by modern C++ and boost, but C# is a step
behind from this point of view, and .NET itself is a step behind for not
supporting const correctness too. It's also a step forward for
properties, events, reflection and self-contained packaging (managed
assembly) support.

Tom
Dec 13 '05 #27
Greg wrote:
Is it your general opinion that C# is generally designed/intended/ready to
replace C++? I know the answer is not black and white but please repond YES
or NO (add any comments if you would like)

I just thought it might be interesting to get a tally. This is related to a
previous thread. So far we have two NOs from "aa" and Bo Persson.


Yes and no.

IMHO C++ - C++/CLI and C# can be used very effectively together and both
languages have their pros and cons.

What about if the C# compiler could freely mix both languages in a
single project e.g. something like 'extern "C++" in C# ;-) and directly
call the C++/CLI code without using an intermediate DLL or linker tricks ?

If additionally C# would have RAII I would be perfectly happy about this
combination.

To be real, I think both languages will coexist for a very long time.
Only if the basis, the operating system and it's main interfaces will be
managed code too and if the native MSIL compiler will be available I
think more and more code will be written in plain C#.

Currently I think C# has another focus than replacing C++. But this may
happen some day.

Andre
Dec 13 '05 #28
int[] x;

x has all the methods of ICollection and Array. So I think int[] x is apples
to apples.

--
Greg McPherran
www.McPherran.com
"Carl Daniel [VC++ MVP]" wrote:
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:OX**************@TK2MSFTNGP11.phx.gbl...

Still not apples to apples - how 'bout:

std::vector<int> x;

vs.

System.Collections.Generic.List<int> x = new
System.Collections.Generic.List<int>();

-cd

Dec 13 '05 #29
JAL
Then you should like C++/cli which implements the using construct within the
language semantics as long as the ref class is constructed "on the stack".

"Carl Daniel [VC++ MVP]" wrote:

Yes you can. The problem is, you have to remember to do it. In C# it's a
coding idiom. In C++ it's the natural language semantics.

-cd

Dec 14 '05 #30
JAL
Tom... C++/cli has implemented deterministic destructors by implementing the
semantics of a ref class "on the stack." It basically automates the using
construct.

http://www.geocities.com/jeff_louie/...estructors.htm

"Tamas Demjen" wrote:
And I believe C++/CLI
is prepared to deal with those cases too. C# is not. Correct me if I'm
wrong.

Dec 14 '05 #31
JAL
I don't really understand your point. Java and C# are garbage collected and
objects are created using reference semantics.They do not normally use
reference counting so finalization is not deterministic. Seems to me there is
a direct link between the decision to use garbage collection and the lack of
deterministic finalization.

C# value types do not have destructors, but C++/cli ref classes can be
declare "on the stack" so that the "destructor" gets called when the "value"
goes out of scope, much in the way that delete is called on a contained
pointer when the smart pointer goes out of scope.

"Carl Daniel [VC++ MVP]" wrote:
It's really not a consequence of the runtime being garbage collected, but
rather a consequence of the by-reference way in which objects are handled in
C# (or Java). The lifetime of the object is de-coupled from the lifetime of
the reference by the indirection. This can be a "Good Thing" if the object
contains no resources other than memory, but it's a detriment when the
object holds other kinds of resources.


Dec 14 '05 #32
JAL
No... but smart pointers replace the try catch also

void foo()
{
auto_ptr<MyClass> p(new MyClass);
p->DoSomething();
}

replaces

void foo()
{
MyClass* p;
try {
p = new MyClass;
p->DoSomething();
delete p;
}
catch (...) {
delete p;
throw;
}
}

"Tamas Demjen" wrote:
I don't want that. try/catch can't do

std::vector<boost::shared_ptr<Resource> > items;
Dec 14 '05 #33
JAL wrote:
I don't really understand your point. Java and C# are garbage collected and
objects are created using reference semantics.They do not normally use
reference counting so finalization is not deterministic. Seems to me there is
a direct link between the decision to use garbage collection and the lack of
deterministic finalization.
Regarding memory this is true. But regarding resources (e.g. file
handles) you cannot always rely on garbage collection.

You have to write:
void foo()
{

using ( File f1 = new File(), File f2 = new File())
{
....
} <-- Here the files are automatically closed

}

Would be fairly easy to allow using keyword to be used in a more direct
manner in C#:

void foo()
{
using File f1; // Automatically default constructed
using File f2("aa"); // Automatically constructed
} <-- Here the files would be automatically closed

But this unfortunately doesn't help if you need to have an object in
multiple lists and automatically disposed after the last reference has
been removed from the list.
[...]


Andre
Dec 14 '05 #34

Greg wrote:
int[] x;

x has all the methods of ICollection and Array. So I think int[] x is apples
to apples.


An array in C# is not extensible. A std::vector is....

Arnaud
MVP - VC

Dec 14 '05 #35
JAL wrote:
I don't really understand your point. Java and C# are garbage
collected and objects are created using reference semantics.They do
not normally use reference counting so finalization is not
deterministic. Seems to me there is a direct link between the
decision to use garbage collection and the lack of deterministic
finalization.


Deterministic object lifetime and deterministics memory reclamation are two
separate things. C# joins them into a single concept, but they needn't be
so joined. C++/CLI is an example of a language that treats them as the
separate things that they are.

-cd
Dec 14 '05 #36

"JAL" <JA*@discussions.microsoft.com> skrev i meddelandet
news:DB**********************************@microsof t.com...
No... but smart pointers replace the try catch also

void foo()
{
auto_ptr<MyClass> p(new MyClass);
p->DoSomething();
}

replaces

void foo()
{
MyClass* p;
try {
p = new MyClass;
p->DoSomething();
delete p;
}
catch (...) {
delete p;
throw;
}
}


Or, if you are using proper C++:

void foo()
{
MyClass m;
m.DoSomething();
}
No problem! ;-)
Bo Persson

Dec 14 '05 #37
JAL wrote:
No... but smart pointers replace the try catch also


My point is that the C# using keyword works for trivial cases when you
locally allocate and object and delete it right away. But C# using
doesn't work when the object has a longer life span, but still requires
automatic destruction. How do you store a list of resources in a
container/collection with deterministic cleanup?

All you're showing is that

C++ auto_ptr<T> t(new T)
is the same as
C# using(T t = new T)

That I agree with. But the real differences begin to show up when I can
do in C++ vector<shared_ptr<T> >. In C# I don't think there's a solution
to it. I actually tried to put objects into a List<T> using C# 2.0, and
it didn't automatically call Dispose on T. So List<T>::Dispose (if it
has such a thing at all) doesn't call Dispose for the contained objects.
I think it would be nice if .NET collections had a Dispose method, which
would call Dispose for its members. But even that wouldn't solve a lot
of other problems that reference counted shared_ptr does. What if a
single resource has 2 object copies, both handling the same resource,
and the actual resource should only be disposed when the last object
copy goes out of existence?

As Carl said, pure managed memory reclamation is not the same thing as
resource reclamation. In C# you don't worry about releasing allocated
managed memory, but you still have the burden of reclaiming resouces,
and the language doesn't provide a very good support for that. The using
keyword only solves that for trivial cases, when the object is create
and delete in the *same scope*, which can't always be ensured. In
complex applications resources are stored in containers, in other
objects, and they're destructed in a very complex way.

One more advantage of the boost implementation is weak_ptr. It clearly
separates ownership semantics (shared_ptr) from reference semantics
(weak_ptr). Using weak_ptr you can be sure that you don't leave trash
objects behind accidentally. It automatically makes sure that when the
last shared_ptr goes out of existence, all weak_ptr's that refer to the
object get NULLed out automatically. I challenge you to implement this
in C#, without manually having to call Dispose. I believe it's doable in
C++/CLI, even if we don't initially have such a solution yet.

I'd like to note that even in C++/CLI the standard .NET containers
suffer from deterministric destruction problems:

using namespace System::Collections::Generic;

ref class Guarded
{
public:
~Guarded() { Console::WriteLine(L"~Guarded"); }
};

int main(array<System::String ^> ^args)
{
List<Guarded^> items;
items.Add(gcnew Guarded);
return 0;
}

Although "items" uses stack semantics, when it goes out of scope it
doesn't Dispose its members. And trying to do

List<Guarded> items

doesn't work, because List<T> is a generic, not a template. I'm not sure
if the upcoming STL.NET will solve this. It's very concerning when I
program in .NET, even in C++/CLI. I simply can't use the .NET containers
to store objects that wrap unmanaged types, because the Dispose pattern
is broken. I think it's just a matter of time before really good C++/CLI
container implementations come out, where we'll be able to write

vector<shared_ptr<ManagedClass> >

Not having the proper tools for this problem, I feel like I live in
danger, just like C programmers do (whether they admit it or not). Or
even more so, because C programmers are not used to exceptions, but .NET
programmers must think of exception safety. Every system that provides
exceptions but no exception safety is a disaster waiting to happen.

Tom
Dec 14 '05 #38
Some have pointed out advantages of C++ which I don't contest. Yet, I am not
sure these are outweighed by other considerations for many .NET development
scenarios.

There are some cases where C++ is the only option: e.g. a pure native
Windows App with no .NET Runtime required.

However for a .NET app, if C++ is on par with C#, then how does one decide
which language to use. Are there any criteria besides being already familiar
with C++ etc.?

I.e. are there any concrete reasons why the full development cycle right up
to delivery will be more cost effective and yield greater ROI using one
language vs. the other?

Thank You
--
Greg McPherran
www.McPherran.com
"Andre Kaufmann" wrote:
Greg wrote:
Is it your general opinion that C# is generally designed/intended/ready to
replace C++? I know the answer is not black and white but please repond YES
or NO (add any comments if you would like)

I just thought it might be interesting to get a tally. This is related to a
previous thread. So far we have two NOs from "aa" and Bo Persson.


Yes and no.

IMHO C++ - C++/CLI and C# can be used very effectively together and both
languages have their pros and cons.

What about if the C# compiler could freely mix both languages in a
single project e.g. something like 'extern "C++" in C# ;-) and directly
call the C++/CLI code without using an intermediate DLL or linker tricks ?

If additionally C# would have RAII I would be perfectly happy about this
combination.

To be real, I think both languages will coexist for a very long time.
Only if the basis, the operating system and it's main interfaces will be
managed code too and if the native MSIL compiler will be available I
think more and more code will be written in plain C#.

Currently I think C# has another focus than replacing C++. But this may
happen some day.

Andre

Dec 15 '05 #39
JAL
Hi Tom... I agree that using comes up short when compared to smart pointers.
Using is "... about as close as we can get to a smart pointer and RAII in C#"
The best I can do in C# is:

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace DeterminedCollection
{
interface IData
{
int I { get;}
}
interface IMyInterface : IData, IDisposable{}
class Dummy : IMyInterface
{
private bool disposed = false;
private int i;
public Dummy(int i)
{
this.i = i;
}
public void Dispose()
{
Dispose(true);
Console.WriteLine("disposed");
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing) // called from Dispose
{
// Dispose managed resources.
}
// Clean up unmanaged resources here.
}
disposed = true;
}
~Dummy() // maps to finalize
{
Dispose(false);
}
public int I {
get {
if (disposed) { throw new ObjectDisposedException("Dummy"); }
return i;
}
}
}
class JALCollection : IDisposable //, IEnumerator
{
private bool disposed = false;
private ArrayList list = new ArrayList();
// ASSERT d is not null
// ASSERT no object holds a reference to
// d outside of this class
// USAGE Add(new MyClass());
// where MyClass implements IMyInterface
public void Add(IMyInterface d) {
if (d != null)
{
list.Add(d);
}
else { throw new ArgumentException(); }
}
// test only, not safe! implement IEnumerator
public int GetValue(int i)
{
return ((IData)list[i]).I;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
foreach (IDisposable d in list)
{
d.Dispose();
}
}
private void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing) // called from Dispose
{
// Dispose managed resources.
}
// Clean up unmanaged resources here.
}
disposed = true;
}
~JALCollection() // maps to finalize
{
Dispose(false);
}
}
class Program
{
static void Main(string[] args)
{
using (JALCollection jal= new JALCollection())
{
jal.Add(new Dummy(1));
Console.WriteLine(jal.GetValue(0)); // test only
}
Console.ReadLine();
}
}
}

Dec 15 '05 #40
JAL
Hi Bo... Of course, the code is not _exactly_ equivalent :) Stack vs Heap
allocation.

"Bo Persson" wrote:

"JAL" <JA*@discussions.microsoft.com> skrev i meddelandet
news:DB**********************************@microsof t.com...
No... but smart pointers replace the try catch also

void foo()
{
auto_ptr<MyClass> p(new MyClass);
p->DoSomething();
}

replaces

void foo()
{
MyClass* p;
try {
p = new MyClass;
p->DoSomething();
delete p;
}
catch (...) {
delete p;
throw;
}
}


Or, if you are using proper C++:

void foo()
{
MyClass m;
m.DoSomething();
}
No problem! ;-)
Bo Persson

Dec 15 '05 #41
Greg wrote:
Some have pointed out advantages of C++ which I don't contest. Yet, I am not
sure these are outweighed by other considerations for many .NET development
scenarios.

There are some cases where C++ is the only option: e.g. a pure native
Windows App with no .NET Runtime required.
Currently yes. But Microsoft (research) has a native C#/MSIL compiler
and there are AFAIK also some commercial native C# compilers.
Though all memory is still garbage collected and surely the C# native
compilers are somewhat restricted.
However for a .NET app, if C++ is on par with C#, then how does one decide
which language to use. Are there any criteria besides being already familiar
with C++ etc.?
I think you are more productive by using C#/VB.Net than C++ for a .NET
app, at least with Visual Studio.
Just because you have many more IDE features available for that
languages. Refactoring, Code Snippets, more Intellisense Features, Test
Cases and many others and they are much faster in compilation.
If you need to access unmanaged resources or (old) C++ code surely
C++/CLI is the way to go.
But since the languages in .NET can be very tightly coupled, it's no big
deal to mix them. Write a C++/CLI Dll which is accessing and dealing
with the umanaged resources and exports .NET wrapper classes and consume
the from C#/VB.Net or any other .NET language.

Another criteria is speed / optimized code. Optimized code currently can
be written only in a native language, like C++ and the C++ compiler
generates more optimized MSIL code than the C#/VB.Net compilers do
(currently).
I.e. are there any concrete reasons why the full development cycle right up
to delivery will be more cost effective and yield greater ROI using one
language vs. the other?

See above. For GUI applications or GUI components I wouldn't use C++,
but prefer C#, just for the reasons I mentioned above.
For speed centric and optimized code I would prefer C++.
Thank You


Regards,
Andre
Dec 15 '05 #42
JAL
Andre.. Ok this stuff IS harder than brain surgery. If I get some time I will
try to implement this but after doing 40 hours of neurosurgery I need to go
to sleep :).

"Andre Kaufmann" wrote:
But this unfortunately doesn't help if you need to have an object in
multiple lists and automatically disposed after the last reference has
been removed from the list.
[...]


Andre

Dec 15 '05 #43

"JAL" <JA*@discussions.microsoft.com> skrev i meddelandet
news:4F**********************************@microsof t.com...
Hi Bo... Of course, the code is not _exactly_ equivalent :) Stack vs
Heap
allocation.
I know it's different, that was a point. :-)

Managed code solves the memory leak "problem" of

void foo()
{
MyClass* p = new MyClass;
p->DoSomethingThatThrows();
delete p;
}

My solution is not to invent a new language, but use what is already
in C++

void foo()
{
MyClass m;
m.DoSomethingThatThrows();
}

Here the m object, and any subobjects or allocations done by m, will
be destroyed at the end of scope. The destructor of m is called,
Always.

So, there isn't any problem in the first place, and then I'm offered a
solution. No thanks. .-)
Bo Persson
"Bo Persson" wrote:

"JAL" <JA*@discussions.microsoft.com> skrev i meddelandet
news:DB**********************************@microsof t.com...
> No... but smart pointers replace the try catch also
>
> void foo()
> {
> auto_ptr<MyClass> p(new MyClass);
> p->DoSomething();
> }
>
> replaces
>
> void foo()
> {
> MyClass* p;
> try {
> p = new MyClass;
> p->DoSomething();
> delete p;
> }
> catch (...) {
> delete p;
> throw;
> }
> }


Or, if you are using proper C++:

void foo()
{
MyClass m;
m.DoSomething();
}
No problem! ;-)
Bo Persson

Dec 15 '05 #44

"Greg" <gm@mcpherran.com> skrev i meddelandet
news:A3**********************************@microsof t.com...
Some have pointed out advantages of C++ which I don't contest. Yet,
I am not
sure these are outweighed by other considerations for many .NET
development
scenarios.

There are some cases where C++ is the only option: e.g. a pure
native
Windows App with no .NET Runtime required.

However for a .NET app, if C++ is on par with C#, then how does one
decide
which language to use. Are there any criteria besides being already
familiar
with C++ etc.?
But that weighs in heavily. If I have been writing C++ code for 15-20
years, and are happy with that, what are the productivity advantages
of moving to another language?

I.e. are there any concrete reasons why the full development cycle
right up
to delivery will be more cost effective and yield greater ROI using
one
language vs. the other?


Exactly my question! :-)


IMHO C++ - C++/CLI and C# can be used very effectively together and
both
languages have their pros and cons.


That is actually 3 languages, not 2.

C++/CLI is not even close to the language called ISO C++. It just
looks similar, just like Java does.
Bo Persson

Dec 15 '05 #45
JAL wrote:
class JALCollection : IDisposable //, IEnumerator
{
private bool disposed = false;
private ArrayList list = new ArrayList(); [...] public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
foreach (IDisposable d in list)
{
d.Dispose();
}
}


Yes, this is exactly how Delphi programmers are forced to handle
containers too. Always wrap it into a 200 line class, just to achieve
the functionality of what the framework provided collection is supposed
to do. It's rather error prone when this responsibility is left up to
the programmer. Let alone the time being spent on typing in the same
code again and again. My applications use 100s of C++ containers, some
of which are inter-references, and it would be very painful to
substitute them with ad-hoc hand-written replacement containers, just
because the language doesn't provide the concept of destructors. Having
destructors makes a lot of difference, and with large projects it saves
a whole lot of time in the long run.

To be fair, using generics in C# 2.0 you can write your own dream
resource-holding containers, that's true, so you don't have to retype
the same kind of code again and again. But it's still a bit
disappointing to me that deterministic destruction of resources is not
automatic but often requires serious effort on the programmer's side.
Isn't it ironic that in C# you're not supposed to worry about leaks
anymore, and yet in old native C++ you can achieve safe resource
handling much easier and safer?

Of course everything can be done in every language, even in assembly,
it's just a matter of how much effort it requires, how error prone it
is, and how maintainable the code is.

Tom
Dec 15 '05 #46
Bo Persson wrote:
"Greg" <gm@mcpherran.com> skrev i meddelandet
news:A3**********************************@microsof t.com...
[...]
IMHO C++ - C++/CLI and C# can be used very effectively together and
both ... That is actually 3 languages, not 2.

C++/CLI is not even close to the language called ISO C++. It just
looks similar, just like Java does.


C++/CLI is an extension to C++, therefore a C++/CLI compiler must be
able to compile standard C++.
So I think it's equally true to speak of 2 and/or of 3 languages. ;-)
Bo Persson


Andre
Dec 15 '05 #47
JAL wrote:
Andre.. Ok this stuff IS harder than brain surgery. If I get some time I will
try to implement this but after doing 40 hours of neurosurgery I need to go
to sleep :).


Uff. I would just state the opposite. You (will) have a comparison
between both - I better don't give it a try - should be better for the
patient ;-)

[...]
Andre

Dec 15 '05 #48

"Andre Kaufmann" <an****************@t-online.de> skrev i meddelandet
news:ef**************@TK2MSFTNGP12.phx.gbl...
Bo Persson wrote:
"Greg" <gm@mcpherran.com> skrev i meddelandet
news:A3**********************************@microsof t.com...
[...]
IMHO C++ - C++/CLI and C# can be used very effectively together
and
both ... That is actually 3 languages, not 2.

C++/CLI is not even close to the language called ISO C++. It just
looks similar, just like Java does.


C++/CLI is an extension to C++, therefore a C++/CLI compiler must be
able to compile standard C++.


No, it does not.

There are some extensions, but also some parts that are missing, and
some parts that just behave differently.
So I think it's equally true to speak of 2 and/or of 3 languages.
;-)


Let's disagree. :-)

Bo Persson
Dec 15 '05 #49
JAL
Sounds like C++ meets all your requirements and you have no need to code in a
garbage collected environment.

"Bo Persson" wrote:

"JAL" <JA*@discussions.microsoft.com> skrev i meddelandet
news:4F**********************************@microsof t.com...
Hi Bo... Of course, the code is not _exactly_ equivalent :) Stack vs
Heap
allocation.


I know it's different, that was a point. :-)

Managed code solves the memory leak "problem" of

void foo()
{
MyClass* p = new MyClass;
p->DoSomethingThatThrows();
delete p;
}

My solution is not to invent a new language, but use what is already
in C++

void foo()
{
MyClass m;
m.DoSomethingThatThrows();
}

Here the m object, and any subobjects or allocations done by m, will
be destroyed at the end of scope. The destructor of m is called,
Always.

So, there isn't any problem in the first place, and then I'm offered a
solution. No thanks. .-)
Bo Persson
"Bo Persson" wrote:

"JAL" <JA*@discussions.microsoft.com> skrev i meddelandet
news:DB**********************************@microsof t.com...
> No... but smart pointers replace the try catch also
>
> void foo()
> {
> auto_ptr<MyClass> p(new MyClass);
> p->DoSomething();
> }
>
> replaces
>
> void foo()
> {
> MyClass* p;
> try {
> p = new MyClass;
> p->DoSomething();
> delete p;
> }
> catch (...) {
> delete p;
> throw;
> }
> }

Or, if you are using proper C++:

void foo()
{
MyClass m;
m.DoSomething();
}
No problem! ;-)
Bo Persson


Dec 16 '05 #50

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.