Your SlideShare is downloading. ×
Chapter 1 Presentation
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

Chapter 1 Presentation

290
views

Published on

MCTS

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