Sunteți pe pagina 1din 3

Re: Utility functions in C#

Re: Utility functions in C#

Source: http://coding.derkeiler.com/Archive/General/comp.programming/2005−06/msg00319.html

• From: "Roger Willcocks" <rkww@xxxxxxxx>


• Date: Sat, 11 Jun 2005 00:33:29 +0100

"Arthur J. O'Dwyer" <ajo@xxxxxxxxxxxxxxxxxxxxx> wrote in message


news:Pine.LNX.4.60−041.0506101832300.6453@xxxxxxxxxxxxxxxxxxxxxxxx
>
> Well, here's a discussion topic I bet some regulars never expected
> from me. ;) I'm doing work (as in internship work) in the C#
> language, and have run into a problem. Here's the gist of it.
> I have in separate source files, in the same project,
>
> namespace Myproject {
> class Alice {
> void alice_bar(int arg)
>{
> if (arg % 2 == 0) baz();
>}
>}
>}
>
> namespace Myproject {
> class Bob {
> Fred bob_bar()
>{
> [...]
> if (wibble % 2 == 0) baz();
>}
>}
>}
>
> Naturally, I would like to replace "xyz % 2 == 0" with something
> more mnemonic, such as "even(xyz)". In C, I would just write at
> the top of the file
>
> #define even(x) ((x) % 2 == 0)
>
> Unfortunately, C# does not allow that kind of macro definition.
> I can create a one−line function in class Alice, called "Alice.even",
> and refer to it as "even" within Alice. That was my solution
> yesterday. But today I need to use "even" within class Bob, also,
> as shown above. I really don't want to write

Re: Utility functions in C# 1


Re: Utility functions in C#
>
> Fred bob_bar()
>{
> [...]
> if (Alice.even(wibble)) baz();
>}
>
> since the part before the word "even" is just useless noise. However,
> I would prefer not to duplicate a lot of code. The "even" example is
> real, but in addition to "even" I have functions such as
>
> bool HasText(string s, string t, int offset)
>
> which returns "s.Substring(offset).StartsWith(t)", except not computed
> that way −−− and I might come up with more useful functions later,
> so the solution needs to scale nicely.
>
> I've even thought of making a "utilities" abstract class, and having
> everything inherit from it, so that the unadorned names are visible
> where I need them −−− but since C# doesn't support multiple inheritance,
> that won't scale.
>
> My current solution involves two classes in namespace Myproject,
> named "Is" and "Has". "Is" has a method "Even", and "Has" has a method
> "Text". So I write
>
> if (Is.Even(wibble)) ...
>
> The dot is annoying and somewhat misleading, but IMO less so than if
> I were to write
>
> if (Utils.even(wibble)) ...
>
> or some such Hungarian−looking phrase.
>
> Any ideas? How do real−world projects balance readability with the
> need to cope with B−&−D languages? Is there a clever trick I'm missing?
>
> −Arthur

It's very much like C++, write

using Utils;

and you can then write:

if (even(wibble)) ...

at least that's what it says in my 'professional c#' book. Not that I've
ever written a line of the stuff...

Re: Utility functions in C# 2


Re: Utility functions in C#

−−
Roger

• Follow−Ups:
♦ Re: Utility functions in C#
◊ From: Arthur J. O'Dwyer

• References:
♦ Utility functions in C#
◊ From: Arthur J. O'Dwyer

• Prev by Date: Re: Suggestions for double−hashing scheme


• Next by Date: Re: Circular buffer
• Previous by thread: Utility functions in C#
• Next by thread: Re: Utility functions in C#
• Index(es):
♦ Date
♦ Thread

Re: Utility functions in C# 3

S-ar putea să vă placă și