Visar inlägg med etikett c#. Visa alla inlägg
Visar inlägg med etikett c#. Visa alla inlägg

torsdag 16 oktober 2008

The ForEach extension method (C#)

Everyone knows about the foreach keyword, but have you ever noticed there are no equivalent extension method for IEnumerables? I.e. execute "this action" on each element. Perhaps the most simple extension method of them all, which is the reason I'll share it with you :)

public static class MyExtensionMethods
{
public static void ForEach<T>(this IEnumerable<T> list, Action<T> action)
{
foreach(var item in list) action(item);
}
}

For you who aren't familiar with extension methods, what it really says is that, ForEach is a member method of IEnumerable<T>, and can be called by simply writing
myList.ForEach(myAction)

For example, to output each item of a list to the console, you could write:
myList.ForEach(x => Console.Write(x));

instead of
foreach(var x in in myList) Console.Write(x);

In this case you might not gain so much in terms of code size, but I think it makes the code more readable. You are reading text from left to right, so it makes sense having the for each statement to the right, right? :)
myList.WhereThis().SelectThat().DoThis();

(There is probably some kind of cool name for this pattern, like "The opposite of law of demeter"... Gustaf probably has more knowledge in this!)

Here is a more complex example of a combination of extension methods:
myList.Where(x => x.IsValid()).Select(x => x.ComputeValue()).Where(x => x > 0).ForEach(x => Console.Write(x));

Where and Select are also extension methods of IEnumerable. Without these you would have to write:

foreach(var x in myList)
{
if (x.IsValid())
{
var value = x.ComputeValue();
if (value > 0) Console.Write(value);
}
}

8 lines instead of 1! Impressed? :)

So, extension methods! Learn them, use them, and write your own!

måndag 25 februari 2008

Visual Studio Code Snippets and Macros

In this first real post I'll try to explain something called macros in Visual Studio, and how they can make your work (programming) easier. I just recently found out about them, and realized that they resemble something I've been missing in the C++ environment of VS for a long time; code snippets! If you don't know what code snippets are, then here is a short description:

Code snippets are short keywords in your C# document, which when you double-press tab will be converted into C# code! If the code is parameterized, over for example different names or types, then these parameters will be selected first so that you can easily change them. If you for example write prop and double-press the tab key, a new property will be added to the class you are in.
prop <tab><tab>
becomes
private int myVar;

public int MyProperty
{
get { return myVar; }
set { myVar = value; }
}
where int, myVar and MyProperty are parameters which can easily be changed. There are a lot of nice code snippets to use in the C# environment, making coding easier. Just goto Tools -> Code Snippet Manager... or press Ctrl+K followed by Ctrl+B for a list of possible snippets.

But for C++ this doesn't seem to exist. I read something about code snippets being available for C++ in VS 2003, but somehow they where removed in VS 2005.

So this is where macros comes into play! With a macro you can more or less do what ever you want to do. You just create a macro, write the code for it (in VB) and then use it from the macro explorer within VS. To make it even more powerful you can bind keyboard shortcuts to these macros (Tools -> Options... -> Environment -> Keyboard). To show the currently available macros, activate the macro explorer using either Tools -> Macros -> Macro Explorer or by pressing Alt+F8. By default you will have a list of sample macros installed, which are great for playing around with and to get familiar with macros.

So, what then can you do with macros, you ask yourself. Well, for example you can write macros that automatically adds new classes, with a default header and cpp-file, based on a class name. You just tell VS to add two files to your current project, and then put some the class definition in the header, and some default implementations of perhaps constructors and destructors in your cpp-file. Or you can create a macro that cleans up headers and cpp-files, by searching in the code using regular expressions for #include "ClassName.h", and then comment this include if ClassName can't be found in the code. The possibilities are really endless!

So if you are developing in VS using C++, or actually any language supported by VS, and haven't used macros, then go ahead and try it out. It's amazing what you can do! Or if you are developing in C# and haven't tried code snippets then I really encourage you to learn about them. As have been said before: to be a good programmer, you must know your tools!