Until now I have only dabbled here-and-there in C# and the .NET framework - it's not my forte, and there are times when seeing a bit of C# code offends my sensibilities which are well and truly rooted into the world of C++ where deterministic finalisation and templates have shaped my ideas about what a "good" bit of code looks like.
But with technology moving the way it is, I need to make that mental leap into the world of understanding the peculiar ways in which C# programmers think about problem solving - it's clearly different to the way I see things; I'm aware how terrible some of my attempts at writing managed .NET code often are. (And equally, I sometimes find it maddening when I see a bit of C++ code written by a C# programmer who has liberally thrown pointers all over the place)
Just today I came across a C# design pattern known as IoC (Inversion of Control) which partly flies in the face of my C++ sensibilities (Although as I type this and think about it a little more, its not so far removed from the ways in which I write template classes - in fact IoC and Dependency Injection seems conceptually similar to 'policy based design' which has been part of C++ template programming for a long time).
My initial reaction upon reading the MSDN article about IoC was along the lines of "Hey, doesn't this go against encapsulation? surely this is a step backwards?" , but upon pondering a bit further, it dawned on me that the reason C++ objects take control (ownership) of objects is that they are fully capable of doing the construction, copying and cleanup of those objects straight out-the-box without any extra fuss; so in C++ it makes all kinds of sense to bundle objects together into a controlling class, but in C# the tables are turned and it becomes a liability because of the way that C# treats class variables as smart-pointers/references means that you suddenly need to think about copying and cleanup (boilerplate code alert!).
Has anyone else who moved from native code to managed code had a similar realisation that they needed to mentally shift their ideas in order to feel comfortable that the code they're writing isn't "Junk"?
I feel as if IoC has opened my eyes to one bit of the C# puzzle for me, but there are still other bits of C# which make me feel uneasy; in particular the Dispose pattern makes me wonder how its possible to write C# code without needing massive amounts of boilerplate everywhere to clean up non-memory resources (IDisposable and try..finally both me long for C++ destructors for things like sockets, databases, etc) I guess people must have found other patterns and idioms which let you avoid all of this?
- In a way, it almost seems that 'good' C# design is sometimes the polar opposite of 'good' C++ design (But then again, there are certain things which C# has taught me which I'm sure has made me a better C++ programmer too - particularly when it comes to writing multi-threaded code).
Is it maybe the case that C++ has taught me to become so hung up on object ownership and finalisation that I'm struggling to see a better way to write code which uses resources? There seem to be too many situations where it's crucial to know precisely when an object will be destroyed. Obviously of the "evils" which have existed in C and C++ code for so long, the necessity of calling delete or free() is right up there, but C# seems to have only shifted the problem in having to remember using, try/catch/finally or .Dispose() instead
I've seen suggestions about using Event-Driven-Disposal code to avoid the problem (For example, WPF apparently has a "Shutdown" event which can be used like a C++ destructor for WPF forms). Do people do this quite a lot?
PS - the reason for this thread is that I'm curious on others' opinions and experiences about how you all learned to write better C# code - especially if you started out with a language such as C++, since it seems that many "C++ best practices" don't apply. (I'm already looking into buying the Effective C# books - hopefully these are as insightful as Scott Meyers' Effective C++ books)
/END Wall of text. Thanks for reading

New Topic/Question
Reply



MultiQuote







|