//The MIT License (MIT) //http://arolibraries.codeplex.com/license using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace AroLibraries.ExtensionMethods.Strings { public static class StringExt { /// <summary> Converts the string representation of a Guid toits Guid /// equivalent. A return value indicates whether the operation succeeded. /// </summary> /// <param name="s">A string containing a Guid to convert.</param> /// <param name="result"> /// When this method returns, contains the Guid value equivalent to /// the Guid contained in <paramref name="s"/>, if the conversion succeeded, or <see cref="Guid.Empty"/> if theconversion failed. /// The conversion fails if the /// <paramref name="s"/> parameter is a /// <see langword="null" /> reference (<see langword="Nothing" /> in /// Visual Basic), or is not of the correct format. /// </param> /// /// <value> /// <see langword="true" /> if <paramref name="s"/> was converted /// successfully; otherwise, <see langword="false" />. </value> /// <exception cref="ArgumentNullException"> /// Thrown if <pararef name="s"/> is <see langword="null"/>. /// </exception> /// <remarks> /// Original code at https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=94072&wa=wsignin1.0#tabs </remarks> public static bool Ext_IsValidGuid(this string s) { if (s == null) throw new ArgumentNullException("s"); Regex format = new Regex("^[A-Fa-f0-9]{32}$|" + "^({|()?[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}(}|))?$|" + "^({)?[0xA-Fa-f0-9]{3,10}(, {0,1}[0xA-Fa-f0-9]{3,6}){2},{0,1}({)([0xA-Fa-f0-9]{3,4}, {0,1}){7}[0xA-Fa-f0-9]{3,4}(}})$"); Match match = format.Match(s); return match.Success; } } }
Author: coder
Create Guid
using System; public class MainClass { public static void Main() { Guid g1 = new Guid("{0d57629c-7d6e-4847-97cb-9e2fc25083fe}"); Guid g2 = new Guid("0d57629c7d6e484797cb9e2fc25083fe"); Console.WriteLine(g1 == g2); // True } }
GCCollectionMode.Optimized
using System; class Program { static void Main(string[] args) { GC.Collect(2, GCCollectionMode.Optimized); } }
Finalizable Disposable Class with using
using System; using System.Collections.Generic; using System.Text; public class MyResourceWrapper : IDisposable { public void Dispose() { Console.WriteLine("In Dispose() method!"); } } class Program { static void Main(string[] args) { MyResourceWrapper rw = new MyResourceWrapper(); if (rw is IDisposable) rw.Dispose(); using (MyResourceWrapper rw2 = new MyResourceWrapper()) { } } }
Print out how many times a generation has been swept.
using System;
using System.Collections.Generic;
using System.Text;
public class Car {
private int currSp;
private string petName;
public Car() { }
public Car(string name, int speed) {
petName = name;
currSp = speed;
}
public override string ToString() {
return string.Format(“{0} is going {1} MPH”,petName, currSp);
}
}
class Program {
static void Main(string[] args) {
Car refToMyCar = new Car(“A”, 100);
Console.WriteLine(refToMyCar.ToString());
Console.WriteLine(GC.GetGeneration(refToMyCar));
object[] tonsOfObjects = new object[50000];
for (int i = 0; i < 50000; i++)
tonsOfObjects[i] = new object();
GC.Collect(0);
GC.WaitForPendingFinalizers();
Console.WriteLine("Generation of refToMyCar is: {0}",GC.GetGeneration(refToMyCar));
if (tonsOfObjects[9000] != null) {
Console.WriteLine("Generation of tonsOfObjects[9000] is: {0}",GC.GetGeneration(tonsOfObjects[9000]));
} else
Console.WriteLine("tonsOfObjects[9000] is no longer alive.");
Console.WriteLine("
Gen 0 has been swept {0} times", GC.CollectionCount(0));
Console.WriteLine("Gen 1 has been swept {0} times", GC.CollectionCount(1));
Console.WriteLine("Gen 2 has been swept {0} times", GC.CollectionCount(2));
}
public static void MakeACar() {
Car myCar = new Car();
}
}
[/csharp]
IDisposable interface
using System; namespace Client.Chapter_5___Building_Your_Own_Classes { public class DTOR: IDisposable { public static int[] MyIntArray; private static int ObjectCount = 0; private bool Disposed = false; static void Main(string[] args) { MyIntArray = new int[10]; ObjectCount++; } //Used to clean up and free unmanaged resources //Never mark this class as virtual as you do not want derived //classes to be able to override it. public void Dispose() { //if this class is derived then call the base //class dispose. //base.Dispose(); //Call the overloaded version of dispose Dispose(true); //Tell the CLR not to run the finalizer this way //you do not free unmanaged resources twice GC.SuppressFinalize(this); } //If user calls dispose both managed and unmanaged resources //are freed //If the finalizer is called then only unmanaged resources are freed private void Dispose(bool disposing) { if(!this.Disposed) { if(disposing) { //free any managed resources } //free unmanaged resources } Disposed = true; } //This finalizer method is called by the GC, //not the user. The net result of having this is that //the object will always survive the first GC cycle and //will be collected the next time GC1 is collected. ~DTOR() { Dispose(false); } } }
Get Total Memory
using System; public class MyClass { public void Dispose() { Console.WriteLine("Dispose()"); GC.SuppressFinalize(this); } } public class ReRegFinalApp { public static void DoSomething() { MyClass t = new MyClass(); Console.WriteLine(t); t.Dispose(); GC.ReRegisterForFinalize(t); } public static void Main(string[] args) { long n = GC.GetTotalMemory(true); Console.WriteLine("start of Main: {0} bytes allocated", n); DoSomething(); n = GC.GetTotalMemory(true); Console.WriteLine("end of Main: {0} bytes allocated", n); } }