Menu

C# Generic List with a Fluent Interface

March 1, 2010 - .NET / C#
C# Generic List with a Fluent Interface

I was just working on some little data provider classes in a sandbox solution.  Just playing around with design patterns and I need a bit of data, nothing major.  A handful of classes that can act as little databases / data sources for my sandbox.  Just to make list a bit easier making lists of things in code I made this generic wrapper so make my code look nicer …

using System.Collections.Generic;

namespace JamSoft.Mvvm.ToyBox.Core

{

    public class FluentList<T> : List<T>

    {

        public new FluentList<T> Add(T obj)

        {

            base.Add(obj);

            return this;

        }

        public new FluentList<T> AddRange(IEnumerable<T> objs)

        {

            base.AddRange(objs);

            return this;

        }

    }

}

So, once you have this in your solution you can start to do things like this in code:

            var stringList = new FluentList<string>()

                .Add(“Moon”)

                .Add(“Alpha Centauri”)

                .Add(“Sirius”);

Pretty neat things these fluent ideas …

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

*