Mindig is irigykedtem, hogy sok fejlesztő publikál példa kódokat, amelyek másoknak is jók lehetnek. Most mi is összegyűjtöttünk egy párat, hátha…
Tömb összefűzés, rendezési opcióval
public static T[] Concatenate<T>(params T[][] arrays)
{
return Concatenate<T>(null, arrays);
}
public static T[] Concatenate<T>(Comparison<T> comparison, params T[][] arrays)
{
List<T> result = new List<T>();
foreach (T[] array in arrays)
result.AddRange(array);
if (comparison != null)
result.Sort(comparison);
return result.ToArray();
}
Tömb "egyszeresítés"
public static T[] Distinct<T>(T[] array)
{
return Distinct(array, null);
}
public static T[] Distinct<T>(T[] array, Comparison<T> comparison)
{
List<T> result = new List<T>();
foreach (T item in array)
if (!result.Contains(item))
result.Add(item);
if (comparison != null)
result.Sort(comparison);
return result.ToArray();
}
Tömb összekavarás
public static T[] Shuffle<T>(T[] array)
{
Random rnd = new Random();
int n = array.Length;
while (n > 1)
{
int k = rnd.Next(n);
n--;
T temp = array[n];
array[n] = array[k];
array[k] = temp;
}
return array;
}
Várakoztató kurzor kezelő
public class WaitCursor : IDisposable
{
public WaitCursor()
{
ContextHandler.WaitCursorOn();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
}
ContextHandler.WaitCursorOff();
}
disposed = true;
}
~WaitCursor()
{
Dispose(false);
}
}
X64 kezelő
public static class X64Handler
{
public static bool IsX64 { get { return IntPtr.Size == 8; } }
public static string Display(bool suppress32) { return IsX64 ? "x64" : suppress32 ? null : "x86"; }
}