Sunteți pe pagina 1din 5

using System; namespace Foo { public class Money { public Money() { } public void Print( ) { Console.WriteLine("Foo.Money.

Print"); } } } namespace Bar { public class Money { public Money( ) { } public void Print( ) { Console.WriteLine("Bar.Money.Print"); } } } public class MainClass { public static void Main( ) { Foo.Money fm = new Foo.Money(); Bar.Money bm = new Bar.Money(); fm.Print( ); bm.Print( ); } } ~~~~~~~~~~~~~~~~ using System; class RefTest { /* This method changes its argument. Notice the use of ref. */ public void sqr(ref int i) { i = i * i; } } class MainClass { public static void Main() { RefTest ob = new RefTest(); int a = 10; Console.WriteLine("a before call: " + a); ob.sqr(ref a); // notice the use of ref Console.WriteLine("a after call: " + a); } }

~~~~~~~~~~~~~~~~~ //Passing ref-types by value using System; class Person { public string fullName; public int age; public Person(string n, int a) { fullName = n; age = a; } public void PrintInfo() { Console.WriteLine("{0} is {1} years old", fullName, age); } } class MainClass { public static void SendAPersonByValue(Person p) { p.age = 99; p = new Person("TOM", 999); } public static void Main() { Person fred = new Person("Fred", 12); fred.PrintInfo(); SendAPersonByValue(fred); fred.PrintInfo(); } } ~~~~~~~~~~~~~~~~ Exception Meaning ArrayTypeMismatchException Type is incompatible with the type of the array. DivideByZeroException Division by zero attempted. IndexOutOfRangeException Array index is out of bounds. InvalidCastException A runtime cast is invalid. OutOfMemoryException Insufficient free memory exists. OverflowException An arithmetic overflow occurred. NullReferenceException An attempt was made to operate on a null reference that is , a reference that does not refer to an object. StackOverflowException The stack was Overflow. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ using System; class MainClass { public static void Main() { int[] myArray = new int[2];

Console.WriteLine("Attempting to access an invalid array element"); myArray[2] = 1; } } ~~~~~~~~~~~~~~~~ using System; class MainClass { public static int AnExceptionFunction(int value) { if (value == 0) // Can't divide by zero throw new DivideByZeroException("Divide By 0 error!"); int x = 20 / value; return x; } public static void Main() { int value = 0; try { value = AnExceptionFunction(10); // This works ok Console.WriteLine("Value = {0}", value); AnExceptionFunction(0); // This doesn't Console.WriteLine("Value = {0}", value); } catch (Exception e) { Console.WriteLine("Caught an exception {0}. Continuing", e); } Console.WriteLine("Done"); } } ~~~~~~~~~~~~~~~ using System; class MainClass{ public static void Main(){ Console.WriteLine("Before catch"); int Zero = 0; try { int j = 22 / Zero; } catch (Exception e) { Console.WriteLine("Exception " + e.Message); } Console.WriteLine("After catch"); } } ~~~~~~~~~~~~~~~~~~~ using System; enum Values { A = 1, B = 2, C = A + B, D = A * C + 33 }

class MainClass { public static void Member(Values value) { Console.WriteLine(value); } public static void Main() { Values i= Values.D; Console.WriteLine("I Value {0}",(int)i); } } ~~~~~~~~~~~~~~~ using System; class Building { public int area; public int occupants; } class BuildingDemo { public static void Main() { Building house = new Building(); Building office = new Building(); int areaPP; // area per person house.occupants = 4; house.area = 2500; office.occupants = 25; office.area = 4200; areaPP = house.area / house.occupants; Console.WriteLine("house has:\n " + house.occupants + " occupants\n " + house.area + " total area\n " + areaPP + " area per person"); Console.WriteLine(); areaPP = office.area / office.occupants; Console.WriteLine("office has:\n " + office.occupants + " occupants\n " + office.area + " total area\n " + areaPP + " area per person"); } } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ using System; using System.IO; public class MainClass { public static void Main() { FileInfo MyFile = new FileInfo(@"c:\Test.txt");

MyFile.Create(); } } ~~~~~~~~~~~~~~~~ using System; using System.IO; public class MainClass { public static int Main(string[] args) { FileInfo f = new FileInfo(@"C:\Test.txt"); FileStream fs = f.Create(); Console.WriteLine("Creation: {0}", f.CreationTime); Console.WriteLine("Full name: {0}", f.FullName); Console.WriteLine("Full atts: {0}", f.Attributes.ToString()); fs.Close(); f.Delete(); return 0; } } ~~~~~~~~~~~~~~~~~~~

S-ar putea să vă placă și