Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Chain some <T>hings

0.00/5 (No votes)
8 Oct 2015 1  
A small class to simplify creating an array with the help of a fluent interface... sort of.

Introduction

I was slightly tired of doing things that way

new T[]
{
      Foo
    , Bar
}

especially when I am forgetting the brackets (which happens way to often :doh:) and T is is similar to IStillCouldUseSomeMoreLettersAndAddSomeNumbersToo

Using the code

By using a different approach

class Chain<T> {

    private Chain() { }

    private readonly List<T> _Chain = new List<T>();

    public static Chain<T> Start => new Chain<T>();

    public Chain<T> this[T This_] {
        get { this._Chain.Add(This_); return this; }
    }

    public T[] End => this._Chain.ToArray();
}

it could be used like this

Chain<String>.Start
    ["Hello"]
    ["World"]
.End;

In short, it is nothing fancy, but for me it is quite usefull.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here