Homework 2 review - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Homework 2 review

Description:

Use a.dict.Keys in foreach instead of just 'a' Write complex code when simple one is OK ... overloading multiple versions of the same operator... The long way: ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 12
Provided by: kam77
Category:
Tags: homework | keys | review

less

Transcript and Presenter's Notes

Title: Homework 2 review


1
Homework 2 review
  • 09/20/02

2
Homework 1 reviewSolution
  • using System
  • using System.Collections
  • using System.Diagnostics
  • namespace BC.Core
  • using Exceptions
  • namespace Collections
  • Serializable
  • public class Set ICollection, ICloneable
  • protected Hashtable S
  • public Set ()
  • S new Hashtable()
  • public Set (object o)
  • this()

3
Homework 1 reviewSolution (cont.)
  • . . .
  • public int Count
  • get
  • return S.Count
  • public bool Empty
  • get
  • return Count 0
  • public bool Contains (object o)
  • return S.Contains(o)

4
Homework 1 reviewSolution (cont.)
  • . . .
  • public bool IsSynchronized
  • get
  • return S.Keys.IsSynchronized
  • public object SyncRoot
  • get
  • return S.Keys.SyncRoot
  • public void Add (object o)
  • So null

5
Homework 1 reviewSolution (cont.)
  • . . .
  • public object Clone ()
  • Set r new Set()
  • foreach (object o in this)
  • r.Add(o)
  • return r
  • public static bool isNull (Set s)
  • return (object)s null
  • public object Singleton
  • get
  • Debug.Assert(Count lt 1)
  • foreach (object o in this)

6
Homework 1 reviewSolution (cont.)
  • . . .
  • public static bool operator lt (Set s1, Set
    s2)
  • foreach (object o in s1)
  • if (!s2.Contains(o))
  • return false
  • return true
  • public static bool operator gt (Set s1, Set
    s2)
  • return s2 lt s1
  • public static bool operator (Set s1, Set
    s2)
  • if (isNull(s1) isNull(s2))
  • return isNull(s1) isNull(s2)
  • else
  • return s1 lt s2 s2 lt s1

7
Homework 1 reviewSolution (cont.)
  • . . .
  • public static Set operator (Set s1, Set s2)
  • Set r new Set()
  • foreach (object o in s1)
  • if (s2.Contains(o))
  • r.Add(o)
  • return r
  • public static Set operator (Set s1, Set s2)
  • Set r new Set()
  • foreach (object o in s1)
  • r.Add(o)
  • foreach (object o in s2)
  • r.Add(o)
  • return r
  • public static Set operator - (Set s1, Set s2)

8
Homework 1 reviewSolution (cont.)
  • . . .
  • public override bool Equals (object o)
  • Set s o as Set
  • return s ! null this s
  • public override int GetHashCode ()
  • int r 0
  • foreach (object o in this)
  • r o.GetHashCode()
  • return r
  • public override string ToString ()
  • string r "\n"
  • foreach (object o in this)
  • r string.Format("\t0\n", o)
  • return r "\n"

9
Homework 1 reviewWhat was useful
  • foreach
  • Properties
  • Operator overloading

10
Homework 1 reviewWhat not to do
  • Write Java code in C
  • It works, but that is not the point
  • Use enumerators explicitly
  • Use for when you can use foreach
  • Propagate Equals and GetHashCode to the base
    class
  • Make array operations take linear (or more) time
  • Use a.dict.Keys in foreach instead of just a
  • Write complex code when simple one is OK
  • Build strings the old way , , ,

11
Homework 1 reviewA General Guideline
  • Use conversions instead of overloading multiple
    versions of the same operator
  • The long way
  • Set operator (Set s, IEnumerable e)
  • Set operator (IEnumerable e, Set s)
  • Instead
  • Set operator (Set s1, Set s2)
  • implicit operator Set (IEnumerable e)
Write a Comment
User Comments (0)
About PowerShow.com