.Net Framework 2 fundamentals


Published on

.Net Framework 2 fundamentals

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

.Net Framework 2 fundamentals

  1. 1. Chapter -1Framework Fundamentals<br />HarshanaWeerasinghe<br />http://www.harshana.info<br />http://Blog.harshana.info<br />
  2. 2. Framework Fundamentals<br />Create a console or Windows Forms application in Visual Studio.<br />Add namespaces and references to system class libraries to a project.<br />Run a project in Visual Studio, set breakpoints, step through code, and watch the values of variables.<br />By Harshana Weerasinghe (http://www.harshana.info)<br />2<br />
  3. 3. Types<br />Mainly 2 types in Microsoft .Net<br />Value Type<br />Reference Type<br />By Harshana Weerasinghe (http://www.harshana.info)<br />3<br />
  4. 4. Value Types<br />Value types are variables that contain their data directly 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.<br />By Harshana Weerasinghe (http://www.harshana.info)<br />4<br />
  5. 5. Built-in Value Types <br />By Harshana Weerasinghe (http://www.harshana.info)<br />5<br />
  6. 6. Other Value Types<br />By Harshana Weerasinghe (http://www.harshana.info)<br />6<br />
  7. 7. Declare Value Types<br /><type> <VariableName> [= <Default Value>];<br />boolbolTestVariable= false;<br />Nullable<bool>b= null; <br /> Or<br />bool? b = null;<br />By Harshana Weerasinghe (http://www.harshana.info)<br />7<br />
  8. 8. Create User-Defined Types<br />structCycle<br />{<br /> // Private fields<br />int_val, _min, _max;<br />// Constructor<br />public Cycle(intmin, intmax)<br /> {<br /> _val = min;<br /> _min = min;<br /> _max = max;<br /> }<br /> public intValue<br /> {<br /> get { return _val; }<br /> set<br /> {<br /> if (value > _max)<br /> _val = _min;<br /> else<br /> {<br /> if (value < _min)<br /> _val = _max;<br /> else<br /> _val = value;<br /> }<br /> }<br /> }<br /> public override string ToString()<br /> {<br /> return Value.ToString();<br /> }<br /> public intToInteger()<br /> {<br /> return Value;<br /> }<br />}<br />By Harshana Weerasinghe (http://www.harshana.info)<br />8<br />
  9. 9. Using Operators (new in .NET 2.0)*<br />public static Cycle operator +(Cycle arg1, int arg2)<br />{<br /> arg1.Value += arg2;<br /> return arg1;<br />}<br />public static Cycle operator -(Cycle arg1,intarg2)<br />{<br /> arg1.Value -= arg2;<br /> return arg1;<br />}<br />By Harshana Weerasinghe (http://www.harshana.info)<br />9<br />
  10. 10. Use User-Defined Types<br />Cycle degrees =new Cycle(0, 359);<br />Cycle quarters= new Cycle(1, 4);<br />for (int i = 0; i <= 8; i++)<br />{<br /> degrees += 90; quarters += 1;<br />Console.WriteLine("degrees = {0}, quarters = {1}", degrees, quarters);<br />}<br />By Harshana Weerasinghe (http://www.harshana.info)<br />10<br />
  11. 11. Enumerations<br />enumTitles : int{ Mr, Ms, Mrs, Dr };<br />Titles t = Titles.Dr;<br />Console.WriteLine("{0}.", t);// Displays "Dr."<br />By Harshana Weerasinghe (http://www.harshana.info)<br />11<br />
  12. 12. Reference Types<br />Reference types store the address of their data, also 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 garbagecollection. Garbage collection recovers memory periodically as needed by disposing of items that are no longer referenced.<br />By Harshana Weerasinghe (http://www.harshana.info)<br />12<br />
  13. 13. Built-in Reference Types<br />By Harshana Weerasinghe (http://www.harshana.info)<br />13<br />
  14. 14. Strings and String Builders<br />string s="this is some text to search";<br />s = s.Replace("search", "replace");<br />Console.WriteLine(s);<br />string s;<br />s = "wombat"; // "wombat“<br />s += " kangaroo";// "wombat kangaroo“<br />s += " wallaby"; // "wombat kangaroo wallaby“<br />s += " koala";// "wombat kangaroo wallaby koala”<br />Console.WriteLine(s);<br />By Harshana Weerasinghe (http://www.harshana.info)<br />14<br />
  15. 15. String Builder<br />using System.Text;<br />StringBuildersb =new StringBuilder(30);<br />sb.Append("wombat");// Build string.<br />sb.Append(" kangaroo");<br />sb.Append(" wallaby");<br />sb.Append(" koala");<br />string s = sb.ToString(); // Copy result to string.<br />Console.WriteLine(s);<br />By Harshana Weerasinghe (http://www.harshana.info)<br />15<br />
  16. 16. Create and Sort Arrays<br />// Declare and initialize an array.<br />int[] ar = { 3, 1, 2 };<br />// Call a shared/static array method.<br />Array.Sort(ar);<br />// Display the result.<br />Console.WriteLine("{0}, {1}, {2}", ar[0], ar[1], ar[2]);<br />By Harshana Weerasinghe (http://www.harshana.info)<br />16<br />
  17. 17. Streams<br />Streams are another very common type because they are the means for reading from and writing to the disk and communicating across the network. The System.IO.Streamtype is the base type for all task-specific stream types. <br />By Harshana Weerasinghe (http://www.harshana.info)<br />17<br />
  18. 18. Streams Cont…<br />using System.IO;<br />// Create and write to a text file<br />StreamWritersw =new StreamWriter("text.txt");<br />sw.WriteLine("Hello, World!");<br />sw.Close();<br />// Read and display a text file<br />StreamReadersr =new StreamReader("text.txt");<br />Console.WriteLine(sr.ReadToEnd());<br />sr.Close();<br />By Harshana Weerasinghe (http://www.harshana.info)<br />18<br />
  19. 19. Throw and Catch Exceptions<br />StreamReadersr = new StreamReader("text.txt");<br />try<br />{<br />Console.WriteLine(sr.ReadToEnd());<br />}<br />catch (Exception ex)<br />{<br />// If there are any problems reading the file, display an error message<br />Console.WriteLine("Error reading file: " + ex.Message);<br />}<br />finally<br />{<br />// Close the StreamReader, whether or not an exception occurred<br />sr.Close();<br />}<br />By Harshana Weerasinghe (http://www.harshana.info)<br />19<br />
  20. 20. Constructing Classes<br />public class Cycle<br />{<br />// Private fields<br />int_val, _min, _max;<br />// Constructor<br />public Cycle(intmin,intmax)<br /> {<br /> _val = min;<br /> _min = min;<br /> _max = max;<br /> }<br />//Property<br />public intValue<br /> {<br /> get { return _val; }<br /> set { _val = value; }<br /> }<br />//Method<br />public string MyValue()<br /> {<br /> return _val.ToString();<br /> } <br />}<br />By Harshana Weerasinghe (http://www.harshana.info)<br />20<br />
  21. 21. Inheritance – Animal Class<br />class Animal<br />{<br />string m_Name;<br />intm_Age;<br /> public Animal(string _Name)<br /> {<br />this.Name = _Name; <br />}<br /> public string Name<br /> {<br /> get { return m_Name; }<br /> set { m_Name = value; }<br /> }<br /> public intAge<br /> {<br /> get { return m_Age; }<br /> set { m_Age = value; }<br /> }<br /> public string Speak()<br /> {<br /> return "Hello Animal";<br /> }<br />}<br />By Harshana Weerasinghe (http://www.harshana.info)<br />21<br />
  22. 22. Cat n Dog Classes<br />class Cat : Animal<br />{<br />double m_Weight;<br /> public double Weight<br /> {<br /> get { return m_Weight; }<br /> set { m_Weight = value; }<br /> }<br /> public string Speak()<br /> {<br /> return "Meaw, Meaw";<br /> }<br />}<br />class Dog : Animal<br />{<br />//Default Ctor<br />public Dog()<br /> {<br /> }<br />//Other Ctor<br />public Dog(string _Name,int_Age)<br /> : base(_Name)//Call Base Class Ctor<br /> {<br />this.Age = _Age;<br /> }<br /> public string Speak()<br /> {<br /> if (this.Age >3) <br /> return " Grr.. Grr..";<br />else<br /> return " Buhh.. Buhh..";<br /> }<br />}<br />By Harshana Weerasinghe (http://www.harshana.info)<br />22<br />
  23. 23. Interfaces<br />Interfaces, also known as contracts, define a common set 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.<br />By Harshana Weerasinghe (http://www.harshana.info)<br />23<br />
  24. 24. By Harshana Weerasinghe (http://www.harshana.info)<br />24<br />
  25. 25. Sample<br />interface IMessage<br />{<br />// Send the message. Returns True is success, False otherwise.<br />boolSend();<br />// The message to send.<br />string Message { get; set; }<br />// The Address to send to.<br />string Address { get; set; }<br />}<br />class EmailMessage : IMessage<br />{<br />public boolSend()<br /> {<br /> throw new Exception("The method or operation is not implemented.");<br />}<br />public string Message<br />{<br /> get<br /> {<br /> throw new Exception("The method or operation is not implemented.");<br /> }<br />set<br /> {<br /> throw new Exception("The method or operation is not implemented.");<br /> }<br /> }<br />public string Address<br /> {<br /> get<br />{<br /> throw new Exception("The method or operation is not implemented.");<br /> }<br />set<br /> {<br /> throw new Exception("The method or operation is not implemented.");<br />}<br /> }<br />}<br />By Harshana Weerasinghe (http://www.harshana.info)<br />25<br />
  26. 26. Commonly used interfaces<br />By Harshana Weerasinghe (http://www.harshana.info)<br />26<br />
  27. 27. Partial Classes - new in .NET 2.0<br />Partial classes allow you to split a class definition 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.<br />By Harshana Weerasinghe (http://www.harshana.info)<br />27<br />
  28. 28. Generics<br />Generics are part of the .NET Framework's type system 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.<br />By Harshana Weerasinghe (http://www.harshana.info)<br />28<br />
  29. 29. Generics Cont..<br />class Obj<br />{<br />public Object t;<br />public Object u;<br />public Obj(Object _t,Object _u)<br />{<br /> t = _t;<br /> u = _u;<br /> }<br />}<br />class Gen<T, U><br />{<br />public T t;<br /> public U u;<br /> public Gen(T _t, U _u)<br />{<br /> t = _t;<br /> u = _u;<br /> }<br />}<br />By Harshana Weerasinghe (http://www.harshana.info)<br />29<br />
  30. 30. Generics Cont..<br />// Add two strings using the Obj class<br />Objoa =new Obj("Hello, ", "World!");<br />Console.WriteLine((string)oa.t + (string)oa.u);<br />// Add two strings using the Gen class<br />Gen<string, string>ga = new Gen<string, string>("Hello, ", "World!");<br />Console.WriteLine(ga.t + ga.u);<br />// Add a double and an int using the Obj class<br />Objob =new Obj(10.125, 2005);<br />Console.WriteLine((double)ob.t + (int)ob.u);<br />// Add a double and an int using the Gen class<br />Gen<double,int> gb = new Gen<double, int>(10.125, 2005);<br />Console.WriteLine(gb.t + gb.u);<br />By Harshana Weerasinghe (http://www.harshana.info)<br />30<br />
  31. 31. Generics Cont..<br />// Add a double and an int using the Gen class<br />Gen<double, int> gc = new Gen<double,int>(10.125, 2005);<br />Console.WriteLine(gc.t + gc.u);<br />// Add a double and an int using the Obj class<br />Objoc =new Obj(10.125, 2005);<br />Console.WriteLine((int)oc.t + (int)oc.u);<br />last line contains an error— the oc.tvalue 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. <br />By Harshana Weerasinghe (http://www.harshana.info)<br />31<br />
  32. 32. Events<br />By Harshana Weerasinghe (http://www.harshana.info)<br />32<br />