Skip to content

Instantly share code, notes, and snippets.

@MattRix
Last active December 17, 2015 17:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattRix/5646734 to your computer and use it in GitHub Desktop.
Save MattRix/5646734 to your computer and use it in GitHub Desktop.
Extension methods for logging arrays and lists in C#, just call someArray.Log() or someList.Log(), or if you want a name logged with it, then just someArray.Log("The list of things")
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//USAGE:
//someArray.Log();
//someArray.Log("a name");
//someList.Log();
//someList.Log("a name");
public static class RXExtensions
{
public static void Log<T>(this List<T> list) {list.Log("");}
public static void Log<T>(this List<T> list, string name)
{
StringBuilder builder = new StringBuilder();
if(name != "")
{
builder.Append(name);
builder.Append(": ");
}
builder.Append('[');
int count = list.Count;
for(int t = 0;t<count;t++)
{
builder.Append(list[t].ToString());
if(t < count-1) builder.Append(',');
}
builder.Append(']');
Debug.Log(builder.ToString());
}
public static void Log<T>(this T[] items) {items.Log("");}
public static void Log<T>(this T[] items, string name)
{
StringBuilder builder = new StringBuilder();
if(name != "")
{
builder.Append(name);
builder.Append(": ");
}
builder.Append('[');
int count = items.Length;
for(int t = 0;t<count;t++)
{
builder.Append(items[t].ToString());
if(t < count-1) builder.Append(',');
}
builder.Append(']');
Debug.Log(builder.ToString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment