Skip to content

Instantly share code, notes, and snippets.

@TryJSIL
TryJSIL / tryjsil.cs
Created February 28, 2013 07:51
Simple Unsafe Structs
using System;
using System.Runtime.InteropServices;
public static class Program {
public static unsafe void Main (string[] args) {
var bytes = new byte[8];
fixed (byte* pBytes = bytes) {
var pStruct = (MyStruct*)pBytes;
*pStruct = new MyStruct {
@TryJSIL
TryJSIL / tryjsil.cs
Created February 24, 2013 11:57
Unsafe Code Test
using System;
public static class Program {
public static unsafe void Main (string[] args) {
var ints = new int[] { 0, 1, 2, 3 };
fixed (int* pInts = ints) {
var pBytes = (byte*)pInts;
for (var i = 0; i < (ints.Length * 4); i++)
@TryJSIL
TryJSIL / tryjsil.cs
Created November 7, 2012 11:33
Int64
using System;
public class Program {
public static void Main () {
var i = 0;
var x = 1L;
Console.WriteLine("Left shift");
Console.WriteLine(x);
Console.WriteLine("{0} {1}", 1, Format(x << 1));
@TryJSIL
TryJSIL / Yield.cs
Created May 27, 2012 08:52
Generators
using System;
using System.Collections;
using System.Collections.Generic;
public static class Program {
public static IEnumerable<int> OneToNine {
get {
for (int i = 0; i < 10; i++)
yield return i;
}
@TryJSIL
TryJSIL / Reflection.cs
Created April 27, 2012 13:08
Reflection
using System;
using System.Reflection;
using System.Collections.Generic;
public static class Program {
public static void Main (string[] args) {
Common.Util.ListMembers<MethodInfo>(
typeof(T),
BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.Public
);
@TryJSIL
TryJSIL / Interfaces.cs
Created April 27, 2012 13:06
Interfaces
using System;
public interface I {
void Foo ();
}
public class A : I {
public void Foo () {
Console.WriteLine("A");
}
@TryJSIL
TryJSIL / BinaryTrees.cs
Created April 27, 2012 12:59
Tree Benchmark
/* The Computer Language Benchmarks Game
http://shootout.alioth.debian.org/
contributed by Marek Safar
*/
using System;
public static class Program {
const int minDepth = 4;
@TryJSIL
TryJSIL / InheritGenericClass.cs
Created April 27, 2012 12:57
Generic Inheritance
using System;
public class GenericClass<T> {
public virtual void Method (T value) {
Console.WriteLine("GenericClass<{0}>.Method({1})", typeof(T), value);
}
}
public class MyClass<T> : GenericClass<T> {
public override void Method (T value) {
@TryJSIL
TryJSIL / MulticastDelegates.cs
Created April 27, 2012 12:56
Multicast Delegates
using System;
public static class Program {
public static void Main (string[] args) {
Action<int> a =
(i) => Console.WriteLine("a({0})", i);
Action<int> b =
(i) => Console.WriteLine("b({0})", i);
Action<int> c = (Action<int>)Delegate.Combine(a, b);
@TryJSIL
TryJSIL / Nullables.cs
Created April 27, 2012 12:55
Nullables
using System;
public static class Program {
public static void PrintNullable (int? i) {
if (i.HasValue)
Console.Write("{0} ", i.Value);
else
Console.Write("null ");
}