Your SlideShare is downloading. ×
0
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

.Net Framework 2 fundamentals

397

Published on

.Net Framework 2 fundamentals

.Net Framework 2 fundamentals

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
397
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Chapter -1Framework Fundamentals<br />HarshanaWeerasinghe<br />http://www.harshana.info<br />http://Blog.harshana.info<br />
  • 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. 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. 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. Built-in Value Types <br />By Harshana Weerasinghe (http://www.harshana.info)<br />5<br />
  • 6. Other Value Types<br />By Harshana Weerasinghe (http://www.harshana.info)<br />6<br />
  • 7. Declare Value Types<br />&lt;type&gt; &lt;VariableName&gt; [= &lt;Default Value&gt;];<br />boolbolTestVariable= false;<br />Nullable&lt;bool&gt;b= null; <br /> Or<br />bool? b = null;<br />By Harshana Weerasinghe (http://www.harshana.info)<br />7<br />
  • 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 &gt; _max)<br /> _val = _min;<br /> else<br /> {<br /> if (value &lt; _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. 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. Use User-Defined Types<br />Cycle degrees =new Cycle(0, 359);<br />Cycle quarters= new Cycle(1, 4);<br />for (int i = 0; i &lt;= 8; i++)<br />{<br /> degrees += 90; quarters += 1;<br />Console.WriteLine(&quot;degrees = {0}, quarters = {1}&quot;, degrees, quarters);<br />}<br />By Harshana Weerasinghe (http://www.harshana.info)<br />10<br />
  • 11. Enumerations<br />enumTitles : int{ Mr, Ms, Mrs, Dr };<br />Titles t = Titles.Dr;<br />Console.WriteLine(&quot;{0}.&quot;, t);// Displays &quot;Dr.&quot;<br />By Harshana Weerasinghe (http://www.harshana.info)<br />11<br />
  • 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. Built-in Reference Types<br />By Harshana Weerasinghe (http://www.harshana.info)<br />13<br />
  • 14. Strings and String Builders<br />string s=&quot;this is some text to search&quot;;<br />s = s.Replace(&quot;search&quot;, &quot;replace&quot;);<br />Console.WriteLine(s);<br />string s;<br />s = &quot;wombat&quot;; // &quot;wombat“<br />s += &quot; kangaroo&quot;;// &quot;wombat kangaroo“<br />s += &quot; wallaby&quot;; // &quot;wombat kangaroo wallaby“<br />s += &quot; koala&quot;;// &quot;wombat kangaroo wallaby koala”<br />Console.WriteLine(s);<br />By Harshana Weerasinghe (http://www.harshana.info)<br />14<br />
  • 15. String Builder<br />using System.Text;<br />StringBuildersb =new StringBuilder(30);<br />sb.Append(&quot;wombat&quot;);// Build string.<br />sb.Append(&quot; kangaroo&quot;);<br />sb.Append(&quot; wallaby&quot;);<br />sb.Append(&quot; koala&quot;);<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. 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(&quot;{0}, {1}, {2}&quot;, ar[0], ar[1], ar[2]);<br />By Harshana Weerasinghe (http://www.harshana.info)<br />16<br />
  • 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. Streams Cont…<br />using System.IO;<br />// Create and write to a text file<br />StreamWritersw =new StreamWriter(&quot;text.txt&quot;);<br />sw.WriteLine(&quot;Hello, World!&quot;);<br />sw.Close();<br />// Read and display a text file<br />StreamReadersr =new StreamReader(&quot;text.txt&quot;);<br />Console.WriteLine(sr.ReadToEnd());<br />sr.Close();<br />By Harshana Weerasinghe (http://www.harshana.info)<br />18<br />
  • 19. Throw and Catch Exceptions<br />StreamReadersr = new StreamReader(&quot;text.txt&quot;);<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(&quot;Error reading file: &quot; + 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. 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. 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 &quot;Hello Animal&quot;;<br /> }<br />}<br />By Harshana Weerasinghe (http://www.harshana.info)<br />21<br />
  • 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 &quot;Meaw, Meaw&quot;;<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 &gt;3) <br /> return &quot; Grr.. Grr..&quot;;<br />else<br /> return &quot; Buhh.. Buhh..&quot;;<br /> }<br />}<br />By Harshana Weerasinghe (http://www.harshana.info)<br />22<br />
  • 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. By Harshana Weerasinghe (http://www.harshana.info)<br />24<br />
  • 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(&quot;The method or operation is not implemented.&quot;);<br />}<br />public string Message<br />{<br /> get<br /> {<br /> throw new Exception(&quot;The method or operation is not implemented.&quot;);<br /> }<br />set<br /> {<br /> throw new Exception(&quot;The method or operation is not implemented.&quot;);<br /> }<br /> }<br />public string Address<br /> {<br /> get<br />{<br /> throw new Exception(&quot;The method or operation is not implemented.&quot;);<br /> }<br />set<br /> {<br /> throw new Exception(&quot;The method or operation is not implemented.&quot;);<br />}<br /> }<br />}<br />By Harshana Weerasinghe (http://www.harshana.info)<br />25<br />
  • 26. Commonly used interfaces<br />By Harshana Weerasinghe (http://www.harshana.info)<br />26<br />
  • 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. Generics<br />Generics are part of the .NET Framework&apos;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. 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&lt;T, U&gt;<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. Generics Cont..<br />// Add two strings using the Obj class<br />Objoa =new Obj(&quot;Hello, &quot;, &quot;World!&quot;);<br />Console.WriteLine((string)oa.t + (string)oa.u);<br />// Add two strings using the Gen class<br />Gen&lt;string, string&gt;ga = new Gen&lt;string, string&gt;(&quot;Hello, &quot;, &quot;World!&quot;);<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&lt;double,int&gt; gb = new Gen&lt;double, int&gt;(10.125, 2005);<br />Console.WriteLine(gb.t + gb.u);<br />By Harshana Weerasinghe (http://www.harshana.info)<br />30<br />
  • 31. Generics Cont..<br />// Add a double and an int using the Gen class<br />Gen&lt;double, int&gt; gc = new Gen&lt;double,int&gt;(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&apos;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. Events<br />By Harshana Weerasinghe (http://www.harshana.info)<br />32<br />

×