Last active
December 17, 2015 17:29
-
-
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")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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