Shuffle List in C#


Yesterday, I was looking for an algorithm to shuffle a list of objects in C#. So in my mind, I started to think about "complex" algorithms to do that, using random numbers and list elements switching...

I say "complex" because while I was googling, I found a good solution to do that ! And it takes only 1 line :p I would have never thinked of that :) :)

Here is the cool code:

var shuffledList = myObjectList.OrderBy(item => Guid.NewGuid());

So, that's really simple. You can use the "OrderBy()" Linq extension to rearrange the list. This method will take a 'random' object, in this case a GUID. And we know that GUIDs are 'unique' so, it does the job!

You can use variants like this one:

var rnd = new Random();
var shuffledList = myObjectList.OrderBy(rnd.Next());

Next step, compare performances, between different solutions...

1 commentaires:

Visual Studio for MAC, review (fr)

If you are a developer, there are many chance that you already used Visual Studio, the famous Microsoft's IDE.

Visual Studio for MAC is borned a few weeks ago. But is it a productive tool like on PC ? If you are a Xamarin Developer and never tried it, I suggest you to take a look at this short review to get some clues !

This review is avaible in french on

0 commentaires:

[Xamarin Forms] Custom bottom bordered entry (iOS & Android)

Custom entry with renderers

For my Xamarin Forms project I needed to render an 'Entry' control with a bottom border of a specific color. There are several possibilities but I will show you mine.

For that, I used renderers. iOS one is particularly not trivial.
The wished result look like that:

iOS Renderer

[assembly: ExportRenderer(typeof(ExtDatePicker), typeof(ExtDatePickerRenderer))]
namespace MyCompany.iOS.Renderers
    public class ExtDatePickerRenderer : DatePickerRenderer
        protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)

            // Need to connect to Sizechanged event because first render time, Entry has no size (-1).
            if (e.NewElement != null)
                e.NewElement.SizeChanged += (obj, args) =>
                    var xamEl = obj as DatePicker;
                    if (xamEl == null)

                    // get native control (UITextField)
                    var entry = this.Control;

                    // Create borders (bottom only)
                    CALayer border = new CALayer();
                    float width = 1.0f;
                    border.BorderColor = new CoreGraphics.CGColor(0.73f, 0.7451f, 0.7647f);  // gray border color
                    border.Frame = new CGRect(x: 0, y: xamEl.Height - width, width: xamEl.Width, height: 1.0f);
                    border.BorderWidth = width;


                    entry.Layer.MasksToBounds = true;
                    entry.BorderStyle = UITextBorderStyle.None;
                    entry.BackgroundColor = new UIColor(1, 1, 1, 1); // white


In iOS, the native control used to represent an 'Entry' is a 'UITextField' control.
The key point is that we need our renderer to update this 'UITextField' on SizeChanged event and not on the traditional 'OnElementChanged' event.

Why because, the size is not yet computed when we enter the 'OnElementChanged' event. So we can draw our bottom line as we don't know its lenght...

Android Renderer

For Android, I decided to make a really simple renderer that will use a drawable resource as a background. Here is how I did:

First step, add the xml file that contains the "shape" in your android project resources:

The 'shape' (xml drawable resource)
To make the bottom line, I found on Stackoverflow a nice solution. You just have to generate a gradient from the desired border color to the transparent color with the border color having a really small height (making a line).

Here is the code:
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="" android:shape="rectangle">
  <gradient android:angle="90"

And finally the renderer:
[assembly: ExportRenderer(typeof(ExtEntry), typeof(ExtEntryRenderer))]
namespace GoodChapp.Android.Renderers
    public class ExtEntryRenderer : EntryRenderer

        protected override void OnElementChanged(ElementChangedEventArgs e)

            if (Control != null)
                // Background

The final results (Android & iOS)

Surely, you can change the border color and any other properties !

7 commentaires:

[Xamarin Forms] build error: Unable to cast object of type 'Xamarin.Forms.Xaml.ElementNode' to type 'Xamarin.Forms.Xaml.ValueNode'

Xaml build error

For one of my Xamarin project, I just had this build error:
This error was related to my XAML code:

Unable to cast object of type 'Xamarin.Forms.Xaml.ElementNode' to type 'Xamarin.Forms.Xaml.ValueNode'

After searching for a long 'moment', I just didn't understand what happened...

My investigation (this process can help you :p)

So I decided to remove a large part of my xaml code and then re-add it little by little. Each step I tried to compile to see when error occurs.

So In my case, I determined that the piece of code that was going wrong was the following:

Ok, it was my mistake but in the code editor, Visual Studio didn't tell me anything.
I was trying to bind an event (I though it was a property) to a DateTime property... What is not possible.

Hope this helps someone to save a few minutes :)

2 commentaires: