Skip to content

Instantly share code, notes, and snippets.

@AlexArchive
AlexArchive / StartupController
Last active December 18, 2015 00:19
Single Instance Startup Controller for Windows Forms Applications
//<sumary>
//Sample Usage of the StartupController class
//</sumary>
static class Program
{
[STAThread]
static void Main()
{
StartupController startupController = new StartupController();
startupController.RunOnce<MainForm>();
@AlexArchive
AlexArchive / RandomDouble.vb
Created June 16, 2013 18:06
Random Double
Dim random As New Random()
...
Dim randomDouble As Double =
Math.Round((random.NextDouble() * 6), 2)
@AlexArchive
AlexArchive / PortChecker.cs
Created June 17, 2013 20:20
Check if a TCP port is available / already in use on local machine
private static bool PortAvailableForListening(int portNumber)
{
IPEndPoint[] activeTcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
return activeTcpListeners.Select(x => x.Port == portNumber).FirstOrDefault();
}
@AlexArchive
AlexArchive / DelegateProxy.vb
Created June 22, 2013 06:38
Delegate "proxy" to Convert.ToInt32(char value)
Public Delegate Function ToInt32Proxy(ByVal value As Char) As Integer
..
Dim someDelegate =
[Delegate].CreateDelegate(GetType(ToInt32Proxy), GetType(Convert).GetMethod("ToInt32", New Type() {GetType(Char)}))
Dim someProxy = DirectCast(someDelegate, ToInt32Proxy)
Dim someValue = someProxy("a")
@AlexArchive
AlexArchive / EnumerableExtension
Last active December 18, 2015 20:58
Convert an Enum to an IEnumerable so you can iterate over the values
internal static IEnumerable<T> ToEnumerableOf<T>(this Enum theEnum)
{
return Enum.GetValues(theEnum.GetType()).Cast<T>();
}
@AlexArchive
AlexArchive / gist:5846845
Created June 23, 2013 22:59
The yield keyword is merely syntactic sugar. This class demonstrates what is generated under the hood when you use yield return.
public class Program
{
private static readonly Random _random = new Random();
private static void Main()
{
foreach (int randomNumber in GetRandomNumbers(10))
Console.WriteLine(randomNumber);
Console.ReadKey();
@AlexArchive
AlexArchive / gist:5854294
Created June 24, 2013 22:32
Basic JavaScriptSerializer Deserialize example
Imports System.Web.Script.Serialization
Module Program
Sub Main()
Const jsonResponse = "{""a"":1,""b"":2,""c"":3,""d"":4,""e"":5}"
Dim serializer As New JavaScriptSerializer()
Dim response As Response = serializer.Deserialize(Of Response)(jsonResponse)
@AlexArchive
AlexArchive / gist:5866012
Last active December 19, 2015 00:00
Git Cheat Sheet
git remote -v #output remotes
git remote rm REMOTENAME #remove remote
git add -A #include deleted files
git commit -am "message" #add and commit at the same time
rm -rf .git #Remove git init
===UniX Hecks
@AlexArchive
AlexArchive / gist:5883416
Created June 28, 2013 08:54
LINQ Compilation
//data source
string[] names = { "ByteBlast", "Shockwave2", "Salmoneus", "Banksy" };
//query syntax
var names1 =
from name in names
where name.StartsWith("B")
select name;
//compiler translates the above into :-
@AlexArchive
AlexArchive / Problem22
Created July 5, 2013 09:10
Project Euler: Problem 22
private static IEnumerable<string> LoadNames()
{
string names =File.ReadAllText("names.txt");
return names.Split(',')
.Select(name => name.Substring(1, name.Length - 2));
}
private static int CalculateTotalScore()
{
return LoadName().OrderBy(x => x)