Resharper and its “Convert to LINQ expression”

Posted by Monty on January 23rd, 2010

I guess im a bit old fashioned, but I like to write code like:

public static List<string> CovertLongListToString(IEnumerable<long> param)
 {
 List<String> returnList = new List<string>();

 foreach (long l in param)
 {
 returnList.Add(l.ToString());
 }

 return returnList;
 }

But then resharper 5.0 gives me this lovely option of “Convert to LINQ Expression”, and it turns into:


public static List<string> CovertLongListToString(IEnumerable<long> param)
 {
 return param.Select(l => l.ToString()).ToList();
 }

Thats just brilliant! I LOVE YOU RESHARPER!

Evolution of looping

Posted by Monty on November 17th, 2008

While im writing a new project using vs.net 2008, Resharper cropped up a nice helper, saying I should use a lambda expression on my code, which made me think about how looping has evolved…


for (int i = 0; i < all.Count; i++)
{
tagCount.Add(new TagCount(container[i].Count, container[i]));
}

foreach (Tag tag in container)
{
tagCount.Add(new TagCount(tag.Count, tag));
}

container.ForEach(delegate(Tag tag)
{
tagCount.Add(new TagCount(container.Count, tag));
});

container.ForEach(tag => tagCount.
Add(new TagCount(container.Count, tag)));

Iv always thought lambda’s were gimmicky, but I do have to admit, they do look fancy and makes things like what im doing easier.

I want my .ForEach(System.Action)!

Posted by Monty on October 14th, 2008

Well I was adding things to a dictionary today (Because im lazy, I have to store 2 values, and I really cant be bothered with creating a class just for that), and I wanted to run a ForEach(System.Action<T>) on it, because I like doing that on System.Collections.Generic.List<t> – it works well, so why change what you know?

But, it turns out Dictionary<TKey, TValue> dosent have a .ForEach. It implements IEnumerable, but it dosent have support for the .ForEach, which is rather strange, since they both do the same thing, but it dosent have the Action<T> method. So I did some digging, and found that .ForEach is a method that belongs to List<T>, and its pretty much the only way to do it (There is a System.Array.Foreach(T[] array, Action<T>) method, but that dosent work with a dictionary.

What I see happening is me extending Dictionary<TKey, TValue> to add a .ForEach (And the other functions that are missing from it, in my opinion at least), because I think that since it implements IEnumerable<T>, it should support .ForEach.

Consider if you actually need a variable…

Posted by Monty on June 16th, 2008

Take this scenario, you need to call a function, so you do the following:

I basically needed to call GetCustomAttribute(true) (if you know a better way to check custom attributes, please let me know!). But then I had an epiphany. Do I really need the trace and frame objects, or can I just do the following:

Which does exactly the same thing, but is shorter, and is easier to read and follow!

I love simplicitiy.

Increasing Productivity with Resharper

Posted by Monty on September 9th, 2007

For those people who rely on //TODO: Comments in their code, you should get Resharper! It will greatly improve how you can see TODO statements, among other things:

Todo Explorer

You can customise these options in the Resharper options page:

image

And you can edit the existing ones:

image

It actually works with files you dont even have open, unlike Visual Studio .NET’s Task list – great for those large projects you have!

If you dont have Resharper, I suggest you go grab it.

Technorati Tags: , ,


Copyright © 2007-2010 Muntedhar Alhakim. All rights reserved.