Skip to content

Instantly share code, notes, and snippets.

View AndrewBarfield's full-sized avatar

Andrew Barfield AndrewBarfield

View GitHub Profile
@AndrewBarfield
AndrewBarfield / gist:2556924
Created April 30, 2012 09:49
C#: How to perform Numerical Integration
using System;
namespace NumericalIntegration {
class Program {
static void Main(string[] args) {
int iterations = 100000;
double x, start, end, dist, sum = 0, sumT = 0;
Console.Write( "Input range start: " );
start = double.Parse( Console.ReadLine() );
@AndrewBarfield
AndrewBarfield / gist:2556878
Created April 30, 2012 09:39
C#: LINQ: How to construct an XML element
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace LINQExample {
class Program {
static void Main() {
var e = new XElement( "Person",
@AndrewBarfield
AndrewBarfield / gist:2556793
Created April 30, 2012 09:23
C#: System.Collections.IEnumerable: Custom Iterator Example
using System;
namespace IteratorsExample {
class Program {
static void Main(string[] args) {
// Create an instance of the collection class
EvenIntegers ei = new EvenIntegers();
// Iterate with foreach
foreach ( int num in ei ) {
@AndrewBarfield
AndrewBarfield / gist:2556782
Created April 30, 2012 09:19
C#: IDisposable: Cleaning up managed and unmanaged resources
public class MyResource : IDisposable {
// Pointer to an external unmanaged resource.
private IntPtr handle;
// Other managed resource this class uses.
private Component component = new Component();
// Track whether Dispose has been called.
private bool disposed = false;
@AndrewBarfield
AndrewBarfield / gist:2556767
Created April 30, 2012 09:16
C#: IComparable: Converting miles to kilometers
using System;
using System.Collections;
/*
* Sample Output
*
* 10 miles (16.09344 km)
* 86 miles (138.403584 km)
* 146 miles (234.964224 km)
* 214 miles (344.399616 km)
@AndrewBarfield
AndrewBarfield / gist:2556751
Created April 30, 2012 09:13
C#: System.Collections.Hashtable
using System;
using System.Collections;
namespace HashtableExample {
class Program {
static void Main(string[] args) {
// Create a new hashtable
Hashtable genericDomains = new Hashtable();
@AndrewBarfield
AndrewBarfield / gist:2556747
Created April 30, 2012 09:11
C#: System.Collections.Generic: Working with a Generic SortedList
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericSortedList {
class Program {
static void Main(string[] args) {
// Creates and initializes a new SortedList.
@AndrewBarfield
AndrewBarfield / gist:2556740
Created April 30, 2012 09:09
C#: System.Collections.Generic: Enumerating a Generic Queue
using System;
using System.Collections.Generic;
namespace GenericQueueEnumerator {
class Program {
static void Main(string[] args) {
// Initialize a new instance of Queue<string>
Queue<string> myQ = new Queue<string>();
@AndrewBarfield
AndrewBarfield / gist:2556734
Created April 30, 2012 09:08
C#: System.Collections.Generic: Generic Queue
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericQueue {
class Program {
static void Main(string[] args) {
// Creates and initializes a new Queue<string>.
@AndrewBarfield
AndrewBarfield / gist:2556542
Created April 30, 2012 08:27
C#: System.Collections.Generic: Enumerating a Generic list
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericListEnumerator {
class Program {
public class Domain {