diff --git a/Class Examples/mutex.cs b/Class Examples/mutex.cs index ff93d3d..ae8cca9 100644 --- a/Class Examples/mutex.cs +++ b/Class Examples/mutex.cs @@ -1,45 +1,52 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading; - -namespace mutexexample2 -{ - class Program - { - public static Mutex m = new Mutex(); - public static readonly int LOOP = 50000; - public static long gvar = 0; - - static public void produce() - { - m.WaitOne(); - for (int i = 0; i < LOOP; i++) - gvar++; - m.ReleaseMutex(); - } - - static public void consume() - { - m.WaitOne(); - for (int i = 0; i < LOOP; i++) - gvar--; - m.ReleaseMutex(); - } - - static void Main(string[] args) - { - Thread t1 = new Thread(new ThreadStart(produce)); - t1.Start(); - - Thread t2 = new Thread(new ThreadStart(consume)); - t2.Start(); - - t1.Join(); - t2.Join(); - - Console.WriteLine(gvar); - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; + +namespace mutexexample2 +{ + class Program + { + public static Mutex m = new Mutex(); + public static readonly int LOOP = 50000; + public static long gvar = 0; + + static public void produce() + { + m.WaitOne(); + for (int i = 0; i < LOOP; i++) + gvar++; + m.ReleaseMutex(); + } + + static public void consume() + { + // Wait for the lock to be available + // If no other thread has it locked then it is + // by default available + m.WaitOne(); + for (int i = 0; i < LOOP; i++) + gvar--; + + // release/unlock the mutex lock + m.ReleaseMutex(); + } + + static void Main(string[] args) + { + // Example using a mutex lock + // Create 2 threads that use the same mutex lock variable + Thread t1 = new Thread(new ThreadStart(produce)); + t1.Start(); + + Thread t2 = new Thread(new ThreadStart(consume)); + t2.Start(); + + t1.Join(); + t2.Join(); + + Console.WriteLine(gvar); + } + } +} diff --git a/Class Examples/node-generic.cs b/Class Examples/node-generic.cs index 46ec617..403e7bd 100644 --- a/Class Examples/node-generic.cs +++ b/Class Examples/node-generic.cs @@ -1,67 +1,68 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace generics -{ - class Program - { - class Node where T : System.IComparable - { - public T element; - public Node left = null; - public Node right = null; - Node parent = null; - - public Node createNode(T element) - { - Node n = new Node() { parent = this, element = element }; - return n; - } - - public Node addLeft(T element) - { - this.left = createNode(element); - return this.left; - } - public Node addRight(T element) - { - this.right = createNode(element); - return this.right; - } - public override string ToString() - { - return "Node element => " + element.ToString(); - } - - } - static Node dfs(T val, Node parent) where T : System.IComparable - { - if (parent == null) - return null; - - if (parent.element.CompareTo(val) == 0) - return parent; - - Node ret = null; - if (parent.left != null) - ret = dfs(val, parent.left); - if (ret != null) - return ret; - if (parent.right != null) - ret = dfs(val, parent.right); - if (ret != null) - return ret; - return null; - } - static void Main(string[] args) - { - Node n = new Node(); - n.addLeft(4).addLeft(10).addLeft(7); - n.addRight(5).addRight(2); - Console.WriteLine(dfs(2, n)); - - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace generics +{ + class Program + { + class Node where T : System.IComparable + { + public T element; + public Node left = null; + public Node right = null; + Node parent = null; + + public Node createNode(T element) + { + Node n = new Node() { parent = this, element = element }; + return n; + } + + public Node addLeft(T element) + { + this.left = createNode(element); + return this.left; + } + public Node addRight(T element) + { + this.right = createNode(element); + return this.right; + } + public override string ToString() + { + return "Node element => " + element.ToString(); + } + + } + static Node dfs(T val, Node parent) where T : System.IComparable + { + if (parent == null) + return null; + + if (parent.element.CompareTo(val) == 0) + return parent; + + Node ret = null; + if (parent.left != null) + ret = dfs(val, parent.left); + if (ret != null) + return ret; + if (parent.right != null) + ret = dfs(val, parent.right); + if (ret != null) + return ret; + return null; + } + static void Main(string[] args) + { + // Binary tree but with any type for the object + Node n = new Node(); + n.addLeft(4).addLeft(10).addLeft(7); + n.addRight(5).addRight(2); + Console.WriteLine(dfs(2, n)); + + } + } +} diff --git a/Class Examples/node.cs b/Class Examples/node.cs index 72e78cd..ade7cb0 100644 --- a/Class Examples/node.cs +++ b/Class Examples/node.cs @@ -1,66 +1,67 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace generics -{ - class Program - { - class Node - { - public int element; - public Node left = null; - public Node right = null; - Node parent = null; - - public Node createNode(int element) - { - Node n = new Node() { parent = this, element = element }; - return n; - } - - public Node addLeft(int element) - { - this.left = createNode(element); - return this.left; - } - public Node addRight(int element) - { - this.right = createNode(element); - return this.right; - } - public override string ToString() - { - return "Node element => " + element.ToString(); - } - } - static Node dfs(int val, Node parent) - { - if (parent == null) - return null; - - if (parent.element == val) - return parent; - - Node ret = null; - if (parent.left != null) - ret = dfs(val, parent.left); - if (ret != null) - return ret; - if (parent.right != null) - ret = dfs(val, parent.right); - if (ret != null) - return ret; - return null; - } - static void Main(string[] args) - { - Node n = new Node(); - n.addLeft(4).addLeft(10).addLeft(7); - n.addRight(5).addRight(2); - Console.WriteLine(dfs(22, n)); - - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace generics +{ + class Program + { + class Node + { + public int element; + public Node left = null; + public Node right = null; + Node parent = null; + + public Node createNode(int element) + { + Node n = new Node() { parent = this, element = element }; + return n; + } + + public Node addLeft(int element) + { + this.left = createNode(element); + return this.left; + } + public Node addRight(int element) + { + this.right = createNode(element); + return this.right; + } + public override string ToString() + { + return "Node element => " + element.ToString(); + } + } + static Node dfs(int val, Node parent) + { + if (parent == null) + return null; + + if (parent.element == val) + return parent; + + Node ret = null; + if (parent.left != null) + ret = dfs(val, parent.left); + if (ret != null) + return ret; + if (parent.right != null) + ret = dfs(val, parent.right); + if (ret != null) + return ret; + return null; + } + static void Main(string[] args) + { + // Typical binary tree example + Node n = new Node(); + n.addLeft(4).addLeft(10).addLeft(7); + n.addRight(5).addRight(2); + Console.WriteLine(dfs(22, n)); + + } + } +} diff --git a/Class Examples/produce-consume.cs b/Class Examples/produce-consume.cs index 9a09a4a..8a0b943 100644 --- a/Class Examples/produce-consume.cs +++ b/Class Examples/produce-consume.cs @@ -1,40 +1,41 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading; - -namespace mutexexample2 -{ - class Program - { - public static readonly int LOOP = 50000; - public static long gvar = 0; - - static public void produce() - { - for (int i = 0; i < LOOP; i++) - gvar++; - } - - static public void consume() - { - for (int i = 0; i < LOOP; i++) - gvar--; - } - - static void Main(string[] args) - { - Thread t1 = new Thread(new ThreadStart(produce)); - t1.Start(); - - Thread t2 = new Thread(new ThreadStart(consume)); - t2.Start(); - - t1.Join(); - t2.Join(); - - Console.WriteLine(gvar); - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; + +namespace mutexexample2 +{ + class Program + { + public static readonly int LOOP = 50000; + public static long gvar = 0; + + static public void produce() + { + for (int i = 0; i < LOOP; i++) + gvar++; + } + + static public void consume() + { + for (int i = 0; i < LOOP; i++) + gvar--; + } + + static void Main(string[] args) + { + // Creating 2 threads with a simple race condition + Thread t1 = new Thread(new ThreadStart(produce)); + t1.Start(); + + Thread t2 = new Thread(new ThreadStart(consume)); + t2.Start(); + + t1.Join(); + t2.Join(); + + Console.WriteLine(gvar); + } + } +} diff --git a/Class Examples/swap.cs b/Class Examples/swap.cs index 055823e..be5f471 100644 --- a/Class Examples/swap.cs +++ b/Class Examples/swap.cs @@ -1,28 +1,29 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace generics -{ - class Program - { - static void swap(ref T lhs,ref T rhs) - { - T temp; - temp = lhs; - lhs = rhs; - rhs = temp; - } - static void Main(string[] args) - { - int a, b; - a = 10; - b = 20; - Console.WriteLine(a.ToString() + " " + b.ToString()); - swap(ref a, ref b); - Console.WriteLine(a.ToString() + " " + b.ToString()); - - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace generics +{ + class Program + { + static void swap(ref T lhs,ref T rhs) + { + T temp; + temp = lhs; + lhs = rhs; + rhs = temp; + } + static void Main(string[] args) + { + // example of a templated/generic swap + int a, b; + a = 10; + b = 20; + Console.WriteLine(a.ToString() + " " + b.ToString()); + swap(ref a, ref b); + Console.WriteLine(a.ToString() + " " + b.ToString()); + + } + } +} diff --git a/Day 1 Class Examples/example1/Program.cs b/Day 1 Class Examples/example1/Program.cs index 48b1b8d..21e7ef5 100644 --- a/Day 1 Class Examples/example1/Program.cs +++ b/Day 1 Class Examples/example1/Program.cs @@ -3,6 +3,10 @@ using System.Collections.Generic; using System.Linq; using System.Text; +/* +String concantation example +*/ + namespace example1 { class Program @@ -13,10 +17,10 @@ namespace example1 static void Main(string[] args) { Console.WriteLine("Hello world"); - //Test t = new Test(); - //t string s = "hello, "; s += "world"; + Test t = new Test(); + // Concat strings using plus or substitution Console.WriteLine("hello, " + s + s + s + "some other string" + s + "something"); Console.WriteLine("hello, {0.2} {1} {2} {3}", s, s, s, s); } diff --git a/Day 1 Class Examples/example2/Program.cs b/Day 1 Class Examples/example2/Program.cs index e90b62d..4782309 100644 --- a/Day 1 Class Examples/example2/Program.cs +++ b/Day 1 Class Examples/example2/Program.cs @@ -13,6 +13,7 @@ namespace example2 Console.WriteLine("Please enter 2 numbers"); n1 = Int32.Parse(Console.ReadLine()); n2 = Int32.Parse(Console.ReadLine()); + // Basic arithmetic examples Console.WriteLine("{0} + {1} = {2}", n1, n2, n1 + n2); Console.WriteLine("{0} * {1} = {2}", n1, n2, n1 * n2); Console.WriteLine("{0} / {1} = {2}", n1, n2, n1 / n2); diff --git a/Day 1 Class Examples/example3/Program.cs b/Day 1 Class Examples/example3/Program.cs index 5b9af75..57629d7 100644 --- a/Day 1 Class Examples/example3/Program.cs +++ b/Day 1 Class Examples/example3/Program.cs @@ -12,6 +12,7 @@ namespace example3 int tal = 0; string tmp; Console.WriteLine("Please enter 5 numbers:"); + // Simple for loop for (int i = 0; i < 5; i++) { tmp = Console.ReadLine(); diff --git a/Day 1 Class Examples/example4/Program.cs b/Day 1 Class Examples/example4/Program.cs index f19dab3..b87b9ba 100644 --- a/Day 1 Class Examples/example4/Program.cs +++ b/Day 1 Class Examples/example4/Program.cs @@ -25,6 +25,7 @@ namespace example4 int n2; int n1; Console.WriteLine("Please enter a number and a power:"); + // Using console input and function calls to implment power n1 = Int32.Parse(Console.ReadLine()); n2 = Int32.Parse(Console.ReadLine()); Math m = new Math(); diff --git a/Day 1 Class Examples/example5/Program.cs b/Day 1 Class Examples/example5/Program.cs index f61eb02..dfd4bb7 100644 --- a/Day 1 Class Examples/example5/Program.cs +++ b/Day 1 Class Examples/example5/Program.cs @@ -16,6 +16,7 @@ namespace example5 } static void Main(string[] args) { + // Example of recursive function Console.WriteLine("{0}", fact(3)); } } diff --git a/Day 2 Class Examples/example6/Program.cs b/Day 2 Class Examples/example6/Program.cs index 6334b30..10fa957 100644 --- a/Day 2 Class Examples/example6/Program.cs +++ b/Day 2 Class Examples/example6/Program.cs @@ -36,7 +36,7 @@ namespace example6 static void Main(string[] args) { - internalexamples. + // Different examples of function calls int x = 4; f1(x); Console.WriteLine(x); diff --git a/Day 2 Class Examples/example7/Program.cs b/Day 2 Class Examples/example7/Program.cs index f22bc13..c3ba9a4 100644 --- a/Day 2 Class Examples/example7/Program.cs +++ b/Day 2 Class Examples/example7/Program.cs @@ -19,6 +19,7 @@ namespace example7 class Ex2 : Example { + // inheriting and overriding a method public override void func() { base.func(x); @@ -27,6 +28,7 @@ namespace example7 static void Main(string[] args) { + // Example of modifying member variables Example e = new Example(); e.test2 = 2; } diff --git a/Day 2 Class Examples/example9/Program.cs b/Day 2 Class Examples/example9/Program.cs index 381ee65..3bee953 100644 --- a/Day 2 Class Examples/example9/Program.cs +++ b/Day 2 Class Examples/example9/Program.cs @@ -26,6 +26,7 @@ namespace example9 { static void Main(string[] args) { + // example showing destructors vs dispose { DestructTest t2 = new DestructTest(); }