Navigation

Rocks

From Mono

Mono.Rocks is a library which aims at providing useful extensions methods for the base class libraries. In the context of this library, such an extension method is called a rock. I like that people type using Mono.Rocks.

Table of contents

Infrastructure

You can check out the source code from SVN in the `rocks` module.

The mailing list to propose, discuss and review rocks is the mono-rocks (http://groups.google.com/group/mono-rocks) google group.

Rocks

Int32

Times

Repeats an action n times.

Signature:

public static void Times (this int self, Action<int> action)

Usage:

6.Times (i => Console.WriteLine (i));

UpTo

Signature:

public static void UpTo (this int self, int limit, Action<int> action)

Usage:

6.UpTo (12, i => Console.WriteLine (i));

DownTo

Signature:

public static void DownTo (this int self, int limit, Action<int> action)

Usage:

12.DownTo (6, i => Console.WriteLine (i));

Step

Signature:

public static void Step (this int self, int limit, int step, Action<int> action)

Usage:

5.Step (9, 2, i => Console.WriteLine (i));

String

Slice

Signature:

public static string Slice (this string self, int start, int end)

Usage:

"01234".Slice (2, 4);
"01234".Slice (2, -1);

EachLine

Signature:

public static void EachLine (this string self, Action<string> action)

Usage:

@"
one
two
three".EachLine (line => Console.WriteLine (line));

ToEnum

Signature:

public static TEnum ToEnum<TEnum> (this string self)

Usage:

"Bar".ToEnum<Foo> ()

IEnumerable

Join

Signature:

public static string Join<TSource> (this IEnumerable<TSource> self, string separator)

Usage:

new int [] { 0, 1, 2, 3, 4, 5 }.Join (", ")

Repeat

Signature:

public static IEnumerable<TSource> Repeat<TSource> (this IEnumerable<TSource> self, int number)

Usage:

new [] {"foo", "bar"}.Repeat (2)


PathCombine

Signature:

public static string PathCombine (this IEnumerable<string> self)

Usage:

var path = new [] {"/home", "jb", "sources"}.PathCombine ();

ICustomAttributeProvider

GetCustomAttribute

Signature:

public static TAttribute GetCustomAttribute<TAttribute> (this ICustomAttributeProvider self) where TAttribute : Attribute

Usage:

FooAttribute foo = typeof (Bar).GetCustomAttribute<FooAttribute> ();

GetCustomAttributes

Signature:

public static TAttribute [] GetCustomAttributes<TAttribute> (this ICustomAttributeProvider self) where TAttribute : Attribute

Usage:

var attributes = typeof (Bar).GetCustomAttributes<FooAttribute> ();