.NET, C#, Tips

IEnumerable to Comma Delimited String

A few years ago I blogged about how to take a delimited string and convert it to a IEnumerable.  Today we ran across the opposite of this in that we wanted to take a list and convert it to a delimited string.   This can come in handy for a number of situations so it’s worth taking a look.

Given:

List<int> values = new List<int> {10, 20, 25};

In older versions of .NET (prior to 4.0 I believe) the easiest way was to do something like the following:

string newString = string.Join(",", values.ToArray());

However, starting in 4.0, the Join has some additional overloads that make this just crazy simple:

string newString = string.Join(",", values);

If you have a list of strings, there is still some benefit to converting this to an array. There is another overload of Join that allows you to be more selective in what you are converting.

Given a list of strings like the following:

List<string> strings = new List<string> {"a", "b", "c", "d", "e", "f"};

We can look at the last overload of; String.Join(delimiter, string[], startIndex, count)

This gives us a delimited string, starting at the element in the array at startIndex and going for the number of count.

string delimited = string.Join(",", strings.ToArray(), 2, 2);

will return “c,d”

I love how the framework provides these methods, often with multiple overloads to give us a lot of power with very little code. Enjoy!

.NET, Resharper

Resharper – Navigate to Type Goodness

In my opinion the navigation improvements made by Resharper are where much of my productivity gains come into play.  When I can quickly find and navigate around my project, I can stay more focused on solving the problem at hand rather than trying to fumble through my project structure remembering where things are.

One of my favorites lately is Go To Type of Symbol (Ctrl-Shift-T in Resharper keymap, Ctrl-Shift-F11 in the Visual Studio keymap).  At it’s simplest, put your cursor on a variable and hit the keystroke.

Photo1

If there is only one match, it simply takes you to the definition of the type (in your source or object browser if not).  Simple, but quick and easy.

The fun begins when there is more than one type.   The handiest example of that is when I have a List<> variable.   Then the Go To Type brings up not only the IList<> ( which I realize may be of limited usage), but it also brings up the type in the list (which is normally what I want to get at).

photo2

This get’s me quickly to what I’m looking for.

One more shortcut in my arsenal to move me quickly around my project.  Enjoy!

Resharper

Resharper – Paste from Clipboard History

Sometimes it’s the simple things that make my day when I’m coding.  This is one of those items that once I bothered to learn the keystroke, has made my time coding that much more productive.

I’ve used Resharper for years (since v2) and while I’ve stumbled upon this feature a few times, I’ve never really bothered to get in the habit of using it.

It’s the Pasting Items from Clipboard History  feature.   Shortcut for this is Ctrl+Shift+Alt+V in both keyboard themes (Note:  I did notice that in my VS2015 RC installation the keystroke was Ctrl+Shift+V, so it could be either of those depending on your setup)

When you select the paste (and assuming you’ve pasted some items to the clipboard while in Visual Studio) you’ll be presented with the clipboard screen;

Resharper-Paste-Multiple

From that screen it’s simple to cursor down to the entry you want and hit Enter to paste it into your current location.  You can also select the number or letter that prefixes the selection.  Pretty simple.

In just two days of using this feature, I’d say it’s increased my performance when I’m making multiple similar changes in some code.   Sometimes it really just is the simple things that really make a difference.