Sign in to follow this  
OpenGL_Guru

C# advice needed...

Recommended Posts

I have been writing code in C/C++ now for about 5 1/2-6 years now not counting college work(just to be clear) and now i am wanting to finally upgrade my skills a little bit since i have some extra time. I have put my finger in the C# waters to gauge the temperature and am ready now to just dive right in head first. What would you suggest that i be on the look out for..sort of a heads-up, especially someone coming from a mostly C++ background. things that i think of are memory allocation, memory management in general, accessing arrays, etc etc. I know i could google some things but i would rather hear from people espeically those that started out in C++ and expanded into C#.. what you encountered that made you code differently in C#. thanks for all your input in advance. [smile]

Share this post


Link to post
Share on other sites
I came from a C++ background when I switched to C# (though, not as extensive as yours). One of the things that I've loved about C# is the fact that almost nothing has come to bite me :) The only minor thing I can think of off had is that classes are reference types and that structs are value types. The reference type thing gave me a bit of grief before I understood it, as I didn't realize I was actually changing my original object instead of a shallow copy of that object ;)

Share this post


Link to post
Share on other sites
To understand how to program in a way that makes the garbage collectors life easier (thus your app perform better) see Gamefest 2006: Windows and Xbox 360 System Programming Track - paying particular attention to "Taming the CLR: How To Write Really Fast Managed Code".

The presentation deck and audio lecture explains really well what is going on and how to write things that are good for both speed and memory usage.

There are several gotchas going from C++ to C#. That particular presentation goes through some of the memory ones, like prefer short-lived objects or life-time objects... the mid-life objects are the most troublesome to collect. And the way links between generations of those objects can be formed in a more GC friendly way.

Err... prefer passing around object references where possible instead of making deep copies.

Use "dispose" and "using" to nudge the garbage collector for particular objects (usually those that are actually a wrapper around something unmanaged).

Look, and look again for anything that you might think "wow, I've done this loads of times, you would think it would be in the framework somewhere". It probably is, hidden away.

Structs and classes actually are very different beasts in C#, def worth while investigating those differences for yourself.

Don't be tempted to put "ref" on everything you want to pass by reference. Classes are implicitly passed by reference, structs aren't. You use ref if you want to do a swap type function (like you would in a bubble sort).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
C# should be trivial for anyone coming from a strong C++ background. Try something interesting like Haskell or O'Caml if you want to learn new things.

Share this post


Link to post
Share on other sites
As instruo says, there's little in C# which will bite.

Boxing used to be a fairly major one, but it's almost non-existant with the addition of generics.

Generics are not generated at compile time like C++ templates, they are generated at JIT time. This allows for a little reflection goodness, but makes quite a bit of C++ black magic unfeasible.

Event references count for the GC. If you just create an object and specify one of it's methods as part of an event it won't get collected until the object containing the event is collected. This is useful to create functors which have no ownership beyond the delegate they're tied to. It's also a little gotcha since it might keep an object around when you don't expect it.


The GC, the compilation model, reflection, more flexible types/lack of templates... These jump out at me as things that made me code differently. The key thing is to code differently. C++ programmers can code in a C style for example, but won't get the most of their tools. Coding C++ style with C# is the same thing, only exacerbated a little because the language and .NET are so consistant (compared to C++) that deviating from that is all the more noticable.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
well.. to tell a little different story on this. if you love doing stuff in c++ with heavy use of templates and you can't get anything done (well.. not anything.. you know what i mean) without designing some kind of templated framework for the task at hand you won't be so happy with what c# has to offer in this regard.
the flexibility of c++ templates are unmatched in c# (of course, the danger and the hassles with templates is also not available in c# but just say you are one of the few who love templates).. i mean, especially if your're into generic adventures that border on a meta programming explosion c# is a problem.

one other thing i don't like is the lack of the ability to utilize special processor instructions like the sse1,2,3,4 ones. if you depend on this kind of raw power then, of course, any kind of managed language isn#t the right thing.

these are the only two reasons that make me go back to native c++ quite frequently and heavily.

cheers,
simon

Share this post


Link to post
Share on other sites
Goodness me all these C# bashers, he wasn't asking about why you believe C++ is better then C#, he was asking about the differences from someone actually willing to learn it and whom obviously finds it "interesting".

Generics share many things in common with templates, except in my opinion easier to understand. No black magic, just easily readable code.

While true anyone with good C++ could write functioning C# code on day one, to assume you know it all back to front just because you know C++ is an invalid assumption. That's just your ego talking. Oh, and then probably when you try and you can't get similar performance to a C++ app, then you blame the language rather then your lack of experience in the new one. I have to admit when I started out in C# I had a Java bias ("MS is just ripping off Java and I already know Java")... nah, I was wrong - on the surface they look the same but they are both different beasts to master.

Yes I do come from a very strong C++ background, I've reprogrammed part of the Windows operating system (the Gina) from scratch and had to deal with all kinds of wierd and wonderful sparsely or undocumented code in doing that and other projects. And I've been doing C# since it's beta 1. Both in a commercial environment on different projects in parallel.

I don't want to get in to a performance flame because that's not what this thread is about, it was a valid question about things a C++ programmer might want to look out for while learning C#.

Share this post


Link to post
Share on other sites
Quote:
Original post by paulecoyote
Goodness me all these C# bashers, he wasn't asking about why you believe C++ is better then C#, he was asking about the differences from someone actually willing to learn it and whom obviously finds it "interesting".

Generics share many things in common with templates, except in my opinion easier to understand. No black magic, just easily readable code.

While true anyone with good C++ could write functioning C# code on day one, to assume you know it all back to front just because you know C++ is an invalid assumption. That's just your ego talking. Oh, and then probably when you try and you can't get similar performance to a C++ app, then you blame the language rather then your lack of experience in the new one. I have to admit when I started out in C# I had a Java bias ("MS is just ripping off Java and I already know Java")... nah, I was wrong - on the surface they look the same but they are both different beasts to master.

Yes I do come from a very strong C++ background, I've reprogrammed part of the Windows operating system (the Gina) from scratch and had to deal with all kinds of wierd and wonderful sparsely or undocumented code in doing that and other projects. And I've been doing C# since it's beta 1. Both in a commercial environment on different projects in parallel.

I don't want to get in to a performance flame because that's not what this thread is about, it was a valid question about things a C++ programmer might want to look out for while learning C#.


thanks Coyote. i appreciate the feedback.. and to everyone else. i am going to get into it heavily starting this weedend. i got the C# Professional 2005 book.. rather thick but more than one person on here recommended it. it looks like its got tons of stuff i just dont like the fact they the book starts you out trying to execute the code through command line and that they mention they wont be going over VC 2005 until way deep in the book. C# is not that hard to set up in 2005 i dont understand the reasoning for this. anyway needless to say i wont be doing it via command line.

would you mind going over short/mid/long term objects? i am not looking for you to spill it all out for me on here just wanting to know more about it and how it relates to C++ in any way, if anything at all.

also i have a bad(not the right word) feeling about memory management. you say C# is really a different beast than Java but how much more strong-typed is C# in regard to this compared to Java. this isnt a java thread but heard many people didnt like Java stronghold over the programmer. I was just thinking that C# might be the same way. thanks again for your feedback.

if there is anything else that i need to be on the look out for i would love to hear them! [smile]

Share this post


Link to post
Share on other sites
You could also use Java tutorial, just to be sure. ~_^ These are actually closely related.

Short term objects are objects that are expected to stop being valid in a next GC step. He probably ment mid term objects as objects that are valid for 30 s - 5 min. It causes GC to copy them into a new location, and memory fragmentation in a long term.

Strong typed? They are using identical data types.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this