diff --git a/Day 2 Class Examples/example10/Program.cs b/Day 2 Class Examples/example10/Program.cs new file mode 100644 index 0000000..b4294c3 --- /dev/null +++ b/Day 2 Class Examples/example10/Program.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace example10 +{ + class Program + { + class EvenInt + { + int val = 0; + + public delegate void CallbackFun(int newval, int oldval); + public event CallbackFun Changed; + + public int Value + { + get { return val; } + set + { + if (Changed != null) + Changed(value, val); + val = value; + } + } + } + static void Main(string[] args) + { + EvenInt num = new EvenInt(); + num.Changed += new EvenInt.CallbackFun(num_Changed); + num.Changed += new EvenInt.CallbackFun(num_Changed2); + + num.Value = 20; + num.Value += 10; + } + + static void num_Changed2(int newval, int oldval) + { + Console.WriteLine("numchanged2"); + } + + static void num_Changed(int newval, int oldval) + { + Console.WriteLine("Changed from {0} to {1}", oldval, newval); + } + } +} diff --git a/Day 2 Class Examples/example11/Program.cs b/Day 2 Class Examples/example11/Program.cs new file mode 100644 index 0000000..65d281f --- /dev/null +++ b/Day 2 Class Examples/example11/Program.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace example11 +{ + class Program + { + static void Main(string[] args) + { + int[] x = new int[10]; + for (int i = 0; i < x.Length; i++) + x[i] = i; + + foreach (int z in x) + Console.WriteLine(z); + + List lst1 = new List(); + for (int i = 0; i < 10; i++) + lst1.Add(i); + + foreach (int z in lst1) + Console.WriteLine(z); + + ArrayList al1 = new ArrayList(); + for (int i = 0; i < 10; i++) + al1.Add(i); + + foreach (int z in al1) + Console.WriteLine(z); + + int w = (int)al1[0]; + } + } +} diff --git a/Day 2 Class Examples/example12/Program.cs b/Day 2 Class Examples/example12/Program.cs new file mode 100644 index 0000000..ea5d5ee --- /dev/null +++ b/Day 2 Class Examples/example12/Program.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace example12 +{ + class MyException : Exception + { + public MyException(string msg) : base(msg) + { + + } + } + class Program + { + static void Main(string[] args) + { + //throw new Exception("this is a boring exception"); + throw new MyException("this is a fun exception"); + } + } +} diff --git a/Day 2 Class Examples/example6/Program.cs b/Day 2 Class Examples/example6/Program.cs new file mode 100644 index 0000000..6334b30 --- /dev/null +++ b/Day 2 Class Examples/example6/Program.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace example6 +{ + internal class PassTest + { + public PassTest(int x) { } + public List vals = new List(); + } + + class Program + { + static void f1(int x) + { + x = 5; + } + + static void f2(ref int x) + { + x = 5; + } + + static void f3(PassTest t) + { + t.vals.Add(5); + } + + static void f4(ref PassTest t) + { + t = new PassTest(5); + t.vals.Add(10); + } + + static void Main(string[] args) + { + internalexamples. + int x = 4; + f1(x); + Console.WriteLine(x); + f2(ref x); + Console.WriteLine(x); + PassTest t = new PassTest(x); + Console.WriteLine(string.Join(",", t.vals.ToArray())); + f3(t); + Console.WriteLine(string.Join(",", t.vals.ToArray())); + f4(ref t); + Console.WriteLine(string.Join(",", t.vals.ToArray())); + } + } +} diff --git a/Day 2 Class Examples/example7/Program.cs b/Day 2 Class Examples/example7/Program.cs new file mode 100644 index 0000000..f22bc13 --- /dev/null +++ b/Day 2 Class Examples/example7/Program.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace example7 +{ + class Program + { + class Example + { + private int test1; + public int test2; + protected int test3; + + public virtual void func(int x) { } + private void func2() { } + } + + class Ex2 : Example + { + public override void func() + { + base.func(x); + } + } + + static void Main(string[] args) + { + 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 new file mode 100644 index 0000000..381ee65 --- /dev/null +++ b/Day 2 Class Examples/example9/Program.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace example9 +{ + class DisposeTest : IDisposable + { + + public void Dispose() + { + Console.WriteLine("Cleaning up DisposeTest"); + } + } + + class DestructTest + { + ~DestructTest() + { + Console.WriteLine("Cleaning up DestructTest"); + } + } + + class Program + { + static void Main(string[] args) + { + { + DestructTest t2 = new DestructTest(); + } + //GC.Collect(3, GCCollectionMode.Forced); + Console.WriteLine("top"); + using (DisposeTest t = new DisposeTest()) + { + + } + Console.WriteLine("bottom"); + } + } +}