Chapter 1:   Framework Fundamentals   produced by BichTran
Objectives <ul><li>Manage data by using the .NET Framework 2.0 system types. </li></ul><ul><li>Implement .NET Framework in...
Agenda <ul><li>Lesson 1: Using Value Types </li></ul><ul><li>Lesson 2: Using Common Reference Types </li></ul><ul><li>Less...
Using Value Types   <ul><li>Built-in types : sbyte – byte – short – int – uint – float </li></ul><ul><li>  long –double – ...
Using Value Types  (cont.) <ul><li>Use with new feature of Struct (operators) in .NET 2.0 </li></ul><ul><ul><ul><li>Cycle ...
Using Common Reference Types   <ul><li>There are about 2500 built-in reference types in the .NET Framework (not derived fr...
Constructing Classes   <ul><li>Inheritance:  use inheritance to create new types based on existing ones. </li></ul><ul><li...
Constructing Classes (cont.) With Generics Without Generics Gen<string, string> ga =  new Gen<string, string>(&quot;Hello,...
Constructing Classes (cont.) <ul><li>Event:  An event is a message sent by an object to signal the occurrence of an action...
Constructing Classes (cont.) <ul><li>Attributes:  Use attributes to describe assemblies, types, and members. </li></ul><ul...
Converting Between Types   <ul><li>Conversion:  2 kinds </li></ul><ul><ul><li>Widening  Conversion  :   the range of the d...
Q&A - Practice test
Thanks for your attention
Upcoming SlideShare
Loading in...5
×

Chapter 1 Presentation

307

Published on

MCTS

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
307
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Chapter 1 Presentation

  1. 1. Chapter 1: Framework Fundamentals produced by BichTran
  2. 2. Objectives <ul><li>Manage data by using the .NET Framework 2.0 system types. </li></ul><ul><li>Implement .NET Framework interfaces </li></ul><ul><li>Control interactions between components by using events and delegates </li></ul>
  3. 3. Agenda <ul><li>Lesson 1: Using Value Types </li></ul><ul><li>Lesson 2: Using Common Reference Types </li></ul><ul><li>Lesson 3: Constructing Classes </li></ul><ul><li>Lesson 4: Converting Between Types </li></ul><ul><li>Question and answer </li></ul><ul><li>Practice test </li></ul>
  4. 4. Using Value Types <ul><li>Built-in types : sbyte – byte – short – int – uint – float </li></ul><ul><li> long –double – decimal </li></ul><ul><li> char – bool – date – none (Pointer to a memory address) </li></ul><ul><li>Enumerations : - enum </li></ul><ul><li>User-defined types: - struct ( Operators is new in .NET 2.0) </li></ul><ul><ul><ul><li>struct Cycle { </li></ul></ul></ul><ul><ul><ul><li>int _val, _min, _max; </li></ul></ul></ul><ul><ul><ul><li>public Cycle (int min, int max) { _val = min; _min = min; _max = max;} </li></ul></ul></ul><ul><ul><ul><li>public static Cycle operator +(Cycle arg1, int arg2) { </li></ul></ul></ul><ul><ul><ul><li>arg1.Value += arg2; </li></ul></ul></ul><ul><ul><ul><li>return arg1; } </li></ul></ul></ul><ul><ul><ul><li> public static Cycle operator -(Cycle arg1, int arg2) { </li></ul></ul></ul><ul><li>arg1.Value -= arg2; </li></ul><ul><li>return arg1;} </li></ul><ul><ul><ul><li>} </li></ul></ul></ul>
  5. 5. Using Value Types (cont.) <ul><li>Use with new feature of Struct (operators) in .NET 2.0 </li></ul><ul><ul><ul><li>Cycle degrees = new Cycle(0, 359); </li></ul></ul></ul><ul><ul><ul><li>Cycle quarters = new Cycle(1, 4); </li></ul></ul></ul><ul><ul><ul><li>for (int i = 0; i <= 8; i++) { </li></ul></ul></ul><ul><ul><ul><li>degrees += 90; quarters += 1; </li></ul></ul></ul><ul><ul><ul><li>Console.WriteLine(&quot;degrees = {0}, quarters = {1}&quot;, </li></ul></ul></ul><ul><ul><ul><li>degrees, quarters); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><li>In short, structure types is suitable for simple data, size less than 16 bytes, not be changed after creation, not be cast to a reference type </li></ul><ul><li>Summary: </li></ul><ul><li>Assigning value-type variables, the data is copied and stored on the stack. </li></ul><ul><li>Value-types represent simple values, they have methods (as objects) </li></ul><ul><li>The Nullable type is new in .NET 2.0. </li></ul><ul><li>Case: use a variable as nullable when its value has not been assigned. </li></ul><ul><li>Ex: a bool variable has not been assigned </li></ul>
  6. 6. Using Common Reference Types <ul><li>There are about 2500 built-in reference types in the .NET Framework (not derived from System. ValueType ): </li></ul><ul><li>Object: the most general type, any type can be converted to Object. </li></ul><ul><li>String and StringBuilder: StringBuilder is more flexible than String </li></ul><ul><li>Exception : unexpected events that interrupt normal execution of an assembly. </li></ul><ul><li>Use following blocks in case catching exceptions: </li></ul><ul><ul><li>try {} </li></ul></ul><ul><ul><li>catch (Exception ex) {} </li></ul></ul><ul><ul><li>finally {} </li></ul></ul><ul><li>Tips: </li></ul><ul><li>- Your own exceptions will be derived from System.ApplicationException </li></ul><ul><li>- The runtime will execute only the first Catch block matching exception type. So order of Catch blocks should be from the most-specific to the least-specific . </li></ul><ul><li>,etc. </li></ul>
  7. 7. Constructing Classes <ul><li>Inheritance: use inheritance to create new types based on existing ones. </li></ul><ul><li>Interface: use interfaces to define a common set of members that must be implemented by related types. </li></ul><ul><li>For example, the IComparable , IDisposable, IConvertible .etc. </li></ul><ul><li>Partial class: use partial classes to split a class definition across multiple source files. </li></ul><ul><li>Use: “ partial ”is preceded keyword “ class ” </li></ul><ul><li>Generics (new in .NET 2.0) </li></ul><ul><li>NET 1.0 and 1.1 did not support generics. </li></ul>With Generics Without Generics public class Gen<T, U> { public T t; public U u; public Gen(T _t, U _u) { t = _t; u = _u;} } public class Obj { public Object t; public Object u; public Obj(Object _t, Object _u) { t = _t; u = _u; } }
  8. 8. Constructing Classes (cont.) With Generics Without Generics Gen<string, string> ga = new Gen<string, string>(&quot;Hello, &quot;, &quot;World!&quot;); string s = ga.t + ga.u; Gen<double, int> gb = new Gen<double, int>(10.125, 2005); double db = gb.t + gb.u; Obj oa = new Obj(&quot;Hello, &quot;, &quot;World!&quot;); string s = (string)oa.t + (string)oa.u); Obj ob = new Obj(10.125, 2005); double db = (double)ob.t + (int)ob.u); double db = (int)ob.t + (int)ob.u); // runtime exception
  9. 9. Constructing Classes (cont.) <ul><li>Event: An event is a message sent by an object to signal the occurrence of an action. </li></ul><ul><li>Delegate : A delegate is a class that can hold references to a methods which matches its signature. ( like method-pointer) </li></ul><ul><li>public event MyEventHandler MyEvent; </li></ul><ul><li>public delegate void MyEventHandler(object sender, EventArgs e); </li></ul><ul><li>Raise event : </li></ul><ul><li> MyEventHandler handler = MyEvent; </li></ul><ul><ul><ul><li>EventArgs e = new EventArgs(); </li></ul></ul></ul><ul><ul><ul><li>if (handler != null) { </li></ul></ul></ul><ul><ul><ul><li>// Invokes the delegates. </li></ul></ul></ul><ul><ul><ul><li>handler(this, e); } //C# checks to determine whether handler is null. </li></ul></ul></ul><ul><li>Respond to the event: </li></ul><ul><li> private void button1_Click(object sender, EventArgs e) {} </li></ul><ul><li>button1.Click += new System.EventHandler(this.button1_Click); </li></ul>
  10. 10. Constructing Classes (cont.) <ul><li>Attributes: Use attributes to describe assemblies, types, and members. </li></ul><ul><li>For example : [Serializable] </li></ul><ul><li>class ShoppingCartItem {….} </li></ul><ul><li>Type Forwarding (thinking) </li></ul><ul><li>Using attribute “TypeForwardedTo” to move a type from one class library to another </li></ul><ul><li>using System.Runtime.CompilerServices; </li></ul><ul><li>[assembly:TypeForwardedTo(typeof(DestLib.TypeA))] </li></ul>
  11. 11. Converting Between Types <ul><li>Conversion: 2 kinds </li></ul><ul><ul><li>Widening Conversion : the range of the destination type is wider than that of the source type.(implicit conversion) </li></ul></ul><ul><ul><li>Narrowing conversion: the range or precision of the source type exceeds that of the destination type, which usually requires explicit conversion. </li></ul></ul><ul><ul><li>NOTE : Use TryParse, TryParseExact, and TryCast are new in .NET 2.0. </li></ul></ul><ul><ul><li>Instead of attempting a parsing/conversion and catch the exception if it failed. </li></ul></ul><ul><li>Boxing and Unboxing </li></ul><ul><ul><li>Boxing converts a value type to a reference type. </li></ul></ul><ul><ul><li>Unboxing converts a reference type to a value type. </li></ul></ul>Unboxing Boxing object o = 123; int i = (int) o; int i = 123; object o = (object) i;
  12. 12. Q&A - Practice test
  13. 13. Thanks for your attention
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×