Posts

Showing posts with the label C#

Converting a List using a lambda expression

Here is a little construct I use when I need to convert a List<T> to a List<U> or when I need to pass a list of concatenated values as a string. First, to convert a List<T> to a List<U> : List<Guid> guidList = // Initialize list here or get it as a parameter or whatever :) List<String> stringList = guidList.ConvertAll<String>(g => g.ToString()); It is really simple, I just use the ConvertAll method parameterized with the target type and pass it the lambda responsible for converting from the source type (Guid) to the target type (String). This example is obvious but the important thing is the combination of ConvertAll and a lambda. For the second use ("passing a list of concatenated values as a string"), it is also really simple : String stringValue = String.Join( " separator ", myGenericList.ConvertAll<String>( /* Insert lambda here that return a string value */).ToArray()); The trick is simply to ca...

Methods taking variable number of args (part 2)

In my previous post , I talked about the choice I made about taking a variable number of parameters. At that time, I used a simple params object[] , and the logic was built into the method (in the constructor of the underlying form in fact, but it is not relevant here). The problem with this approach is how to handle parameters with the same type but different meanings. A very simple example is the caption and the text of the InformationBox. Both are strings but how to differenciate between the two. I had to force the first parameter to be the text, but it was beginning to look like the MessageBox. For those who are familiar with C# 3.0, you may have already guessed what I could have used if .NET 3.5 had been available at that time. I could have used object initializers. It is a simple constructor syntax that allow the developer to specify only the properties he (or she) wants to define. For example, assuming the Client class is already defined with two properties "Name" and ...

Methods taking variable number of args (part 1)

A few months ago, I started a small project on Codeplex , its goal was simple : providing an alternative to the default Windows Forms MessageBox. One of the things that annoyed me with the MessageBox was to be forced to provide values for parameters I did not want to change. Here is a little example : MessageBox.Show("Text", "Caption", MessageBoxButtons.OK, MessageBoxIcon.Information); Clearly in this case, I should not have to specify the third parameter since it is already the default value. So basically, I needed to create a method ( Show in this case) that take a variable number of parameters. The problem was with the types of the parameters, I have a whole lot of different types. At that time, I had two ways to achieve that : Providing an overload of Show for each combination of parameters. Using params and the only type shared by all the parameters : object. The first one is nice when you have two parameters, but in InformationBox we are now dealing with up t...