|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
C# to the fullest: readable codeI've found that C# 2 has several non-basic-programming-features turn out to be a great help in making code readable and typesafe, most notable the Generics stuff. Consequently, when I was creating the code below, I figured there must be an easier and above all more readable way to do this: /*---------------------------------------*/ private string ImplodeMyObjects (MyClass[] items) { string result = string.Empty; foreach (MyClass item in items) result += item.ToString(); return result; } private void InterestingFunction() { MyClass[] items = GetFromFoo(); string coolstring = ImplodeMyObjects(items); } /*---------------------------------------*/ It seems that C# 3.5 will provide even cooler stuff to do the above, but even in C# 2 there should be a nicer way to do this? Any comments and suggestions on how to handle this kind of stuff would be much appreciated. Please indicate for which version any suggestions would be. Thanks. -Jeroen On Nov 28, 12:32 pm, Jeroen <mercu***@gmail.com> wrote:
Show quote > I've found that C# 2 has several non-basic-programming-features turn Well, a few suggestions:> out to be a great help in making code readable and typesafe, most > notable the Generics stuff. Consequently, when I was creating the code > below, I figured there must be an easier and above all more readable > way to do this: > > /*---------------------------------------*/ > private string ImplodeMyObjects (MyClass[] items) > { > string result = string.Empty; > foreach (MyClass item in items) > result += item.ToString(); > return result; > > } 1) Use a StringBuilder 2) Make the method generic 3) Make the parameter IEnumerable 4) Make it static static string ImplodeMyObjects<T>(IEnumerable<T> data) { StringBuilder builder = new StringBuilder(); foreach (T item in data) { builder.Append(item); } return builder.ToString(); } I think that's pretty readable on its own, isn't it? With C# 3 (not 3.5 - that's the version of the framework) you could make it an extension method just by changing the parameter list to (this IEnumerable<T> data) so long as it's in a static class. That would let you do: private void InterestingFunction() { string combined = GetFromFoo().ImplodeMyObjects(); } Is this any help? Jon > Is this any help? Yes Jon, thanks, this is a good start for me. I have one follow-upquestion though: > 1) Use a StringBuilder Why to use this instead of simply attaching things one at a time to astring? Also, I'm getting more and more curious whether it would also be possible to do without a function at all, and just do this on one line, though that would probably not improve readability :D. > With C# 3 (not 3.5 - that's the version of the framework) D'oh!Thanks, Jeroen On Nov 28, 12:56 pm, Jeroen <mercu***@gmail.com> wrote:
> > 1) Use a StringBuilder See http://pobox.com/~skeet/csharp/stringbuilder.html> > Why to use this instead of simply attaching things one at a time to a > string? > Also, I'm getting more and more curious whether it would also be Well, with C# 3 it *is* all in one line, just with an extra method.> possible to do without a function at all, and just do this on one > line, though that would probably not improve readability :D. That's likely to be a lot nicer than anything that's built in. (I'm sure it's possible with LINQ and the Aggregate method, but you really don't want to see what it would involve.) Jon Jon,
Thanks a bunch for your helpful responses, at such a short notice even! -Jeroen "Jeroen" <mercu***@gmail.com> wrote in message Not to put anyone else down since lots of people help in this community, and news:574bfa7b-efdf-4d56-9a15-12cdf4042881@d27g2000prf.googlegroups.com... > Jon, > > Thanks a bunch for your helpful responses, at such a short notice > even! > > -Jeroen I'm not the first to say this, but Jon is one of the most contributing members to this community and jumps all over questions he's able to answer. He's a very good guy when it comes to this community (thought I'd mention that since I don't know him in person but have spoken to him via NG quite often). Also used his site more than a few times to help clarify a few things for myself.... Mythran |
|||||||||||||||||||||||