Studio. Add namespaces and references to system class libraries to a project. Run a project in Visual Studio, set breakpoints, step through code, and watch the values of variables. By Harshana Weerasinghe (http://www.harshana.info) 2
instead of containing a reference to the data stored elsewhere in memory. Instances of value types are stored in an area of memory called the stack, where the runtime can create, read, update, and remove them quickly with minimal overhead. By Harshana Weerasinghe (http://www.harshana.info) 4
known as a pointer, on the stack. The actual data that address refers to is stored in an area of memory called the heap. The runtime manages the memory used by the heap through a process called garbage collection. Garbage collection recovers memory periodically as needed by disposing of items that are no longer referenced. By Harshana Weerasinghe (http://www.harshana.info) 12
the means for reading from and writing to the disk and communicating across the network. The System.IO.Stream type is the base type for all task-specific stream types. By Harshana Weerasinghe (http://www.harshana.info) 17
StreamWriter sw = new StreamWriter("text.txt"); sw.WriteLine("Hello, World!"); sw.Close(); // Read and display a text file StreamReader sr = new StreamReader("text.txt"); Console.WriteLine(sr.ReadToEnd()); sr.Close(); By Harshana Weerasinghe (http://www.harshana.info) 18
(Exception ex) { // If there are any problems reading the file, display an error message Console.WriteLine("Error reading file: " + ex.Message); } finally { // Close the StreamReader, whether or not an exception occurred sr.Close(); } By Harshana Weerasinghe (http://www.harshana.info) 19
{ this.Name = _Name; } public string Name { get { return m_Name; } set { m_Name = value; } } public int Age { get { return m_Age; } set { m_Age = value; } } public string Speak() { return "Hello Animal"; } } By Harshana Weerasinghe (http://www.harshana.info) 21
of members that all classes that implement the interface must provide. For example, the IComparable interface defines the CompareTo method, which enables two instances of a class to be compared for equality. All classes that implement the IComparable interface, whether custom- created or built in the .NET Framework, can be compared for equality. By Harshana Weerasinghe (http://www.harshana.info) 23
success, False otherwise. bool Send(); // The message to send. string Message { get; set; } // The Address to send to. string Address { get; set; } } class EmailMessage : IMessage { public bool Send() { throw new Exception("The method or operation is not implemented."); } public string Message { get { throw new Exception("The method or operation is not implemented."); } set { throw new Exception("The method or operation is not implemented."); } } public string Address { get { throw new Exception("The method or operation is not implemented."); } set { throw new Exception("The method or operation is not implemented."); } } } By Harshana Weerasinghe (http://www.harshana.info) 25
across multiple source files. The benefit of this approach is that it hides details of the class definition so that derived classes can focus on more significant portions. By Harshana Weerasinghe (http://www.harshana.info) 27
that allows you to define a type while leaving some details unspecified. Instead of specifying the types of parameters or member classes, you can allow code that uses your type to specify it. This allows consumer code to tailor your type to its own specific needs. By Harshana Weerasinghe (http://www.harshana.info) 28
Obj(Object _t, Object _u) { t = _t; u = _u; } } class Gen<T, U> { public T t; public U u; public Gen(T _t, U _u) { t = _t; u = _u; } } By Harshana Weerasinghe (http://www.harshana.info) 29
= new Obj("Hello, ", "World!"); Console.WriteLine((string)oa.t + (string)oa.u); // Add two strings using the Gen class Gen<string, string> ga = new Gen<string, string>("Hello, ", "World!"); Console.WriteLine(ga.t + ga.u); // Add a double and an int using the Obj class Obj ob = new Obj(10.125, 2005); Console.WriteLine((double)ob.t + (int)ob.u); // Add a double and an int using the Gen class Gen<double, int> gb = new Gen<double, int>(10.125, 2005); Console.WriteLine(gb.t + gb.u); By Harshana Weerasinghe (http://www.harshana.info) 30
class Gen<double, int> gc = new Gen<double, int>(10.125, 2005); Console.WriteLine(gc.t + gc.u); // Add a double and an int using the Obj class Obj oc = new Obj(10.125, 2005); Console.WriteLine((int)oc.t + (int)oc.u); last line contains an error — the oc.t value is cast to an Int instead of to a double. Unfortunately, the compiler won't catch the mistake. Instead, a run-time exception is thrown when the runtime attempts to cast a double to an Int value. By Harshana Weerasinghe (http://www.harshana.info) 31